Skip to content

Commit 337907b

Browse files
committed
Modified 4 solutions
1 parent da4655d commit 337907b

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
class Solution {
10-
public ListNode deleteDuplicates(ListNode head) {
11-
if (head == null) return head;
12-
ListNode copyHead = 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-
return copyHead;
10+
public ListNode deleteDuplicates(ListNode head) {
11+
ListNode curr = 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+
return head;
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
class Solution {
10-
public ListNode deleteDuplicates(ListNode head) {
11-
if(head==null) return null;
12-
13-
ListNode FakeHead=new ListNode(0);
14-
FakeHead.next=head;
15-
ListNode pre=FakeHead;
16-
ListNode cur=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-
return FakeHead.next;
10+
public ListNode deleteDuplicates(ListNode head) {
11+
ListNode dummy = new ListNode(0);
12+
dummy.next = head;
13+
ListNode prev = dummy;
14+
ListNode curr = head;
15+
while (curr != null) {
16+
int val = curr.val;
17+
int count = 0;
18+
ListNode temp = 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+
return dummy.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
class Solution {
2-
public void setZeroes(int[][] arr) {
3-
if (arr.length == 0 || arr[0].length == 0) {
4-
return;
5-
}
6-
7-
8-
int numOfRows = arr.length;
9-
int numOfCols = arr[0].length;
10-
boolean firstRowZero = false;
11-
boolean firstColZero = false;
12-
13-
for (int i = 0; i < numOfCols; i++) {
14-
if (arr[0][i] == 0) {
15-
firstRowZero = true;
16-
break;
17-
}
18-
}
19-
20-
for (int i = 0; i < numOfRows; i++) {
21-
if (arr[i][0] == 0) {
22-
firstColZero = true;
23-
break;
24-
}
25-
}
26-
27-
for (int i = 1; i < numOfRows; i++) {
28-
for (int j = 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 (int i = 1; i < numOfRows; i++) {
37-
if (arr[i][0] == 0) {
38-
for (int j = 1; j < numOfCols; j++) {
39-
arr[i][j] = 0;
40-
}
41-
}
42-
}
43-
44-
for (int i = 0; i < numOfCols; i++) {
45-
if (arr[0][i] == 0) {
46-
for (int j = 1; j < numOfRows; j++) {
47-
arr[j][i] = 0;
48-
}
49-
}
50-
}
51-
52-
if (firstRowZero) {
53-
for (int i = 0; i < numOfCols; i++) {
54-
arr[0][i] = 0;
55-
}
2+
public void setZeroes(int[][] matrix) {
3+
boolean firstRowZero = false;
4+
boolean firstColZero = false;
5+
int numRows = matrix.length;
6+
int numCols = matrix[0].length;
7+
// Mark if first column needs to be set zero
8+
for (int i = 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 (int i = 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 (int i = 1; i < numRows; i++) {
21+
for (int j = 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 (int i = 1; i < numRows; i++) {
30+
if (matrix[i][0] == 0) {
31+
for (int j = 1; j < numCols; j++) {
32+
matrix[i][j] = 0;
5633
}
57-
58-
if (firstColZero) {
59-
for (int i = 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 (int i = 1; i < numCols; i++) {
38+
if (matrix[0][i] == 0) {
39+
for (int j = 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 (int i = 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 (int i = 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
class Solution {
2-
3-
public int uniquePaths(int m, int n) {
4-
int[][] arr = new int[m][n];
5-
return getCount(arr, m, n);
2+
Integer[][] dp;
3+
public int uniquePaths(int m, int n) {
4+
dp = new Integer[m][n];
5+
return dfs(m, n, 0, 0);
6+
}
7+
8+
private int dfs(int m, int n, int x, int y) {
9+
if (x >= m || y >= n) {
10+
return 0;
611
}
7-
8-
private int getCount(int[][] arr, int m, int n) {
9-
for (int i=0; i<arr.length; i++) {
10-
for (int j=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-
return arr[m-1][n-1];
12+
if (dp[x][y] != null) {
13+
return dp[x][y];
2114
}
15+
if (x == m - 1 && y == n - 1) {
16+
return 1;
17+
}
18+
dp[x][y] = dfs(m, n, x + 1, y) + dfs(m, n, x, y + 1);
19+
return dp[x][y];
20+
}
2221
}

0 commit comments

Comments
 (0)