看到以前那些不堪入目的畫作 ...,練畫真的有血有淚啊。
為了熟習 C# 的 syntax 所以做了一些簡單的 Leetcode 題目,
以下測試 syntaxhighlighter,我的 code 是解答這題:
https://leetcode.com/problems/longest-substring-without-repeating-characters/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using System; using System.Collections.Generic; namespace Leetcode { public class Solution { public int LengthOfLongestSubstring( string s) { if (s.Length < 2) { return s.Length; } else { var resultingLengths = new List< int >(); for ( int i = 0; i < s.Length - 1; i++) { int count = 1; string substring = Convert.ToString(s[i]); for ( int j = i + 1; j < s.Length; j++) { if (substring.IndexOf(s[j]) != -1) { break ; } else { substring += s[j]; count++; } } resultingLengths.Add(count); } return resultingLengths.Max(); } } } } |
No comments:
Post a Comment