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

Commit1dbb9d2

Browse files
authored
Added tasks 61-68
1 parentccde28b commit1dbb9d2

File tree

15 files changed

+547
-0
lines changed

15 files changed

+547
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespaceLeetCodeNet.G0001_0100.S0061_rotate_list{
2+
3+
usingXunit;
4+
usingLeetCodeNet.Com_github_leetcode;
5+
6+
publicclassSolutionTest{
7+
privateListNodeBuildList(int[]vals){
8+
ListNodedummy=newListNode(0);
9+
ListNodecurr=dummy;
10+
foreach(varvinvals){
11+
curr.next=newListNode(v);
12+
curr=curr.next;
13+
}
14+
returndummy.next;
15+
}
16+
17+
privateint[]ToArray(ListNodehead){
18+
varlist=newSystem.Collections.Generic.List<int>();
19+
while(head!=null){
20+
list.Add(head.val);
21+
head=head.next;
22+
}
23+
returnlist.ToArray();
24+
}
25+
26+
[Fact]
27+
publicvoidRotateRight_Example1(){
28+
varsolution=newSolution();
29+
varhead=BuildList(newint[]{1,2,3,4,5});
30+
varresult=solution.RotateRight(head,2);
31+
Assert.Equal(newint[]{4,5,1,2,3},ToArray(result));
32+
}
33+
34+
[Fact]
35+
publicvoidRotateRight_Example2(){
36+
varsolution=newSolution();
37+
varhead=BuildList(newint[]{0,1,2});
38+
varresult=solution.RotateRight(head,4);
39+
Assert.Equal(newint[]{2,0,1},ToArray(result));
40+
}
41+
42+
[Fact]
43+
publicvoidRotateRight_Empty(){
44+
varsolution=newSolution();
45+
varresult=solution.RotateRight(null,1);
46+
Assert.Null(result);
47+
}
48+
49+
[Fact]
50+
publicvoidRotateRight_SingleNode(){
51+
varsolution=newSolution();
52+
varhead=BuildList(newint[]{1});
53+
varresult=solution.RotateRight(head,99);
54+
Assert.Equal(newint[]{1},ToArray(result));
55+
}
56+
}
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespaceLeetCodeNet.G0001_0100.S0063_unique_paths_ii{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidUniquePathsWithObstacles_Example1(){
8+
varsolution=newSolution();
9+
int[][]grid=newint[][]{
10+
newint[]{0,0,0},
11+
newint[]{0,1,0},
12+
newint[]{0,0,0}
13+
};
14+
Assert.Equal(2,solution.UniquePathsWithObstacles(grid));
15+
}
16+
17+
[Fact]
18+
publicvoidUniquePathsWithObstacles_Example2(){
19+
varsolution=newSolution();
20+
int[][]grid=newint[][]{
21+
newint[]{0,1},
22+
newint[]{0,0}
23+
};
24+
Assert.Equal(1,solution.UniquePathsWithObstacles(grid));
25+
}
26+
27+
[Fact]
28+
publicvoidUniquePathsWithObstacles_AllBlocked(){
29+
varsolution=newSolution();
30+
int[][]grid=newint[][]{
31+
newint[]{1,0},
32+
newint[]{0,0}
33+
};
34+
Assert.Equal(0,solution.UniquePathsWithObstacles(grid));
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespaceLeetCodeNet.G0001_0100.S0066_plus_one{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidPlusOne_Example1(){
8+
varsolution=newSolution();
9+
Assert.Equal(newint[]{1,2,4},solution.PlusOne(newint[]{1,2,3}));
10+
}
11+
12+
[Fact]
13+
publicvoidPlusOne_Example2(){
14+
varsolution=newSolution();
15+
Assert.Equal(newint[]{4,3,2,2},solution.PlusOne(newint[]{4,3,2,1}));
16+
}
17+
18+
[Fact]
19+
publicvoidPlusOne_Example3(){
20+
varsolution=newSolution();
21+
Assert.Equal(newint[]{1},solution.PlusOne(newint[]{0}));
22+
}
23+
24+
[Fact]
25+
publicvoidPlusOne_Example4(){
26+
varsolution=newSolution();
27+
Assert.Equal(newint[]{1,0},solution.PlusOne(newint[]{9}));
28+
}
29+
30+
[Fact]
31+
publicvoidPlusOne_AllNines(){
32+
varsolution=newSolution();
33+
Assert.Equal(newint[]{1,0,0,0},solution.PlusOne(newint[]{9,9,9}));
34+
}
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespaceLeetCodeNet.G0001_0100.S0067_add_binary{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidAddBinary_Example1(){
8+
varsolution=newSolution();
9+
Assert.Equal("100",solution.AddBinary("11","1"));
10+
}
11+
12+
[Fact]
13+
publicvoidAddBinary_Example2(){
14+
varsolution=newSolution();
15+
Assert.Equal("10101",solution.AddBinary("1010","1011"));
16+
}
17+
18+
[Fact]
19+
publicvoidAddBinary_Zero(){
20+
varsolution=newSolution();
21+
Assert.Equal("0",solution.AddBinary("0","0"));
22+
}
23+
24+
[Fact]
25+
publicvoidAddBinary_DifferentLengths(){
26+
varsolution=newSolution();
27+
Assert.Equal("1000",solution.AddBinary("1","111"));
28+
}
29+
}
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespaceLeetCodeNet.G0001_0100.S0068_text_justification{
2+
3+
usingXunit;
4+
usingSystem.Collections.Generic;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidFullJustify_Example1(){
9+
varsolution=newSolution();
10+
varwords=newstring[]{"This","is","an","example","of","text","justification."};
11+
intmaxWidth=16;
12+
varexpected=newList<string>{
13+
"This is an",
14+
"example of text",
15+
"justification. "
16+
};
17+
Assert.Equal(expected,solution.FullJustify(words,maxWidth));
18+
}
19+
20+
[Fact]
21+
publicvoidFullJustify_Example2(){
22+
varsolution=newSolution();
23+
varwords=newstring[]{"What","must","be","acknowledgment","shall","be"};
24+
intmaxWidth=16;
25+
varexpected=newList<string>{
26+
"What must be",
27+
"acknowledgment ",
28+
"shall be "
29+
};
30+
Assert.Equal(expected,solution.FullJustify(words,maxWidth));
31+
}
32+
33+
[Fact]
34+
publicvoidFullJustify_Example3(){
35+
varsolution=newSolution();
36+
varwords=newstring[]{"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"};
37+
intmaxWidth=20;
38+
varexpected=newList<string>{
39+
"Science is what we",
40+
"understand well",
41+
"enough to explain to",
42+
"a computer. Art is",
43+
"everything else we",
44+
"do "
45+
};
46+
Assert.Equal(expected,solution.FullJustify(words,maxWidth));
47+
}
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespaceLeetCodeNet.G0001_0100.S0061_rotate_list{
2+
3+
// #Medium #Two_Pointers #Linked_List #Programming_Skills_II_Day_16 #Udemy_Linked_List
4+
// #Top_Interview_150_Linked_List #2025_07_02_Time_0_ms_(100.00%)_Space_43.04_MB_(61.65%)
5+
6+
usingLeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* public int val;
12+
* public ListNode next;
13+
* public ListNode(int val=0, ListNode next=null) {
14+
* this.val = val;
15+
* this.next = next;
16+
* }
17+
* }
18+
*/
19+
publicclassSolution{
20+
publicListNodeRotateRight(ListNodehead,intk){
21+
if(head==null||head.next==null||k==0){
22+
returnhead;
23+
}
24+
// Compute the length
25+
intlen=1;
26+
ListNodetail=head;
27+
while(tail.next!=null){
28+
tail=tail.next;
29+
len++;
30+
}
31+
k=k%len;
32+
if(k==0){
33+
returnhead;
34+
}
35+
// Make it a circle
36+
tail.next=head;
37+
// Find new tail: (len - k - 1)th node
38+
ListNodenewTail=head;
39+
for(inti=0;i<len-k-1;i++){
40+
newTail=newTail.next;
41+
}
42+
ListNodenewHead=newTail.next;
43+
newTail.next=null;
44+
returnnewHead;
45+
}
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
61\. Rotate List
2+
3+
Medium
4+
5+
Given the`head` of a linked list, rotate the list to the right by`k` places.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
10+
11+
**Input:** head =[1,2,3,4,5], k = 2
12+
13+
**Output:**[4,5,1,2,3]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
18+
19+
**Input:** head =[0,1,2], k = 4
20+
21+
**Output:**[2,0,1]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range`[0, 500]`.
26+
*`-100 <= Node.val <= 100`
27+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespaceLeetCodeNet.G0001_0100.S0063_unique_paths_ii{
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
4+
// #Top_Interview_150_Multidimensional_DP #2025_07_02_Time_0_ms_(100.00%)_Space_42.12_MB_(14.91%)
5+
6+
publicclassSolution{
7+
publicintUniquePathsWithObstacles(int[][]obstacleGrid){
8+
intm=obstacleGrid.Length;
9+
intn=obstacleGrid[0].Length;
10+
int[,]dp=newint[m,n];
11+
for(inti=0;i<m;i++){
12+
for(intj=0;j<n;j++){
13+
if(obstacleGrid[i][j]==1){
14+
dp[i,j]=0;
15+
}elseif(i==0&&j==0){
16+
dp[i,j]=1;
17+
}else{
18+
dp[i,j]=(i>0?dp[i-1,j]:0)+(j>0?dp[i,j-1]:0);
19+
}
20+
}
21+
}
22+
returndp[m-1,n-1];
23+
}
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
63\. Unique Paths II
2+
3+
Medium
4+
5+
A robot is located at the top-left corner of a`m x n` grid (marked 'Start' in the diagram below).
6+
7+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
8+
9+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
10+
11+
An obstacle and space is marked as`1` and`0` respectively in the grid.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
16+
17+
**Input:** obstacleGrid =[[0,0,0],[0,1,0],[0,0,0]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
26+
27+
**Input:** obstacleGrid =[[0,1],[0,0]]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
*`m == obstacleGrid.length`
34+
*`n == obstacleGrid[i].length`
35+
*`1 <= m, n <= 100`
36+
*`obstacleGrid[i][j]` is`0` or`1`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespaceLeetCodeNet.G0001_0100.S0066_plus_one{
2+
3+
// #Easy #Top_Interview_Questions #Array #Math #Programming_Skills_II_Day_3 #Udemy_Arrays
4+
// #Top_Interview_150_Math #2025_07_02_Time_0_ms_(100.00%)_Space_46.64_MB_(51.35%)
5+
6+
publicclassSolution{
7+
publicint[]PlusOne(int[]digits){
8+
for(inti=digits.Length-1;i>=0;i--){
9+
if(digits[i]<9){
10+
digits[i]++;
11+
returndigits;
12+
}
13+
digits[i]=0;
14+
}
15+
int[]res=newint[digits.Length+1];
16+
res[0]=1;
17+
returnres;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp