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

Commit337907b

Browse files
committed
Modified 4 solutions
1 parentda4655d commit337907b

File tree

4 files changed

+105
-116
lines changed

4 files changed

+105
-116
lines changed

‎Easy/Remove Duplicates From Sorted Lists.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@
77
* }
88
*/
99
classSolution {
10-
publicListNodedeleteDuplicates(ListNodehead) {
11-
if (head ==null)returnhead;
12-
ListNodecopyHead =head;
13-
while (head.next !=null) {
14-
if (head.val ==head.next.val) {
15-
if(head.next.next ==null) {
16-
head.next =null;
17-
break;
18-
}
19-
else {
20-
head.next =head.next.next;
21-
}
22-
}
23-
else {
24-
head =head.next;
25-
}
26-
}
27-
returncopyHead;
10+
publicListNodedeleteDuplicates(ListNodehead) {
11+
ListNodecurr =head;
12+
while (curr !=null) {
13+
if (curr.next !=null &&curr.val ==curr.next.val) {
14+
curr.next =curr.next.next;
15+
}
16+
else {
17+
curr =curr.next;
18+
}
2819
}
20+
returnhead;
21+
}
2922
}

‎Medium/Remove Duplicates from Sorted List II.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@
77
* }
88
*/
99
classSolution {
10-
publicListNodedeleteDuplicates(ListNodehead) {
11-
if(head==null)returnnull;
12-
13-
ListNodeFakeHead=newListNode(0);
14-
FakeHead.next=head;
15-
ListNodepre=FakeHead;
16-
ListNodecur=head;
17-
18-
while(cur!=null){
19-
while(cur.next!=null&&cur.val==cur.next.val){
20-
cur=cur.next;
21-
}
22-
if(pre.next==cur){
23-
pre=pre.next;
24-
}
25-
else{
26-
pre.next=cur.next;
27-
}
28-
cur=cur.next;
29-
}
30-
returnFakeHead.next;
10+
publicListNodedeleteDuplicates(ListNodehead) {
11+
ListNodedummy =newListNode(0);
12+
dummy.next =head;
13+
ListNodeprev =dummy;
14+
ListNodecurr =head;
15+
while (curr !=null) {
16+
intval =curr.val;
17+
intcount =0;
18+
ListNodetemp =curr;
19+
// Track the number of occurrences of value of current node
20+
while (temp !=null &&temp.val ==val) {
21+
temp =temp.next;
22+
count++;
23+
}
24+
// Update prev.next to the temp which can be a null or node with different value
25+
if (count >1) {
26+
prev.next =temp;
27+
curr =temp;
28+
}
29+
else {
30+
prev =curr;
31+
curr =curr.next;
32+
}
3133
}
34+
returndummy.next;
35+
}
3236
}

‎Medium/Set Matrix Zeroes.java

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,57 @@
11
classSolution {
2-
publicvoidsetZeroes(int[][]arr) {
3-
if (arr.length ==0 ||arr[0].length ==0) {
4-
return;
5-
}
6-
7-
8-
intnumOfRows =arr.length;
9-
intnumOfCols =arr[0].length;
10-
booleanfirstRowZero =false;
11-
booleanfirstColZero =false;
12-
13-
for (inti =0;i <numOfCols;i++) {
14-
if (arr[0][i] ==0) {
15-
firstRowZero =true;
16-
break;
17-
}
18-
}
19-
20-
for (inti =0;i <numOfRows;i++) {
21-
if (arr[i][0] ==0) {
22-
firstColZero =true;
23-
break;
24-
}
25-
}
26-
27-
for (inti =1;i <numOfRows;i++) {
28-
for (intj =0;j <numOfCols;j++) {
29-
if (arr[i][j] ==0) {
30-
arr[0][j] =0;
31-
arr[i][0] =0;
32-
}
33-
}
34-
}
35-
36-
for (inti =1;i <numOfRows;i++) {
37-
if (arr[i][0] ==0) {
38-
for (intj =1;j <numOfCols;j++) {
39-
arr[i][j] =0;
40-
}
41-
}
42-
}
43-
44-
for (inti =0;i <numOfCols;i++) {
45-
if (arr[0][i] ==0) {
46-
for (intj =1;j <numOfRows;j++) {
47-
arr[j][i] =0;
48-
}
49-
}
50-
}
51-
52-
if (firstRowZero) {
53-
for (inti =0;i <numOfCols;i++) {
54-
arr[0][i] =0;
55-
}
2+
publicvoidsetZeroes(int[][]matrix) {
3+
booleanfirstRowZero =false;
4+
booleanfirstColZero =false;
5+
intnumRows =matrix.length;
6+
intnumCols =matrix[0].length;
7+
// Mark if first column needs to be set zero
8+
for (inti =0;i <numRows;i++) {
9+
if (matrix[i][0] ==0) {
10+
firstColZero =true;
11+
}
12+
}
13+
// Mark if first row needs to be set zero
14+
for (inti =0;i <numCols;i++) {
15+
if (matrix[0][i] ==0) {
16+
firstRowZero =true;
17+
}
18+
}
19+
// If a value in matrix is zero set the value at first row and column to be zero
20+
for (inti =1;i <numRows;i++) {
21+
for (intj =1;j <numCols;j++) {
22+
if (matrix[i][j] ==0) {
23+
matrix[0][j] =0;
24+
matrix[i][0] =0;
25+
}
26+
}
27+
}
28+
// Update the complete column to be zero if first item of row is zero
29+
for (inti =1;i <numRows;i++) {
30+
if (matrix[i][0] ==0) {
31+
for (intj =1;j <numCols;j++) {
32+
matrix[i][j] =0;
5633
}
57-
58-
if (firstColZero) {
59-
for (inti =0;i <numOfRows;i++) {
60-
arr[i][0] =0;
61-
}
34+
}
35+
}
36+
// Update the complete row to be zero if first item of column is zero
37+
for (inti =1;i <numCols;i++) {
38+
if (matrix[0][i] ==0) {
39+
for (intj =1;j <numRows;j++) {
40+
matrix[j][i] =0;
6241
}
42+
}
43+
}
44+
// Set the first row to zero if flag is set
45+
if (firstRowZero) {
46+
for (inti =0;i <numCols;i++) {
47+
matrix[0][i] =0;
48+
}
49+
}
50+
// Set the first column to zero if flag is set
51+
if (firstColZero) {
52+
for (inti =0;i <numRows;i++) {
53+
matrix[i][0] =0;
54+
}
6355
}
56+
}
6457
}

‎Medium/Unique Paths.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
classSolution {
2-
3-
publicintuniquePaths(intm,intn) {
4-
int[][]arr =newint[m][n];
5-
returngetCount(arr,m,n);
2+
Integer[][]dp;
3+
publicintuniquePaths(intm,intn) {
4+
dp =newInteger[m][n];
5+
returndfs(m,n,0,0);
6+
}
7+
8+
privateintdfs(intm,intn,intx,inty) {
9+
if (x >=m ||y >=n) {
10+
return0;
611
}
7-
8-
privateintgetCount(int[][]arr,intm,intn) {
9-
for (inti=0;i<arr.length;i++) {
10-
for (intj=0;j<arr[0].length;j++) {
11-
if (i ==0 ||j ==0) {
12-
arr[i][j] =1;
13-
}
14-
else {
15-
arr[i][j] =arr[i-1][j] +arr[i][j-1];
16-
}
17-
}
18-
}
19-
20-
returnarr[m-1][n-1];
12+
if (dp[x][y] !=null) {
13+
returndp[x][y];
2114
}
15+
if (x ==m -1 &&y ==n -1) {
16+
return1;
17+
}
18+
dp[x][y] =dfs(m,n,x +1,y) +dfs(m,n,x,y +1);
19+
returndp[x][y];
20+
}
2221
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp