From a2f368196f9b496a8a171538ad491765ab4630cb Mon Sep 17 00:00:00 2001 From: HendSame <154903157@qq.com> Date: Sun, 12 Apr 2020 20:55:39 +0800 Subject: [PATCH 1/4] add 0500 and 0501 java version --- .../0500-0599/0500.Keyboard Row/Solution.java | 31 +++++++++++++++++ .../Solution.java | 33 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 solution/0500-0599/0500.Keyboard Row/Solution.java create mode 100644 solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java diff --git a/solution/0500-0599/0500.Keyboard Row/Solution.java b/solution/0500-0599/0500.Keyboard Row/Solution.java new file mode 100644 index 0000000000000..6c4f90b27b099 --- /dev/null +++ b/solution/0500-0599/0500.Keyboard Row/Solution.java @@ -0,0 +1,31 @@ + public String[] findWords(String[] words) { + if(words == null){ + return null; + } + ArrayList 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 list) { + for (int i = 1; i list = new ArrayList<>(); + findMode(root, list); + int[] res = new int[list.size()]; + for (int i = 0;i 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); + } From 56511fb72f2ff488593d5678932e36036565cc25 Mon Sep 17 00:00:00 2001 From: HendSame <154903157@qq.com> Date: Mon, 13 Apr 2020 21:26:46 +0800 Subject: [PATCH 2/4] feat:format code --- .../0500-0599/0500.Keyboard Row/Solution.java | 25 +++++++++++-------- .../Solution.java | 23 +++++++++++------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/solution/0500-0599/0500.Keyboard Row/Solution.java b/solution/0500-0599/0500.Keyboard Row/Solution.java index 6c4f90b27b099..ac61b5f269e4e 100644 --- a/solution/0500-0599/0500.Keyboard Row/Solution.java +++ b/solution/0500-0599/0500.Keyboard Row/Solution.java @@ -1,17 +1,21 @@ +import java.util.ArrayList; + +public class Solution { + public String[] findWords(String[] words) { - if(words == null){ + if (words == null) { return null; } ArrayList list = new ArrayList<>(); - String[] keyboards = {"qwertyuiop","asdfghjkl","zxcvbnm"}; + 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++) { + for (int j = 0; j < keyboards.length; j++) { // 先用word首字符确定属于哪一行 - if(keyboards[j].indexOf(word.charAt(0))>-1){ + if (keyboards[j].indexOf(word.charAt(0)) > -1) { // 判断word字符串所有字符是否都属于同一行 - boolean match = match(keyboards[j], word,list); - if(match){ + boolean match = match(keyboards[j], word, list); + if (match) { list.add(words[i]); } break; @@ -22,10 +26,11 @@ public String[] findWords(String[] words) { } private boolean match(String keyboard, String word, ArrayList list) { - for (int i = 1; i list = new ArrayList<>(); findMode(root, list); int[] res = new int[list.size()]; - for (int i = 0;i list) { - if(root == null){ + if (root == null) { return; } findMode(root.left, list); - if(preNode!=null && root.val == preNode.val ){ + if (preNode != null && root.val == preNode.val) { cur++; } else { cur = 1; } - if(max < cur ){ + if (max < cur) { max = cur; list.clear(); list.add(root.val); - }else if(max == cur) { + } else if (max == cur) { list.add(root.val); } preNode = root; findMode(root.right, list); } +} From f20932d361ca56d5155fc7c81a3f5398cb31d992 Mon Sep 17 00:00:00 2001 From: acbin <44314231+acbin@users.noreply.github.com> Date: Wed, 15 Apr 2020 10:23:56 +0800 Subject: [PATCH 3/4] Update Solution.java --- .../Solution.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java b/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java index 296a7c37b357e..c1b05e8549886 100644 --- a/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java +++ b/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.java @@ -1,8 +1,13 @@ -import java.util.List; -import java.util.ArrayList; - -public class Solution { - +/** + * 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; From b6ca69437fb6c42ddd67623398c082267375df61 Mon Sep 17 00:00:00 2001 From: acbin <44314231+acbin@users.noreply.github.com> Date: Wed, 15 Apr 2020 10:24:36 +0800 Subject: [PATCH 4/4] Update Solution.java --- solution/0500-0599/0500.Keyboard Row/Solution.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/solution/0500-0599/0500.Keyboard Row/Solution.java b/solution/0500-0599/0500.Keyboard Row/Solution.java index ac61b5f269e4e..cf4cbd85e342b 100644 --- a/solution/0500-0599/0500.Keyboard Row/Solution.java +++ b/solution/0500-0599/0500.Keyboard Row/Solution.java @@ -1,6 +1,4 @@ -import java.util.ArrayList; - -public class Solution { +class Solution { public String[] findWords(String[] words) { if (words == null) { @@ -33,4 +31,4 @@ private boolean match(String keyboard, String word, ArrayList list) { } return true; } -} \ No newline at end of file +}