-
Notifications
You must be signed in to change notification settings - Fork 0
LeetCode17 Done #61
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
base: master
Are you sure you want to change the base?
LeetCode17 Done #61
Conversation
| digitMap.put(6, "mno"); | ||
| digitMap.put(7, "pqrs"); | ||
| digitMap.put(8, "tuv"); | ||
| digitMap.put(9, "wxyz"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually for this kind of objects we'll make it a static object and construct it within a static class.
The reason is that you'll probably be able to reuse the same object at many places but apparently you don't want to construct it again and again.
Test1/src/leetcode/LeetCode17.java
Outdated
|
|
||
| int digitsLength = digits.length(); | ||
|
|
||
| String[] str = new String[digitsLength]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String[] str = new String[digits.length()];
You don't need the variable digitsLength.
digits.length() is very trivial and straightforward.
| loop--; | ||
| } | ||
|
|
||
| return list; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take a look into this snippet, I wrote this based on your code:
class Solution {
public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
if(digits.isEmpty()) return list;
Map<Integer, String> digitMap = new HashMap<Integer, String>();
digitMap.put(2, "abc");
digitMap.put(3, "def");
digitMap.put(4, "ghi");
digitMap.put(5, "jkl");
digitMap.put(6, "mno");
digitMap.put(7, "pqrs");
digitMap.put(8, "tuv");
digitMap.put(9, "wxyz");
int digitsLength = digits.length();
list.add("");
for(int i = 0; i < digits.length(); i++) {
String words = digitMap.get(Integer.valueOf(String.valueOf(digits.charAt(i))));
List<String> newList = new ArrayList<String>();
for(String s: list) {
for(char c: words.toCharArray()) {
newList.add(s+c);
}
}
list = newList;
}
return list;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow. This make me overwhelmed.
So brilliant! Thank you.
Time Complexity : O(N...)?