-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
add 0500 and 0501 java version #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
solution/0500-0599/0500.Keyboard Row/Solution.java
This file contains hidden or 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,34 @@ | ||
| class Solution { | ||
|
|
||
| public String[] findWords(String[] words) { | ||
| if (words == null) { | ||
| return null; | ||
| } | ||
| ArrayList<String> list = new ArrayList<>(); | ||
| String[] keyboards = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; | ||
| for (int i = 0; i < words.length; i++) { | ||
| String word = words[i].toLowerCase(); | ||
| for (int j = 0; j < keyboards.length; j++) { | ||
| // 先用word首字符确定属于哪一行 | ||
| if (keyboards[j].indexOf(word.charAt(0)) > -1) { | ||
| // 判断word字符串所有字符是否都属于同一行 | ||
| boolean match = match(keyboards[j], word, list); | ||
| if (match) { | ||
| list.add(words[i]); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return list.toArray(new String[list.size()]); | ||
| } | ||
|
|
||
| private boolean match(String keyboard, String word, ArrayList<String> list) { | ||
| for (int i = 1; i < word.length(); i++) { | ||
| if (keyboard.indexOf(word.charAt(i)) < 0) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java
This file contains hidden or 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,45 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode(int x) { val = x; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| int max = 0; | ||
| int cur = 0; | ||
| TreeNode preNode = null; | ||
|
|
||
| public int[] findMode(TreeNode root) { | ||
| ArrayList<Integer> list = new ArrayList<>(); | ||
| findMode(root, list); | ||
| int[] res = new int[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| res[i] = list.get(i); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| private void findMode(TreeNode root, ArrayList<Integer> list) { | ||
| if (root == null) { | ||
| return; | ||
| } | ||
| findMode(root.left, list); | ||
| if (preNode != null && root.val == preNode.val) { | ||
| cur++; | ||
| } else { | ||
| cur = 1; | ||
| } | ||
| if (max < cur) { | ||
| max = cur; | ||
| list.clear(); | ||
| list.add(root.val); | ||
| } else if (max == cur) { | ||
| list.add(root.val); | ||
| } | ||
| preNode = root; | ||
| findMode(root.right, list); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.