[ 命名建議 ] LeetCode 3. Longest Substring Without Repeating Characters #116
Answered
by
twy30
LPenny-github
asked this question in
Q&A
-
|
LeetCode: 線上教材: 再麻煩 @twy30 給予命名建議 orz 感激不盡 using System;
using System.Collections.Generic;
namespace Sandbox
{
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
string inputString = s;
int inputStringLength = inputString.Length;
if (inputStringLength == 0){ return 0; }
if (inputStringLength == 1){ return 1; }
HashSet<char> substring = new HashSet<char>();
int longestSubstringLength = 0, headIndex = 0, tailIndex = 0;
while (tailIndex < inputStringLength)
{
if (substring.Contains(inputString[tailIndex]))
{
substring.Remove(inputString[headIndex]);
++headIndex;
}
else
{
substring.Add(inputString[tailIndex]);
longestSubstringLength = Math.Max(longestSubstringLength, tailIndex- headIndex + 1);
++tailIndex;
}
}
return longestSubstringLength;
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
twy30
Jan 17, 2021
Replies: 1 comment 1 reply
-
|
你好 😊 int inputStringLength = inputString.Length;個人感覺 HashSet<char> substring = new HashSet<char>();這裡我會用 這段與命名無關;這裡的 longestSubstringLength = Math.Max(longestSubstringLength, tailIndex- headIndex + 1); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LPenny-github
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LPenny-github
你好 😊
個人感覺
inputString.Length已經很清楚,可以選擇不再另外宣告一個inputStringLength😊這裡我會用
charSet取代substring,因為 "string" 會引導讀者往「這是個string物件」的方向去想 (但它事實上是個HashSet。這段與命名無關;這裡的
tailIndex- headIndex + 1可以用substring.Count代替。