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

Commitf3fad08

Browse files
authored
Added tasks 239-394
1 parentc178a7b commitf3fad08

File tree

31 files changed

+937
-0
lines changed

31 files changed

+937
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespaceLeetCodeNet.G0201_0300.S0239_sliding_window_maximum{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidMaxSlidingWindow(){
8+
Assert.Equal(newint[]{3,3,5,5,6,7},
9+
newSolution().MaxSlidingWindow(newint[]{1,3,-1,-3,5,3,6,7},3));
10+
}
11+
12+
[Fact]
13+
publicvoidMaxSlidingWindow2(){
14+
Assert.Equal(newint[]{1},newSolution().MaxSlidingWindow(newint[]{1},1));
15+
}
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespaceLeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidSearchMatrix(){
8+
int[][]matrix={
9+
newint[]{1,4,7,11,15},
10+
newint[]{2,5,8,12,19},
11+
newint[]{3,6,9,16,22},
12+
newint[]{10,13,14,17,24},
13+
newint[]{18,21,23,26,30}
14+
};
15+
Assert.Equal(true,newSolution().SearchMatrix(matrix,5));
16+
}
17+
18+
[Fact]
19+
publicvoidSearchMatrix2(){
20+
int[][]matrix={
21+
newint[]{1,4,7,11,15},
22+
newint[]{2,5,8,12,19},
23+
newint[]{3,6,9,16,22},
24+
newint[]{10,13,14,17,24},
25+
newint[]{18,21,23,26,30}
26+
};
27+
Assert.Equal(false,newSolution().SearchMatrix(matrix,20));
28+
}
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespaceLeetCodeNet.G0201_0300.S0283_move_zeroes{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidMoveZeroes(){
8+
int[]array={0,1,0,3,12};
9+
newSolution().MoveZeroes(array);
10+
Assert.Equal(newint[]{1,3,12,0,0},array);
11+
}
12+
13+
[Fact]
14+
publicvoidMoveZeroes2(){
15+
int[]array={0};
16+
newSolution().MoveZeroes(array);
17+
Assert.Equal(newint[]{0},array);
18+
}
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespaceLeetCodeNet.G0201_0300.S0287_find_the_duplicate_number{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidFindDuplicate(){
8+
Assert.Equal(2,newSolution().FindDuplicate(newint[]{1,3,4,2,2}));
9+
}
10+
11+
[Fact]
12+
publicvoidFindDuplicate2(){
13+
Assert.Equal(3,newSolution().FindDuplicate(newint[]{3,1,3,4,2}));
14+
}
15+
16+
[Fact]
17+
publicvoidFindDuplicate3(){
18+
Assert.Equal(1,newSolution().FindDuplicate(newint[]{1,1}));
19+
}
20+
21+
[Fact]
22+
publicvoidFindDuplicate4(){
23+
Assert.Equal(1,newSolution().FindDuplicate(newint[]{1,1,2}));
24+
}
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespaceLeetCodeNet.G0201_0300.S0295_find_median_from_data_stream{
2+
3+
usingXunit;
4+
5+
publicclassMedianFinderTest{
6+
[Fact]
7+
publicvoidMedianFinder(){
8+
MedianFindermedianFinder=newMedianFinder();
9+
// arr = [1]
10+
medianFinder.AddNum(1);
11+
// arr = [1, 2]
12+
medianFinder.AddNum(2);
13+
// return 1.5 (i.e., (1 + 2) / 2)
14+
Assert.Equal(1.5,medianFinder.FindMedian());
15+
// arr[1, 2, 3]
16+
medianFinder.AddNum(3);
17+
// return 2.0
18+
Assert.Equal(2.0,medianFinder.FindMedian());
19+
}
20+
21+
[Fact]
22+
publicvoidMedianFinder2(){
23+
MedianFindermedianFinder=newMedianFinder();
24+
medianFinder.AddNum(1);
25+
medianFinder.AddNum(3);
26+
medianFinder.AddNum(-1);
27+
Assert.Equal(1.0,medianFinder.FindMedian());
28+
}
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespaceLeetCodeNet.G0201_0300.S0300_longest_increasing_subsequence{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidLengthOfLIS(){
8+
Assert.Equal(4,newSolution().LengthOfLIS(newint[]{10,9,2,5,3,7,101,18}));
9+
}
10+
11+
[Fact]
12+
publicvoidLengthOfLIS2(){
13+
Assert.Equal(4,newSolution().LengthOfLIS(newint[]{0,1,0,3,2,3}));
14+
}
15+
16+
[Fact]
17+
publicvoidLengthOfLIS3(){
18+
Assert.Equal(1,newSolution().LengthOfLIS(newint[]{7,7,7,7,7,7,7}));
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespaceLeetCodeNet.G0301_0400.S0322_coin_change{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidCoinChange(){
8+
Assert.Equal(3,newSolution().CoinChange(newint[]{1,2,5},11));
9+
}
10+
11+
[Fact]
12+
publicvoidCoinChange2(){
13+
Assert.Equal(-1,newSolution().CoinChange(newint[]{2},3));
14+
}
15+
16+
[Fact]
17+
publicvoidCoinChange3(){
18+
Assert.Equal(0,newSolution().CoinChange(newint[]{1},0));
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespaceLeetCodeNet.G0301_0400.S0338_counting_bits{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidCountBits(){
8+
Assert.Equal(newint[]{0,1,1},newSolution().CountBits(2));
9+
}
10+
11+
[Fact]
12+
publicvoidCountBits2(){
13+
Assert.Equal(newint[]{0,1,1,2,1,2},newSolution().CountBits(5));
14+
}
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespaceLeetCodeNet.G0301_0400.S0347_top_k_frequent_elements{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidTopKFrequent(){
8+
Assert.Equal(newint[]{1,2},
9+
newSolution().TopKFrequent(newint[]{1,1,1,2,2,3},2));
10+
}
11+
12+
[Fact]
13+
publicvoidTopKFrequent2(){
14+
Assert.Equal(newint[]{1},newSolution().TopKFrequent(newint[]{1},1));
15+
}
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespaceLeetCodeNet.G0301_0400.S0394_decode_string{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidDecodeString(){
8+
Assert.Equal("aaabcbc",newSolution().DecodeString("3[a]2[bc]"));
9+
}
10+
11+
[Fact]
12+
publicvoidDecodeString2(){
13+
Assert.Equal("accaccacc",newSolution().DecodeString("3[a2[c]]"));
14+
}
15+
16+
[Fact]
17+
publicvoidDecodeString3(){
18+
Assert.Equal("abcabccdcdcdef",newSolution().DecodeString("2[abc]3[cd]ef"));
19+
}
20+
21+
[Fact]
22+
publicvoidDecodeString4(){
23+
Assert.Equal("abccdcdcdxyz",newSolution().DecodeString("abc3[cd]xyz"));
24+
}
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespaceLeetCodeNet.G0201_0300.S0239_sliding_window_maximum{
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
4+
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
5+
// #2024_01_07_Time_493_ms_(46.05%)_Space_133.5_MB_(14.15%)
6+
7+
usingSystem;
8+
usingSystem.Collections.Generic;
9+
10+
publicclassSolution{
11+
publicint[]MaxSlidingWindow(int[]nums,intk){
12+
intn=nums.Length;
13+
int[]res=newint[n-k+1];
14+
intx=0;
15+
LinkedList<int>dq=newLinkedList<int>();
16+
inti=0;
17+
intj=0;
18+
while(j<nums.Length){
19+
while(dq.Count!=0&&dq.Last.Value<nums[j]){
20+
dq.RemoveLast();
21+
}
22+
dq.AddLast(nums[j]);
23+
if(j-i+1==k){
24+
res[x]=dq.First.Value;
25+
++x;
26+
if(dq.First.Value==nums[i]){
27+
dq.RemoveFirst();
28+
}
29+
++i;
30+
}
31+
++j;
32+
}
33+
returnres;
34+
}
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
239\. Sliding Window Maximum
2+
3+
Hard
4+
5+
You are given an array of integers`nums`, there is a sliding window of size`k` which is moving from the very left of the array to the very right. You can only see the`k` numbers in the window. Each time the sliding window moves right by one position.
6+
7+
Return_the max sliding window_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums =[1,3,-1,-3,5,3,6,7], k = 3
12+
13+
**Output:**[3,3,5,5,6,7]
14+
15+
**Explanation:**
16+
17+
Window position Max
18+
--------------- -----
19+
[1 3 -1] -3 5 3 6 7 3
20+
1 [3 -1 -3] 5 3 6 7 3
21+
1 3 [-1 -3 5] 3 6 7 5
22+
1 3 -1 [-3 5 3] 6 7 5
23+
1 3 -1 -3 [5 3 6] 7 6
24+
1 3 -1 -3 5 [3 6 7] 7
25+
26+
**Example 2:**
27+
28+
**Input:** nums =[1], k = 1
29+
30+
**Output:**[1]
31+
32+
**Example 3:**
33+
34+
**Input:** nums =[1,-1], k = 1
35+
36+
**Output:**[1,-1]
37+
38+
**Example 4:**
39+
40+
**Input:** nums =[9,11], k = 2
41+
42+
**Output:**[11]
43+
44+
**Example 5:**
45+
46+
**Input:** nums =[4,-2], k = 2
47+
48+
**Output:**[4]
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
54+
*`1 <= k <= nums.length`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespaceLeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii{
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix
4+
// #Divide_and_Conquer #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8
5+
// #Big_O_Time_O(n+m)_Space_O(1) #2024_01_07_Time_142_ms_(60.76%)_Space_54_MB_(79.75%)
6+
7+
publicclassSolution{
8+
publicboolSearchMatrix(int[][]matrix,inttarget){
9+
intr=0;
10+
intc=matrix[0].Length-1;
11+
while(r<matrix.Length&&c>=0){
12+
if(matrix[r][c]==target){
13+
returntrue;
14+
}elseif(matrix[r][c]>target){
15+
c--;
16+
}else{
17+
r++;
18+
}
19+
}
20+
returnfalse;
21+
}
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
240\. Search a 2D Matrix II
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a`target` value in an`m x n` integer`matrix`. The`matrix` has the following properties:
6+
7+
* Integers in each row are sorted in ascending from left to right.
8+
* Integers in each column are sorted in ascending from top to bottom.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)
13+
14+
**Input:** matrix =[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)
21+
22+
**Input:** matrix =[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
*`m == matrix.length`
29+
*`n == matrix[i].length`
30+
*`1 <= n, m <= 300`
31+
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
32+
* All the integers in each row are**sorted** in ascending order.
33+
* All the integers in each column are**sorted** in ascending order.
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp