Skip to content

Commit 6874a34

Browse files
[LEET-500] add 500
1 parent cd3ee74 commit 6874a34

File tree

5 files changed

+98
-18
lines changed

5 files changed

+98
-18
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
|506|[Relative Ranks](https://leetcode.com/problems/relative-ranks/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RelativeRanks.java) | O(nlogn) |O(n) | Easy|
99
|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Base7.java) | O(1) |O(1) | Easy|
1010
|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindModeinBinaryTree.java) | O(n) |O(k) | Easy| Binary Tree
11+
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KeyboardRow.java) | O(n) |O(1) | Easy|
1112
|492|[Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConstructTheRectangle.java) | O(n) |O(1) | Easy| Array
1213
|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MaxConsecutiveOnes.java) | O(n) |O(1) | Easy| Array
1314
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TotalHammingDistance.java) | O(n) |O(1) | Medium| Bit Manipulation

leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLargestValueinEachTreeRow.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
*/
2424
public class FindLargestValueinEachTreeRow {
2525

26-
public int[] largestValues(TreeNode root) {
26+
public List<Integer> largestValues(TreeNode root) {
27+
List<Integer> list = new ArrayList<>();
2728
Queue<TreeNode> queue = new LinkedList<>();
2829
if (root != null) {
2930
queue.offer(root);
30-
List<Integer> list = new ArrayList<>();
3131
while (!queue.isEmpty()) {
3232
int max = Integer.MIN_VALUE;
3333
int size = queue.size();
@@ -39,13 +39,7 @@ public int[] largestValues(TreeNode root) {
3939
}
4040
list.add(max);
4141
}
42-
int[] result = new int[list.size()];
43-
for (int i = 0; i < list.size(); i++) {
44-
result[i] = list.get(i);
45-
}
46-
return result;
47-
} else {
48-
return new int[]{};
4942
}
43+
return list;
5044
}
5145
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
7+
* <p>
8+
* <p>
9+
* American keyboard
10+
* <p>
11+
* <p>
12+
* Example 1:
13+
* Input: ["Hello", "Alaska", "Dad", "Peace"]
14+
* Output: ["Alaska", "Dad"]
15+
* Note:
16+
* You may use one character in the keyboard more than once.
17+
* You may assume the input string will only contain letters of alphabet.
18+
*/
19+
public class KeyboardRow {
20+
21+
public String[] findWords(String[] words) {
22+
final Set<Character> row1 = new HashSet<>(Arrays.asList('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'));
23+
final Set<Character> row2 = new HashSet<>(Arrays.asList('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'));
24+
final Set<Character> row3 = new HashSet<>(Arrays.asList('z', 'x', 'c', 'v', 'b', 'n', 'm'));
25+
final List<Set<Character>> setList = Arrays.asList(row1, row2, row3);
26+
List<String> wordList = new ArrayList<>();
27+
for (String word : words) {
28+
String lowerCaseWord = word.toLowerCase();
29+
boolean can = true;
30+
for (Set<Character> row : setList) {
31+
for (char c : lowerCaseWord.toCharArray()) {
32+
if (!row.contains(c)) {
33+
can = false;
34+
break;
35+
}
36+
}
37+
if (can) {
38+
wordList.add(word);
39+
break;
40+
}
41+
can = true;
42+
}
43+
}
44+
String[] result = new String[wordList.size()];
45+
for (int i = 0; i < wordList.size(); i++) {
46+
result[i] = wordList.get(i);
47+
}
48+
return result;
49+
}
50+
51+
}

leetcode-algorithms/src/test/java/com/stevesun/FindLargestValueinEachTreeRowTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import org.junit.BeforeClass;
77
import org.junit.Test;
88

9-
import static org.junit.Assert.assertArrayEquals;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.assertEquals;
1014

1115
public class FindLargestValueinEachTreeRowTest {
1216
private static FindLargestValueinEachTreeRow test;
13-
private static int[] expected;
14-
private static int[] actual;
17+
private static List<Integer> expected;
18+
private static List<Integer> actual;
1519
private static TreeNode root;
1620

1721
@BeforeClass
@@ -21,8 +25,8 @@ public static void setup(){
2125

2226
@Before
2327
public void setupForEachTest(){
24-
expected = new int[]{};
25-
actual = new int[]{};
28+
expected = new ArrayList<>();
29+
actual = new ArrayList<>();
2630
root = new TreeNode(0);
2731
}
2832

@@ -31,17 +35,17 @@ public void test1(){
3135
TreeNode root = new TreeNode(1);
3236
root.left = new TreeNode(3);
3337
root.right= new TreeNode(2);
34-
expected = new int[]{1, 3};
38+
expected = Arrays.asList(1, 3);
3539
actual = test.largestValues(root);
36-
assertArrayEquals(expected, actual);
40+
assertEquals(expected, actual);
3741

3842
}
3943

4044
@Test
4145
public void test2(){
42-
expected = new int[]{};
46+
expected = new ArrayList<>();
4347
actual = test.largestValues(null);
44-
assertArrayEquals(expected, actual);
48+
assertEquals(expected, actual);
4549

4650
}
4751
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.KeyboardRow;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
/**
10+
* Created by stevesun on 1/15/17.
11+
*/
12+
public class KeyboardRowTest {
13+
private static KeyboardRow test;
14+
private static String[] expected;
15+
private static String[] actual;
16+
private String[] words;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new KeyboardRow();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
words = new String[]{"Alaska", "Hello", "Dad", "Peace"};
26+
expected = new String[]{"Alaska", "Dad"};
27+
actual = test.findWords(words);
28+
assertArrayEquals(expected, actual);
29+
}
30+
}

0 commit comments

Comments
 (0)