Skip to content

Commit f670ef9

Browse files
committed
upload 30 answers, Aug 25th
1 parent 7995015 commit f670ef9

30 files changed

+969
-0
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence:
3+
4+
"abc" -> "bcd" -> ... -> "xyz"
5+
6+
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.
7+
8+
For example,
9+
given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], Return:
10+
11+
[
12+
["abc","bcd","xyz"],
13+
["az","ba"],
14+
["acef"],
15+
["a","z"]
16+
]
17+
18+
Note: For the return value, each inner list’s elements must follow the lexicographic order.
19+
*/
20+
21+
public class GroupShiftedStrings {
22+
public List<List<String>> groupStrings(String[] strings) {
23+
assert strings != null : "null array";
24+
List<List<String>> result = new ArrayList<>();
25+
Map<String, List<String>> map = new HashMap<>();
26+
for (String s : strings) {
27+
String code = getFeatureCode(s);
28+
List<String> val;
29+
if (!map.containsKey(code)) {
30+
val = new ArrayList<>();
31+
} else {
32+
val = map.get(code);
33+
}
34+
val.add(s);
35+
map.put(code, val);
36+
}
37+
for (String key : map.keySet()) {
38+
List<String> val = map.get(key);
39+
Collections.sort(val);
40+
result.add(val);
41+
}
42+
return result;
43+
}
44+
45+
private String getFeatureCode(String s) {
46+
StringBuilder sb = new StringBuilder();
47+
sb.append("#");
48+
for (int i = 1; i < s.length(); i++) {
49+
int tmp = ((s.charAt(i) - s.charAt(i - 1)) + 26) % 26;
50+
sb.append(tmp).append("#");
51+
}
52+
return sb.toString();
53+
}
54+
}
55+
56+
/*
57+
Reference:
58+
http://sbzhouhao.net/LeetCode/LeetCode-Group-Shifted-Strings.html
59+
*/

Additional/Easy/MeetingRooms.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
3+
4+
For example,
5+
Given [[0, 30],[5, 10],[15, 20]],
6+
return false
7+
*/
8+
9+
public class MeetingRooms {
10+
public boolean canAttendMeetings(Interval[] intervals) {
11+
assert intervals != null : "null input";
12+
Arrays.sort(intervals, (o1, o2) -> {
13+
int r = o1.start - o2.start;
14+
return r == 0 ? o1.end - o2.end : r;
15+
});
16+
for (int i = 1; i < intervals.length; i++) {
17+
Interval i1 = intervals[i - 1];
18+
Interval i2 = intervals[i];
19+
if (i1.end > i2.start) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
}
26+
27+
/*
28+
Reference:
29+
http://sbzhouhao.net/LeetCode/LeetCode-Meeting-Rooms.html
30+
*/
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Given a string, determine if a permutation of the string could form a palindrome.
3+
For example,
4+
"code" -> False, "aab" -> True, "carerac" -> True.
5+
6+
Understand the problem:
7+
The problem can be easily solved by count the frequency of each character using a hash map. The only thing need to take special care is consider the length of the string to be even or odd.
8+
-- If the length is even. Each character should appear exactly times of 2, e.g. 2, 4, 6, etc..
9+
-- If the length is odd. One and only one character could appear odd times
10+
*/
11+
12+
public class PalindromePermutation {
13+
public boolean canPermutePalindrome(String s) {
14+
Set<Character> set=new HashSet<Character>();
15+
for(int i=0; i<s.length(); ++i){
16+
if (!set.contains(s.charAt(i)))
17+
set.add(s.charAt(i));
18+
else
19+
set.remove(s.charAt(i));
20+
}
21+
return set.size()==0 || set.size()==1;
22+
}
23+
}
24+
25+
/*
26+
Reference:
27+
https://leetcode.com/discuss/53295/java-solution-w-set-one-pass-without-counters
28+
https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java
29+
https://leetcode.com/discuss/53187/ac-java-solution
30+
https://leetcode.com/discuss/53201/java-use-map-one-scan
31+
https://leetcode.com/discuss/53234/accepted-java-o-n-solution-using-hashset-only-one-scan
32+
https://leetcode.com/discuss/53744/java-ac-8-lines
33+
http://buttercola.blogspot.com/2015/08/leetcode-palindrome-permutation.html
34+
*/
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
3+
4+
For example,
5+
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
6+
Given word1 = "coding", word2 = "practice", return 3. Given word1 = "makes", word2 = "coding", return 1.
7+
8+
Note:
9+
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
10+
*/
11+
12+
public class ShortestWordDistance {
13+
public int shortestDistance(String[] words, String word1, String word2) {
14+
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
15+
for (int i = 0; i < words.length; i++) {
16+
String s = words[i];
17+
List<Integer> list;
18+
if (map.containsKey(s)) {
19+
list = map.get(s);
20+
} else {
21+
list = new ArrayList<>();
22+
}
23+
list.add(i);
24+
map.put(s, list);
25+
}
26+
List<Integer> l1 = map.get(word1);
27+
List<Integer> l2 = map.get(word2);
28+
int min = Integer.MAX_VALUE;
29+
for (int a : l1) {
30+
for (int b : l2) {
31+
min = Math.min(Math.abs(b - a), min);
32+
}
33+
}
34+
return min;
35+
}
36+
}
37+
38+
/*
39+
Reference:
40+
http://sbzhouhao.net/LeetCode/LeetCode-Shortest-Word-Distance.html
41+
http://www.cnblogs.com/jcliBlogger/p/4704962.html
42+
*/
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
3+
4+
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
5+
6+
For example,
7+
the numbers "69", "88", and "818" are all strobogrammatic.
8+
*/
9+
10+
public class StrobogrammaticNumber {
11+
public boolean isStrobogrammatic(String num) {
12+
for (int i = 0; i <= num.length() / 2; i++) {
13+
char a = num.charAt(i);
14+
char b = num.charAt(num.length() - 1 - i);
15+
if (!isValid(a, b)) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
private boolean isValid(char c, char b) {
22+
switch (c) {
23+
case '1':
24+
return b == '1';
25+
case '6':
26+
return b == '9';
27+
case '9':
28+
return b == '6';
29+
case '8':
30+
return b == '8';
31+
case '0':
32+
return b == '0';
33+
default:
34+
return false;
35+
}
36+
}
37+
}
38+
39+
/*
40+
Reference:
41+
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number.html
42+
*/
File renamed without changes.

Additional/Hard/PaintHouseII.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
3+
The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.
4+
5+
Note:
6+
All costs are positive integers.
7+
8+
Follow up:
9+
Could you solve it in O(nk) runtime?
10+
*/
11+
12+
/*
13+
Referene:
14+
https://leetcode.com/discuss/52964/my-accept-java-o-nk-solution
15+
https://leetcode.com/discuss/52969/java-o-nkk-and-improved-o-nk-solution
16+
http://www.meetqun.com/thread-10660-1-1.html
17+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
3+
4+
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
5+
6+
For example,
7+
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
8+
9+
Note:
10+
Because the range might be a large number, the low and high numbers are represented as string.
11+
*/
12+
13+
public class StrobogrammaticNumberIII {
14+
private char[] validNumbers = new char[]{'0', '1', '6', '8', '9'};
15+
private char[] singleable = new char[]{'0', '1', '8'};
16+
17+
public int strobogrammaticInRange(String low, String high) {
18+
assert low != null && high != null;
19+
int ll = low.length();
20+
int hl = high.length();
21+
int result = 0;
22+
if(ll > hl || (ll == hl&&low.compareTo(high) > 0)) {
23+
return 0;
24+
}
25+
List<String> list = findStrobogrammatic(ll);
26+
if (ll == hl) {
27+
for (String s : list) {
28+
if (s.compareTo(low) >= 0 && s.compareTo(high) <= 0) {
29+
result++;
30+
}
31+
if (s.compareTo(high) > 0) {
32+
break;
33+
}
34+
}
35+
} else {
36+
for (int i = list.size() - 1; i >= 0; i--) {
37+
String s = list.get(i);
38+
if (s.compareTo(low) >= 0) {
39+
result++;
40+
}
41+
if (s.compareTo(low) < 0) {
42+
break;
43+
}
44+
}
45+
list = findStrobogrammatic(hl);
46+
for (String s : list) {
47+
if (s.compareTo(high) <= 0) {
48+
result++;
49+
}
50+
if (s.compareTo(high) > 0) {
51+
break;
52+
}
53+
}
54+
for (int i = ll + 1; i < hl; i++) {
55+
result += findStrobogrammatic(i).size();
56+
}
57+
}
58+
return result;
59+
}
60+
61+
public List<String> findStrobogrammatic(int n) {
62+
assert n > 0;
63+
List<String> result = new ArrayList<>();
64+
if (n == 1) {
65+
for (char c : singleable) {
66+
result.add(String.valueOf(c));
67+
}
68+
return result;
69+
}
70+
if (n % 2 == 0) {
71+
helper(n, new StringBuilder(), result);
72+
} else {
73+
helper(n - 1, new StringBuilder(), result);
74+
List<String> tmp = new ArrayList<>();
75+
for (String s : result) {
76+
for (char c : singleable) {
77+
tmp.add(new StringBuilder(s).insert(s.length() / 2, c).toString());
78+
}
79+
}
80+
result = tmp;
81+
}
82+
return result;
83+
}
84+
85+
private void helper(int n, StringBuilder sb, List<String> result) {
86+
if (sb.length() > n) return;
87+
if (sb.length() == n) {
88+
if (sb.length() > 0 && sb.charAt(0) != '0') {
89+
result.add(sb.toString());
90+
}
91+
return;
92+
}
93+
for (char c : validNumbers) {
94+
StringBuilder tmp = new StringBuilder(sb);
95+
String s = "" + c + findMatch(c);
96+
tmp.insert(tmp.length() / 2, s);
97+
helper(n, tmp, result);
98+
}
99+
}
100+
101+
private char findMatch(char c) {
102+
switch (c) {
103+
case '1':
104+
return '1';
105+
case '6':
106+
return '9';
107+
case '9':
108+
return '6';
109+
case '8':
110+
return '8';
111+
case '0':
112+
return '0';
113+
default:
114+
return 0;
115+
}
116+
}
117+
}
118+
119+
/*
120+
Reference:
121+
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number-III.html
122+
*/

0 commit comments

Comments
 (0)