看到以前那些不堪入目的畫作 ...,練畫真的有血有淚啊。
為了熟習 C# 的 syntax 所以做了一些簡單的 Leetcode 題目,
以下測試 syntaxhighlighter,我的 code 是解答這題:
https://leetcode.com/problems/longest-substring-without-repeating-characters/
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