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

Commitc35f8f3

Browse files
authored
Added tasks 3-10
1 parent4b74a2a commitc35f8f3

File tree

24 files changed

+852
-0
lines changed

24 files changed

+852
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespaceLeetCodeNet.G0001_0100.S0003_longest_substring_without_repeating_characters{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidLengthOfLongestSubstring(){
9+
Assert.Equal(3,newSolution().LengthOfLongestSubstring("abcabcbb"));
10+
}
11+
12+
[Fact]
13+
publicvoidLengthOfLongestSubstring2(){
14+
Assert.Equal(1,newSolution().LengthOfLongestSubstring("bbbbb"));
15+
}
16+
17+
[Fact]
18+
publicvoidLengthOfLongestSubstring3(){
19+
Assert.Equal(3,newSolution().LengthOfLongestSubstring("pwwkew"));
20+
}
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespaceLeetCodeNet.G0001_0100.S0004_median_of_two_sorted_arrays{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidFindMedianSortedArrays(){
9+
Assert.Equal(2.0,newSolution().FindMedianSortedArrays(newint[]{1,3},newint[]{2}));
10+
}
11+
12+
[Fact]
13+
publicvoidFindMedianSortedArrays2(){
14+
Assert.Equal(2.5,newSolution().FindMedianSortedArrays(newint[]{1,2},newint[]{3,4}));
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespaceLeetCodeNet.G0001_0100.S0005_longest_palindromic_substring{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidLongestPalindrome(){
9+
Assert.Equal("bab",newSolution().LongestPalindrome("babad"));
10+
}
11+
12+
[Fact]
13+
publicvoidLongestPalindrome2(){
14+
Assert.Equal("bb",newSolution().LongestPalindrome("cbbd"));
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespaceLeetCodeNet.G0001_0100.S0006_zigzag_conversion{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidConvert(){
9+
Assert.Equal("PAHNAPLSIIGYIR",newSolution().Convert("PAYPALISHIRING",3));
10+
}
11+
12+
[Fact]
13+
publicvoidConvert2(){
14+
Assert.Equal("PINALSIGYAHRPI",newSolution().Convert("PAYPALISHIRING",4));
15+
}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespaceLeetCodeNet.G0001_0100.S0007_reverse_integer{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidReverse(){
9+
Assert.Equal(321,newSolution().Reverse(123));
10+
}
11+
12+
[Fact]
13+
publicvoidReverse2(){
14+
Assert.Equal(-321,newSolution().Reverse(-123));
15+
}
16+
17+
[Fact]
18+
publicvoidReverse3(){
19+
Assert.Equal(21,newSolution().Reverse(120));
20+
}
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespaceLeetCodeNet.G0001_0100.S0008_string_to_integer_atoi{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidMyAtoi(){
9+
Assert.Equal(42,newSolution().MyAtoi("42"));
10+
}
11+
12+
[Fact]
13+
publicvoidMyAtoi2(){
14+
Assert.Equal(-42,newSolution().MyAtoi(" -42"));
15+
}
16+
17+
[Fact]
18+
publicvoidMyAtoi3(){
19+
Assert.Equal(4193,newSolution().MyAtoi("4193 with words"));
20+
}
21+
22+
[Fact]
23+
publicvoidMyAtoi4(){
24+
Assert.Equal(0,newSolution().MyAtoi("words and 987"));
25+
}
26+
27+
[Fact]
28+
publicvoidMyAtoi5(){
29+
Assert.Equal(-2147483648,newSolution().MyAtoi("-91283472332"));
30+
}
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespaceLeetCodeNet.G0001_0100.S0009_palindrome_number{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidIsPalindrome(){
9+
Assert.Equal(true,newSolution().IsPalindrome(121));
10+
}
11+
12+
[Fact]
13+
publicvoidIsPalindrome2(){
14+
Assert.Equal(false,newSolution().IsPalindrome(-121));
15+
}
16+
17+
[Fact]
18+
publicvoidIsPalindrome3(){
19+
Assert.Equal(false,newSolution().IsPalindrome(10));
20+
}
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespaceLeetCodeNet.G0001_0100.S0010_regular_expression_matching{
2+
3+
usingXunit;
4+
usingSystem;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidIsMatch(){
9+
Assert.Equal(false,newSolution().IsMatch("aa","a"));
10+
}
11+
12+
[Fact]
13+
publicvoidIsMatch2(){
14+
Assert.Equal(true,newSolution().IsMatch("aa","a*"));
15+
}
16+
17+
[Fact]
18+
publicvoidIsMatch3(){
19+
Assert.Equal(true,newSolution().IsMatch("ab",".*"));
20+
}
21+
22+
[Fact]
23+
publicvoidIsMatch4(){
24+
Assert.Equal(true,newSolution().IsMatch("aab","c*a*b"));
25+
}
26+
27+
[Fact]
28+
publicvoidIsMatch5(){
29+
Assert.Equal(false,newSolution().IsMatch("mississippi","mis*is*p*."));
30+
}
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespaceLeetCodeNet.G0001_0100.S0003_longest_substring_without_repeating_characters{
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
4+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
5+
// #Big_O_Time_O(n)_Space_O(1) #2023_12_22_Time_50_ms_(98.40%)_Space_41.9_MB_(30.26%)
6+
7+
publicclassSolution{
8+
publicintLengthOfLongestSubstring(strings){
9+
int[]lastIndices=newint[256];
10+
for(inti=0;i<256;i++){
11+
lastIndices[i]=-1;
12+
}
13+
intmaxLen=0;
14+
intcurLen=0;
15+
intstart=0;
16+
for(inti=0;i<s.Length;i++){
17+
charcur=s[i];
18+
if(lastIndices[cur]<start){
19+
lastIndices[cur]=i;
20+
curLen++;
21+
}else{
22+
intlastIndex=lastIndices[cur];
23+
start=lastIndex+1;
24+
curLen=i-start+1;
25+
lastIndices[cur]=i;
26+
}
27+
if(curLen>maxLen){
28+
maxLen=curLen;
29+
}
30+
}
31+
returnmaxLen;
32+
}
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string`s`, find the length of the**longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
*`s` consists of English letters, digits, symbols and spaces.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespaceLeetCodeNet.G0001_0100.S0004_median_of_two_sorted_arrays{
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2023_12_22_Time_83_ms_(96.35%)_Space_54_MB_(6.56%)
5+
6+
publicclassSolution{
7+
publicdoubleFindMedianSortedArrays(int[]nums1,int[]nums2){
8+
if(nums2.Length<nums1.Length){
9+
returnFindMedianSortedArrays(nums2,nums1);
10+
}
11+
intcut1;
12+
intcut2;
13+
intn1=nums1.Length;
14+
intn2=nums2.Length;
15+
intlow=0;
16+
inthigh=n1;
17+
while(low<=high){
18+
cut1=(low+high)/2;
19+
cut2=((n1+n2+1)/2)-cut1;
20+
intl1=cut1==0?int.MinValue:nums1[cut1-1];
21+
intl2=cut2==0?int.MinValue:nums2[cut2-1];
22+
intr1=cut1==n1?int.MaxValue:nums1[cut1];
23+
intr2=cut2==n2?int.MaxValue:nums2[cut2];
24+
if(l1<=r2&&l2<=r1){
25+
if((n1+n2)%2==0){
26+
return(Math.Max(l1,l2)+Math.Min(r1,r2))/2.0;
27+
}
28+
returnMath.Max(l1,l2);
29+
}elseif(l1>r2){
30+
high=cut1-1;
31+
}else{
32+
low=cut1+1;
33+
}
34+
}
35+
return0.0;
36+
}
37+
}
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays`nums1` and`nums2` of size`m` and`n` respectively, return**the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be`O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 =[1,3], nums2 =[2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array =[1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 =[1,2], nums2 =[3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array =[1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 =[0,0], nums2 =[0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 =[], nums2 =[1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 =[2], nums2 =[]
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
*`nums1.length == m`
46+
*`nums2.length == n`
47+
*`0 <= m <= 1000`
48+
*`0 <= n <= 1000`
49+
*`1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespaceLeetCodeNet.G0001_0100.S0005_longest_palindromic_substring{
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
6+
// #2023_12_22_Time_61_ms_(99.50%)_Space_40_MB_(56.86%)
7+
8+
publicclassSolution{
9+
publicstringLongestPalindrome(strings){
10+
if(s.Length==1)returns;
11+
intres=0;
12+
intl=0;
13+
intr=0;
14+
intlen=s.Length;
15+
for(inti=0;i<len;i++){
16+
for(intdiff=1;i-diff+1>=0&&i+diff<len;diff++){
17+
if(s[i-diff+1]!=s[i+diff])break;
18+
elseif(res<diff*2){
19+
res=diff*2;
20+
l=i-diff+1;
21+
r=i+diff;
22+
}
23+
}
24+
}
25+
for(inti=0;i<len;i++){
26+
for(intdiff=1;i-diff>=0&&i+diff<len;diff++){
27+
if(s[i-diff]!=s[i+diff])break;
28+
elseif(res<diff*2+1){
29+
res=diff*2+1;
30+
l=i-diff;
31+
r=i+diff;
32+
}
33+
}
34+
}
35+
returns.Substring(l,r-l+1);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp