-
Notifications
You must be signed in to change notification settings - Fork 37
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
1 parent
7995015
commit f670ef9
Showing
30 changed files
with
969 additions
and
0 deletions.
There are no files selected for viewing
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,59 @@ | ||
/* | ||
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: | ||
"abc" -> "bcd" -> ... -> "xyz" | ||
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. | ||
For example, | ||
given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], Return: | ||
[ | ||
["abc","bcd","xyz"], | ||
["az","ba"], | ||
["acef"], | ||
["a","z"] | ||
] | ||
Note: For the return value, each inner list’s elements must follow the lexicographic order. | ||
*/ | ||
|
||
public class GroupShiftedStrings { | ||
public List<List<String>> groupStrings(String[] strings) { | ||
assert strings != null : "null array"; | ||
List<List<String>> result = new ArrayList<>(); | ||
Map<String, List<String>> map = new HashMap<>(); | ||
for (String s : strings) { | ||
String code = getFeatureCode(s); | ||
List<String> val; | ||
if (!map.containsKey(code)) { | ||
val = new ArrayList<>(); | ||
} else { | ||
val = map.get(code); | ||
} | ||
val.add(s); | ||
map.put(code, val); | ||
} | ||
for (String key : map.keySet()) { | ||
List<String> val = map.get(key); | ||
Collections.sort(val); | ||
result.add(val); | ||
} | ||
return result; | ||
} | ||
|
||
private String getFeatureCode(String s) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("#"); | ||
for (int i = 1; i < s.length(); i++) { | ||
int tmp = ((s.charAt(i) - s.charAt(i - 1)) + 26) % 26; | ||
sb.append(tmp).append("#"); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
http://sbzhouhao.net/LeetCode/LeetCode-Group-Shifted-Strings.html | ||
*/ |
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,30 @@ | ||
/* | ||
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. | ||
For example, | ||
Given [[0, 30],[5, 10],[15, 20]], | ||
return false | ||
*/ | ||
|
||
public class MeetingRooms { | ||
public boolean canAttendMeetings(Interval[] intervals) { | ||
assert intervals != null : "null input"; | ||
Arrays.sort(intervals, (o1, o2) -> { | ||
int r = o1.start - o2.start; | ||
return r == 0 ? o1.end - o2.end : r; | ||
}); | ||
for (int i = 1; i < intervals.length; i++) { | ||
Interval i1 = intervals[i - 1]; | ||
Interval i2 = intervals[i]; | ||
if (i1.end > i2.start) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
http://sbzhouhao.net/LeetCode/LeetCode-Meeting-Rooms.html | ||
*/ |
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,34 @@ | ||
/* | ||
Given a string, determine if a permutation of the string could form a palindrome. | ||
For example, | ||
"code" -> False, "aab" -> True, "carerac" -> True. | ||
Understand the problem: | ||
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. | ||
-- If the length is even. Each character should appear exactly times of 2, e.g. 2, 4, 6, etc.. | ||
-- If the length is odd. One and only one character could appear odd times | ||
*/ | ||
|
||
public class PalindromePermutation { | ||
public boolean canPermutePalindrome(String s) { | ||
Set<Character> set=new HashSet<Character>(); | ||
for(int i=0; i<s.length(); ++i){ | ||
if (!set.contains(s.charAt(i))) | ||
set.add(s.charAt(i)); | ||
else | ||
set.remove(s.charAt(i)); | ||
} | ||
return set.size()==0 || set.size()==1; | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
https://leetcode.com/discuss/53295/java-solution-w-set-one-pass-without-counters | ||
https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java | ||
https://leetcode.com/discuss/53187/ac-java-solution | ||
https://leetcode.com/discuss/53201/java-use-map-one-scan | ||
https://leetcode.com/discuss/53234/accepted-java-o-n-solution-using-hashset-only-one-scan | ||
https://leetcode.com/discuss/53744/java-ac-8-lines | ||
http://buttercola.blogspot.com/2015/08/leetcode-palindrome-permutation.html | ||
*/ |
File renamed without changes.
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,42 @@ | ||
/* | ||
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. | ||
For example, | ||
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. | ||
Given word1 = "coding", word2 = "practice", return 3. Given word1 = "makes", word2 = "coding", return 1. | ||
Note: | ||
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list. | ||
*/ | ||
|
||
public class ShortestWordDistance { | ||
public int shortestDistance(String[] words, String word1, String word2) { | ||
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>(); | ||
for (int i = 0; i < words.length; i++) { | ||
String s = words[i]; | ||
List<Integer> list; | ||
if (map.containsKey(s)) { | ||
list = map.get(s); | ||
} else { | ||
list = new ArrayList<>(); | ||
} | ||
list.add(i); | ||
map.put(s, list); | ||
} | ||
List<Integer> l1 = map.get(word1); | ||
List<Integer> l2 = map.get(word2); | ||
int min = Integer.MAX_VALUE; | ||
for (int a : l1) { | ||
for (int b : l2) { | ||
min = Math.min(Math.abs(b - a), min); | ||
} | ||
} | ||
return min; | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
http://sbzhouhao.net/LeetCode/LeetCode-Shortest-Word-Distance.html | ||
http://www.cnblogs.com/jcliBlogger/p/4704962.html | ||
*/ |
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,42 @@ | ||
/* | ||
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | ||
Write a function to determine if a number is strobogrammatic. The number is represented as a string. | ||
For example, | ||
the numbers "69", "88", and "818" are all strobogrammatic. | ||
*/ | ||
|
||
public class StrobogrammaticNumber { | ||
public boolean isStrobogrammatic(String num) { | ||
for (int i = 0; i <= num.length() / 2; i++) { | ||
char a = num.charAt(i); | ||
char b = num.charAt(num.length() - 1 - i); | ||
if (!isValid(a, b)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
private boolean isValid(char c, char b) { | ||
switch (c) { | ||
case '1': | ||
return b == '1'; | ||
case '6': | ||
return b == '9'; | ||
case '9': | ||
return b == '6'; | ||
case '8': | ||
return b == '8'; | ||
case '0': | ||
return b == '0'; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number.html | ||
*/ |
File renamed without changes.
File renamed without changes.
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,17 @@ | ||
/* | ||
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. | ||
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. | ||
Note: | ||
All costs are positive integers. | ||
Follow up: | ||
Could you solve it in O(nk) runtime? | ||
*/ | ||
|
||
/* | ||
Referene: | ||
https://leetcode.com/discuss/52964/my-accept-java-o-nk-solution | ||
https://leetcode.com/discuss/52969/java-o-nkk-and-improved-o-nk-solution | ||
http://www.meetqun.com/thread-10660-1-1.html | ||
*/ |
File renamed without changes.
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,122 @@ | ||
/* | ||
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | ||
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. | ||
For example, | ||
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers. | ||
Note: | ||
Because the range might be a large number, the low and high numbers are represented as string. | ||
*/ | ||
|
||
public class StrobogrammaticNumberIII { | ||
private char[] validNumbers = new char[]{'0', '1', '6', '8', '9'}; | ||
private char[] singleable = new char[]{'0', '1', '8'}; | ||
|
||
public int strobogrammaticInRange(String low, String high) { | ||
assert low != null && high != null; | ||
int ll = low.length(); | ||
int hl = high.length(); | ||
int result = 0; | ||
if(ll > hl || (ll == hl&&low.compareTo(high) > 0)) { | ||
return 0; | ||
} | ||
List<String> list = findStrobogrammatic(ll); | ||
if (ll == hl) { | ||
for (String s : list) { | ||
if (s.compareTo(low) >= 0 && s.compareTo(high) <= 0) { | ||
result++; | ||
} | ||
if (s.compareTo(high) > 0) { | ||
break; | ||
} | ||
} | ||
} else { | ||
for (int i = list.size() - 1; i >= 0; i--) { | ||
String s = list.get(i); | ||
if (s.compareTo(low) >= 0) { | ||
result++; | ||
} | ||
if (s.compareTo(low) < 0) { | ||
break; | ||
} | ||
} | ||
list = findStrobogrammatic(hl); | ||
for (String s : list) { | ||
if (s.compareTo(high) <= 0) { | ||
result++; | ||
} | ||
if (s.compareTo(high) > 0) { | ||
break; | ||
} | ||
} | ||
for (int i = ll + 1; i < hl; i++) { | ||
result += findStrobogrammatic(i).size(); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public List<String> findStrobogrammatic(int n) { | ||
assert n > 0; | ||
List<String> result = new ArrayList<>(); | ||
if (n == 1) { | ||
for (char c : singleable) { | ||
result.add(String.valueOf(c)); | ||
} | ||
return result; | ||
} | ||
if (n % 2 == 0) { | ||
helper(n, new StringBuilder(), result); | ||
} else { | ||
helper(n - 1, new StringBuilder(), result); | ||
List<String> tmp = new ArrayList<>(); | ||
for (String s : result) { | ||
for (char c : singleable) { | ||
tmp.add(new StringBuilder(s).insert(s.length() / 2, c).toString()); | ||
} | ||
} | ||
result = tmp; | ||
} | ||
return result; | ||
} | ||
|
||
private void helper(int n, StringBuilder sb, List<String> result) { | ||
if (sb.length() > n) return; | ||
if (sb.length() == n) { | ||
if (sb.length() > 0 && sb.charAt(0) != '0') { | ||
result.add(sb.toString()); | ||
} | ||
return; | ||
} | ||
for (char c : validNumbers) { | ||
StringBuilder tmp = new StringBuilder(sb); | ||
String s = "" + c + findMatch(c); | ||
tmp.insert(tmp.length() / 2, s); | ||
helper(n, tmp, result); | ||
} | ||
} | ||
|
||
private char findMatch(char c) { | ||
switch (c) { | ||
case '1': | ||
return '1'; | ||
case '6': | ||
return '9'; | ||
case '9': | ||
return '6'; | ||
case '8': | ||
return '8'; | ||
case '0': | ||
return '0'; | ||
default: | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
/* | ||
Reference: | ||
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number-III.html | ||
*/ |
Oops, something went wrong.