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

Commit8887b82

Browse files
authored
Added tasks 283-383
1 parenta2a6d20 commit8887b82

File tree

15 files changed

+487
-0
lines changed

15 files changed

+487
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespaceLeetCodeNet.G0201_0300.S0289_game_of_life{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidGameOfLife1(){
8+
int[][]board=newint[][]{
9+
newint[]{0,1,0},
10+
newint[]{0,0,1},
11+
newint[]{1,1,1},
12+
newint[]{0,0,0}
13+
};
14+
newSolution().GameOfLife(board);
15+
int[][]expected=newint[][]{
16+
newint[]{0,0,0},
17+
newint[]{1,0,1},
18+
newint[]{0,1,1},
19+
newint[]{0,1,0}
20+
};
21+
Assert.Equal(expected,board);
22+
}
23+
24+
[Fact]
25+
publicvoidGameOfLife2(){
26+
int[][]board=newint[][]{
27+
newint[]{1,1},
28+
newint[]{1,0}
29+
};
30+
newSolution().GameOfLife(board);
31+
int[][]expected=newint[][]{
32+
newint[]{1,1},
33+
newint[]{1,1}
34+
};
35+
Assert.Equal(expected,board);
36+
}
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespaceLeetCodeNet.G0201_0300.S0290_word_pattern{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidWordPattern1(){
8+
Assert.True(newSolution().WordPattern("abba","dog cat cat dog"));
9+
}
10+
[Fact]
11+
publicvoidWordPattern2(){
12+
Assert.False(newSolution().WordPattern("abba","dog cat cat fish"));
13+
}
14+
[Fact]
15+
publicvoidWordPattern3(){
16+
Assert.False(newSolution().WordPattern("aaaa","dog cat cat dog"));
17+
}
18+
[Fact]
19+
publicvoidWordPattern4(){
20+
Assert.False(newSolution().WordPattern("abba","dog dog dog dog"));
21+
}
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespaceLeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums{
2+
3+
usingSystem;
4+
usingXunit;
5+
usingSystem.Collections.Generic;
6+
7+
publicclassSolutionTest{
8+
[Fact]
9+
publicvoidKSmallestPairs1(){
10+
varresult=newSolution().KSmallestPairs(newint[]{1,7,11},newint[]{2,4,6},3);
11+
varexpected=newList<IList<int>>{
12+
newList<int>{1,2},
13+
newList<int>{1,4},
14+
newList<int>{1,6}
15+
};
16+
Assert.Equal(expected,result);
17+
}
18+
[Fact]
19+
publicvoidKSmallestPairs2(){
20+
varresult=newSolution().KSmallestPairs(newint[]{1,1,2},newint[]{1,2,3},2);
21+
varexpected=newList<IList<int>>{
22+
newList<int>{1,1},
23+
newList<int>{1,1}
24+
};
25+
Assert.Equal(expected,result);
26+
}
27+
[Fact]
28+
publicvoidKSmallestPairs3(){
29+
varresult=newSolution().KSmallestPairs(newint[]{1,2},newint[]{3},3);
30+
varexpected=newList<IList<int>>{
31+
newList<int>{1,3},
32+
newList<int>{2,3}
33+
};
34+
Assert.Equal(expected,result);
35+
}
36+
}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespaceLeetCodeNet.G0301_0400.S0380_insert_delete_getrandom_o1{
2+
3+
usingSystem;
4+
usingSystem.Collections.Generic;
5+
usingXunit;
6+
7+
publicclassRandomizedSetTest{
8+
[Fact]
9+
publicvoidRandomizedSetBasic(){
10+
varresult=newList<string>();
11+
RandomizedSetrandomizedSet=null;
12+
result.Add("null");
13+
randomizedSet=newRandomizedSet();
14+
result.Add(randomizedSet.Insert(1).ToString().ToLower());
15+
result.Add(randomizedSet.Remove(2).ToString().ToLower());
16+
result.Add(randomizedSet.Insert(2).ToString().ToLower());
17+
intrandom=randomizedSet.GetRandom();
18+
result.Add(random.ToString());
19+
result.Add(randomizedSet.Remove(1).ToString().ToLower());
20+
result.Add(randomizedSet.Insert(2).ToString().ToLower());
21+
result.Add(randomizedSet.GetRandom().ToString());
22+
varexpected1=newList<string>{"null","true","false","true","1","true","false","2"};
23+
varexpected2=newList<string>{"null","true","false","true","2","true","false","2"};
24+
if(random==1){
25+
Assert.Equal(expected1,result);
26+
}else{
27+
Assert.Equal(expected2,result);
28+
}
29+
}
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespaceLeetCodeNet.G0301_0400.S0383_ransom_note{
2+
3+
usingSystem;
4+
usingXunit;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidCanConstruct1(){
9+
Assert.False(newSolution().CanConstruct("a","b"));
10+
}
11+
[Fact]
12+
publicvoidCanConstruct2(){
13+
Assert.False(newSolution().CanConstruct("aa","ab"));
14+
}
15+
[Fact]
16+
publicvoidCanConstruct3(){
17+
Assert.True(newSolution().CanConstruct("aa","aab"));
18+
}
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespaceLeetCodeNet.G0201_0300.S0289_game_of_life{
2+
3+
// #Medium #Array #Matrix #Simulation #Top_Interview_150_Matrix
4+
// #2025_07_16_Time_0_ms_(100.00%)_Space_46.31_MB_(66.67%)
5+
6+
publicclassSolution{
7+
publicvoidGameOfLife(int[][]board){
8+
intm=board.Length,n=board[0].Length;
9+
int[]dx={0,0,1,1,1,-1,-1,-1};
10+
int[]dy={1,-1,0,1,-1,0,1,-1};
11+
for(inti=0;i<m;i++){
12+
for(intj=0;j<n;j++){
13+
intlive=0;
14+
for(intd=0;d<8;d++){
15+
intni=i+dx[d],nj=j+dy[d];
16+
if(ni>=0&&ni<m&&nj>=0&&nj<n&&(board[ni][nj]&1)==1)live++;
17+
}
18+
if((board[i][j]&1)==1){
19+
if(live==2||live==3)board[i][j]|=2;
20+
}else{
21+
if(live==3)board[i][j]|=2;
22+
}
23+
}
24+
}
25+
for(inti=0;i<m;i++){
26+
for(intj=0;j<n;j++){
27+
board[i][j]>>=1;
28+
}
29+
}
30+
}
31+
}
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
289\. Game of Life
2+
3+
Medium
4+
5+
According to[Wikipedia's article](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life): "The**Game of Life**, also known simply as**Life**, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
6+
7+
The board is made up of an`m x n` grid of cells, where each cell has an initial state:**live** (represented by a`1`) or**dead** (represented by a`0`). Each cell interacts with its[eight neighbors](https://en.wikipedia.org/wiki/Moore_neighborhood) (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
8+
9+
1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
10+
2. Any live cell with two or three live neighbors lives on to the next generation.
11+
3. Any live cell with more than three live neighbors dies, as if by over-population.
12+
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
13+
14+
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the`m x n` grid`board`, return_the next state_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg)
19+
20+
**Input:** board =[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
21+
22+
**Output:**[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg)
27+
28+
**Input:** board =[[1,1],[1,0]]
29+
30+
**Output:**[[1,1],[1,1]]
31+
32+
**Constraints:**
33+
34+
*`m == board.length`
35+
*`n == board[i].length`
36+
*`1 <= m, n <= 25`
37+
*`board[i][j]` is`0` or`1`.
38+
39+
**Follow up:**
40+
41+
* Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
42+
* In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespaceLeetCodeNet.G0201_0300.S0290_word_pattern{
2+
3+
// #Easy #String #Hash_Table #Data_Structure_II_Day_7_String #Top_Interview_150_Hashmap
4+
// #2025_07_16_Time_0_ms_(100.00%)_Space_40.84_MB_(71.08%)
5+
6+
publicclassSolution{
7+
publicboolWordPattern(stringpattern,strings){
8+
Dictionary<char,string>dict=new();
9+
string[]str=s.Split(" ");
10+
if(pattern.Length!=str.Length){
11+
returnfalse;
12+
}
13+
for(inti=0;i<pattern.Length;i++){
14+
if(!dict.ContainsKey(pattern[i])&&!dict.ContainsValue(str[i])){
15+
dict.Add(pattern[i],str[i]);
16+
}
17+
if(!dict.ContainsKey(pattern[i])||!str[i].Equals(dict[pattern[i]])){
18+
returnfalse;
19+
}
20+
}
21+
returntrue;
22+
}
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
290\. Word Pattern
2+
3+
Easy
4+
5+
Given a`pattern` and a string`s`, find if`s` follows the same pattern.
6+
7+
Here**follow** means a full match, such that there is a bijection between a letter in`pattern` and a**non-empty** word in`s`.
8+
9+
**Example 1:**
10+
11+
**Input:** pattern = "abba", s = "dog cat cat dog"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** pattern = "abba", s = "dog cat cat fish"
18+
19+
**Output:** false
20+
21+
**Example 3:**
22+
23+
**Input:** pattern = "aaaa", s = "dog cat cat dog"
24+
25+
**Output:** false
26+
27+
**Example 4:**
28+
29+
**Input:** pattern = "abba", s = "dog dog dog dog"
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
*`1 <= pattern.length <= 300`
36+
*`pattern` contains only lower-case English letters.
37+
*`1 <= s.length <= 3000`
38+
*`s` contains only lower-case English letters and spaces`' '`.
39+
*`s`**does not contain** any leading or trailing spaces.
40+
* All the words in`s` are separated by a**single space**.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespaceLeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums{
2+
3+
// #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
4+
// #2025_07_16_Time_52_ms_(73.33%)_Space_92.81_MB_(30.00%)
5+
6+
usingSystem.Collections.Generic;
7+
8+
publicclassSolution{
9+
publicIList<IList<int>>KSmallestPairs(int[]nums1,int[]nums2,intk){
10+
varresult=newList<IList<int>>();
11+
if(nums1.Length==0||nums2.Length==0||k==0){
12+
returnresult;
13+
}
14+
varpq=newPriorityQueue<(inti,intj),int>();
15+
// Initialize with pairs from nums1[0] with each element in nums2 up to `k`
16+
for(intj=0;j<nums2.Length&&j<k;j++){
17+
pq.Enqueue((0,j),nums1[0]+nums2[j]);
18+
}
19+
while(k-->0&&pq.Count>0){
20+
var(i,j)=pq.Dequeue();
21+
result.Add(newList<int>{nums1[i],nums2[j]});
22+
// If there's another pair with the next element in nums1, add it
23+
if(i+1<nums1.Length){
24+
pq.Enqueue((i+1,j),nums1[i+1]+nums2[j]);
25+
}
26+
}
27+
returnresult;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp