Skip to content

Commit 743bba8

Browse files
committed
Modified 3 solutions
1 parent d886046 commit 743bba8

File tree

3 files changed

+52
-48
lines changed

3 files changed

+52
-48
lines changed

Easy/Add Strings.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
class Solution {
2-
public String addStrings(String num1, String num2) {
3-
StringBuilder sb = new StringBuilder();
4-
int idx1 = num1.length() - 1;
5-
int idx2 = num2.length() - 1;
6-
int carry = 0;
7-
8-
while (idx1 >= 0 || idx2 >= 0) {
9-
int temp = carry;
10-
if (idx1 >= 0 && idx2 >= 0) {
11-
temp += (num1.charAt(idx1) - '0') + (num2.charAt(idx2) - '0');
12-
}
13-
else if (idx1 >= 0 && idx2 < 0) {
14-
temp += (num1.charAt(idx1) - '0');
15-
}
16-
else {
17-
temp += (num2.charAt(idx2) - '0');
18-
}
19-
20-
carry = temp > 9 ? temp / 10 : 0;
21-
temp = temp > 9 ? temp % 10 : temp;
22-
sb.append(temp);
23-
idx1--;
24-
idx2--;
25-
}
26-
27-
sb.append(carry > 0 ? carry : "");
28-
29-
return sb.reverse().toString();
2+
public String addStrings(String num1, String num2) {
3+
StringBuilder sb = new StringBuilder();
4+
int idx1 = num1.length() - 1;
5+
int idx2 = num2.length() - 1;
6+
int carry = 0;
7+
while (idx1 >= 0 || idx2 >= 0) {
8+
int temp = carry;
9+
if (idx1 >= 0 && idx2 >= 0) {
10+
temp += Character.getNumericValue(num1.charAt(idx1--)) + Character.getNumericValue(num2.charAt(idx2--));
11+
}
12+
else if (idx1 >= 0 && idx2 < 0) {
13+
temp += Character.getNumericValue(num1.charAt(idx1--));
14+
}
15+
else {
16+
temp += Character.getNumericValue(num2.charAt(idx2--));
17+
}
18+
carry = temp > 9 ? 1 : 0;
19+
temp = temp > 9 ? temp % 10 : temp;
20+
sb.append(temp);
3021
}
22+
if (carry > 0) {
23+
sb.append(carry);
24+
}
25+
return sb.reverse().toString();
26+
}
3127
}

Easy/Palindrome Number.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
class Solution {
2-
public boolean isPalindrome(int x) {
3-
return x < 0 ? false : x == getReverseDigit(x);
2+
public boolean isPalindrome(int x) {
3+
if (x < 0) {
4+
return false;
45
}
5-
6-
private int getReverseDigit(int x) {
7-
int num = 0;
8-
while (x > 0) {
9-
num = num * 10 + x % 10;
10-
x /= 10;
11-
}
12-
13-
return num;
6+
return x - reverse(x) == 0;
7+
}
8+
9+
private int reverse(int n) {
10+
int newNum = 0;
11+
while (n > 0) {
12+
newNum = newNum * 10 + n % 10;
13+
n /= 10;
1414
}
15+
return newNum;
16+
}
1517
}

Easy/Trim a Binary Search Tree.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
* }
99
*/
1010
class Solution {
11-
public TreeNode trimBST(TreeNode root, int L, int R) {
12-
if (root == null) return null;
13-
if (root.val < L) return trimBST(root.right, L, R);
14-
if (root.val > R) return trimBST(root.left, L, R);
15-
16-
root.left = trimBST(root.left, L, R);
17-
root.right = trimBST(root.right, L, R);
18-
19-
return root;
11+
public TreeNode trimBST(TreeNode root, int L, int R) {
12+
if (root == null) {
13+
return null;
2014
}
15+
if (root.val < L) {
16+
return trimBST(root.right, L, R);
17+
}
18+
else if (root.val > R) {
19+
return trimBST(root.left, L, R);
20+
}
21+
else {
22+
root.left = trimBST(root.left, L, R);
23+
root.right = trimBST(root.right, L, R);
24+
return root;
25+
}
26+
}
2127
}

0 commit comments

Comments
 (0)