Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit131fc44

Browse files
authored
Merge pull requestneetcode-gh#3758 from NotADucc/0003
Additional solution csharp/0003-longest-substring-without-repeating-characters.cs
2 parents8d01381 +ad912fb commit131fc44

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
1-
publicclassSolution{
2-
publicintLengthOfLongestSubstring(strings){
1+
// hashset
2+
publicclassSolution
3+
{
4+
publicintLengthOfLongestSubstring(strings)
5+
{
36
intleftPointer=0,rightPointer=0,maxLength=0;
47
HashSet<int>chars=newHashSet<int>();
58

6-
while(rightPointer<s.Length){
9+
while(rightPointer<s.Length)
10+
{
711
charcurrChar=s[rightPointer];
8-
if(chars.Contains(currChar)){// Move left pointer until all duplicate chars removed
12+
if(chars.Contains(currChar))
13+
{
14+
// Move left pointer until all duplicate chars removed
915
chars.Remove(s[leftPointer]);
1016
leftPointer++;
11-
}else{
17+
}
18+
else
19+
{
1220
chars.Add(currChar);
1321
maxLength=Math.Max(maxLength,rightPointer-leftPointer+1);
1422
rightPointer++;
1523
}
1624
}
1725
returnmaxLength;
1826
}
27+
}
28+
29+
//bitmask
30+
publicclassSolution
31+
{
32+
privateInt128ONE=1;
33+
publicintLengthOfLongestSubstring(strings)
34+
{
35+
intConvert(charch)=>ch-' ';
36+
Int128mask=0;
37+
intl=0,r=0,output=0;
38+
while(r<s.Length)
39+
{
40+
Int128temp=mask^(ONE<<Convert(s[r]));
41+
if(temp<mask)
42+
{
43+
while(s[l]!=s[r])
44+
{
45+
mask^=ONE<<Convert(s[l]);
46+
l++;
47+
}
48+
mask^=ONE<<Convert(s[l]);
49+
l++;
50+
}
51+
else
52+
{
53+
mask=temp;
54+
output=Math.Max(output,r-l+1);
55+
r++;
56+
}
57+
}
58+
59+
returnoutput;
60+
}
1961
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp