Skip to content

Commit f336742

Browse files
committed
Added 4 solutions
1 parent 2287a5f commit f336742

4 files changed

+123
-0
lines changed

Easy/Valid Word Abbrevation.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public static boolean validWordAbbreviation(String word, String abbr) {
3+
char[] wordChar = word.toCharArray();
4+
char[] abbrChar = abbr.toCharArray();
5+
6+
int i = 0;
7+
int j = 0;
8+
9+
while (j < abbrChar.length && i < wordChar.length) {
10+
if (abbrChar[j] == wordChar[i]) {
11+
i++;
12+
j++;
13+
continue;
14+
}
15+
16+
if (abbrChar[j] <= '0' || abbrChar[j] > '9') {
17+
return false;
18+
}
19+
20+
int start = j;
21+
while (j<abbrChar.length && abbrChar[j] >= '0' && abbrChar[j] <= '9') {
22+
++j;
23+
}
24+
25+
int num = Integer.valueOf(abbr.substring(start, j));
26+
i += num;
27+
}
28+
29+
return i == wordChar.length && j == abbrChar.length;
30+
}
31+
}

Medium/Count Univalue Subtrees.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
int count = 0;
12+
public int countUnivalSubtrees(TreeNode root) {
13+
updateCount(root);
14+
return count;
15+
}
16+
17+
private void updateCount(TreeNode root) {
18+
if (root == null) {
19+
return;
20+
}
21+
22+
if (isUnivalue(root, root.val)) {
23+
count++;
24+
}
25+
26+
updateCount(root.left);
27+
updateCount(root.right);
28+
}
29+
30+
private boolean isUnivalue(TreeNode root, int val) {
31+
if (root == null) {
32+
return true;
33+
}
34+
if (root.val != val) {
35+
return false;
36+
}
37+
38+
return isUnivalue(root.left, val) && isUnivalue(root.right, val);
39+
}
40+
}

Medium/Max Consecutives Ones II.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int findMaxConsecutiveOnes(int[] nums) {
3+
int slow = 0;
4+
int fast = 0;
5+
int max = Integer.MIN_VALUE;
6+
boolean flipped = false;
7+
int zeroIndex = slow;
8+
9+
while (fast < nums.length) {
10+
if (nums[fast] != 1) {
11+
if (!flipped) {
12+
flipped = true;
13+
zeroIndex = fast;
14+
}
15+
else {
16+
max = Math.max(max, fast - slow);
17+
slow = zeroIndex + 1;
18+
flipped = false;
19+
fast = zeroIndex;
20+
}
21+
}
22+
23+
fast++;
24+
}
25+
26+
return Math.max(max, fast-slow);
27+
}
28+
}

Medium/Sort Transformed Array.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public static int[] sortTransformedArray(int[] nums, int a, int b, int c) {
3+
int[] ans = new int[nums.length];
4+
int start = 0;
5+
int end = nums.length-1;
6+
7+
int index = a >= 0 ? end : start;
8+
9+
while (start <= end){
10+
if (a >= 0) {
11+
ans[index--] = getQuad(nums[start], a, b, c) >= getQuad(nums[end], a, b, c) ? getQuad(nums[start++], a, b, c) : getQuad(nums[end--], a, b, c);
12+
}
13+
else {
14+
ans[index++] = getQuad(nums[start], a, b, c) >= getQuad(nums[end], a, b, c) ? getQuad(nums[end--], a, b, c) : getQuad(nums[start++], a, b, c);
15+
}
16+
}
17+
18+
return ans;
19+
}
20+
21+
private static int getQuad(int num, int a, int b, int c) {
22+
return a *(num * num) + b * num + c;
23+
}
24+
}

0 commit comments

Comments
 (0)