-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.leetcode; | ||
|
||
import com.sun.xml.internal.bind.v2.model.core.NonElement; | ||
import sun.reflect.generics.tree.Tree; | ||
|
||
import java.net.Inet4Address; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution1457 { | ||
int count; | ||
public int pseudoPalindromicPaths (TreeNode root) { | ||
this.count = 0; | ||
dfs(root, new HashMap<>()); | ||
return this.count; | ||
} | ||
|
||
private void dfs(TreeNode root, HashMap<Integer, Integer> map) { | ||
int v = 1; | ||
if (map.containsKey(root.val)) { | ||
v += map.get(root.val); | ||
map.replace(root.val, v); | ||
} else { | ||
map.put(root.val, v); | ||
} | ||
if (root.left != null) { | ||
dfs(root.left, map); | ||
} | ||
if (root.right != null) { | ||
dfs(root.right, map); | ||
} | ||
if (root.left == null && root.right == null) { | ||
int oddCnt = 0; | ||
for (Map.Entry<Integer, Integer> entry: map.entrySet()) { | ||
if (entry.getValue() % 2 == 1) { | ||
oddCnt++; | ||
} | ||
if (oddCnt > 1) { | ||
break; | ||
} | ||
} | ||
if (oddCnt <= 1) { | ||
this.count++; | ||
} | ||
} | ||
|
||
if (v == 1) { | ||
map.remove(root.val); | ||
} else { | ||
map.replace(root.val, v-1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.leetcode; | ||
|
||
class Solution { | ||
public int countDigits(int num) { | ||
int copy = num; | ||
int res = 0; | ||
while (copy > 0) { | ||
int tmp = copy % 10; | ||
copy /= 10; | ||
if (num % tmp == 0) { | ||
res++; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.leetcode; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
|
||
class Solution2586 { | ||
public int vowelStrings(String[] words, int left, int right) { | ||
int res = 0; | ||
HashSet<Character> vowel = new HashSet<>(5); | ||
vowel.add('a'); | ||
vowel.add('e'); | ||
vowel.add('i'); | ||
vowel.add('o'); | ||
vowel.add('u'); | ||
for (int i = left; i <= right; i++) { | ||
String s = words[i]; | ||
if (vowel.contains(s.charAt(0)) && vowel.contains(s.charAt(s.length()-1))) { | ||
res++; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.leetcode; | ||
|
||
import java.util.Arrays; | ||
|
||
class Solution2594 { | ||
public long repairCars(int[] ranks, int cars) { | ||
long l = 1, r = (long)Arrays.stream(ranks).min().getAsInt() * cars * cars; | ||
while (l < r) { | ||
long mid = (l + r) / 2; | ||
if (this.check(ranks, cars, mid)) { | ||
r = mid; | ||
} else { | ||
l = mid + 1; | ||
} | ||
} | ||
return l; | ||
} | ||
|
||
private boolean check(int[] ranks, int cars, long time) { | ||
for (int rank: ranks) { | ||
int car = (int) Math.floor(Math.sqrt((double)time / rank)); | ||
cars -= car; | ||
if (cars <= 0) { | ||
return true; | ||
} | ||
} | ||
return cars <= 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.leetcode; | ||
|
||
class Solution2609 { | ||
public int findTheLongestBalancedSubstring(String s) { | ||
int res = 0; | ||
int[] cnt = new int[2]; | ||
for (char c: s.toCharArray()) { | ||
switch (c) { | ||
case '0': | ||
if (cnt[1] > 0) { | ||
cnt = new int[2]; | ||
} | ||
cnt[0]++; | ||
break; | ||
case '1': | ||
cnt[1]++; | ||
if (cnt[0] >= cnt[1]) { | ||
res = Math.max(res, cnt[1] * 2); | ||
} else { | ||
cnt = new int[2]; | ||
} | ||
break; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.leetcode; | ||
|
||
|
||
class Solution2760 { | ||
public int longestAlternatingSubarray(int[] nums, int threshold) { | ||
int res = 0; | ||
for (int l = 0; l < nums.length; l++) { | ||
boolean isEven = true; | ||
boolean isInclude = true; | ||
for (int i = l; i < nums.length; i++) { | ||
if (((nums[i] % 2 == 0) == isEven) && nums[i] <= threshold) { | ||
isEven = !isEven; | ||
} else { | ||
res = Math.max(res, i - l); | ||
isInclude = false; | ||
break; | ||
} | ||
} | ||
if (isInclude) { | ||
res = Math.max(res, nums.length - l); | ||
} | ||
|
||
} | ||
return res; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] nums = new int[]{3,2,5,4}; | ||
Solution2760 solution2760 = new Solution2760(); | ||
System.out.println(solution2760.longestAlternatingSubarray(nums, 5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters