Skip to content

Commit 855fae7

Browse files
committed
Added 3 solutions
1 parent 7a8d0c9 commit 855fae7

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

Easy/Factorial Trailing Zeroes.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int trailingZeroes(int n) {
3+
int count = 0;
4+
while (n > 4) {
5+
count += n/5;
6+
n = n/5;
7+
}
8+
9+
return count;
10+
}
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int[] findMode(TreeNode root) {
12+
Map<Integer, Integer> map = new HashMap<>();
13+
inorderTraversal(root, map);
14+
15+
int maxVal = 0;
16+
int count = 0;
17+
18+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
19+
if (entry.getValue() > maxVal) {
20+
maxVal = entry.getValue();
21+
count = 1;
22+
}
23+
else if (entry.getValue() == maxVal) {
24+
count++;
25+
}
26+
}
27+
28+
int[] ans = new int[count];
29+
int i = 0;
30+
31+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
32+
if (entry.getValue() == maxVal) {
33+
ans[i++] = entry.getKey();
34+
}
35+
}
36+
37+
return ans;
38+
39+
}
40+
41+
public void inorderTraversal(TreeNode root, Map<Integer, Integer> map) {
42+
43+
if (root == null) return;
44+
45+
inorderTraversal(root.left, map);
46+
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
47+
inorderTraversal(root.right, map);
48+
}
49+
}

Easy/Valid Palindrome.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
public class Solution {
1+
class Solution {
22
public boolean isPalindrome(String s) {
3-
if (s=="" || s.length() == 0) {
4-
return true;
5-
}
6-
else {
7-
StringBuilder sb=new StringBuilder("");
8-
s = s.toLowerCase();
9-
for (int i=0;i<s.length();i++) {
10-
if (((int) s.charAt(i) >= 97 && (int) s.charAt(i) <= 122) || Character.isDigit(s.charAt(i))) {
11-
sb.append(Character.toString(s.charAt(i)));
12-
}
3+
StringBuilder sb = new StringBuilder("");
4+
5+
for (char c : s.toCharArray()) {
6+
if (Character.isLetterOrDigit(c)) {
7+
sb.append(c);
138
}
14-
return sb.toString().toLowerCase().equals(sb.reverse().toString().toLowerCase());
159
}
10+
11+
String S = sb.toString().toLowerCase();
12+
13+
return isPalindromeImpl(S);
14+
}
15+
16+
public boolean isPalindromeImpl(String s) {
17+
if (s.length() <= 1) return true;
18+
19+
return s.charAt(0) == s.charAt(s.length()-1) && isPalindromeImpl(s.substring(1, s.length()-1));
1620
}
17-
}
21+
}

0 commit comments

Comments
 (0)