This file contains hidden or 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
| public int[] twoSum(int[] numbers, int target) { | |
| int[] ans = new int[2]; | |
| HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); | |
| for (int i = 0; i < numbers.length; i++) { | |
| int num = numbers[i]; | |
| if (map.containsKey(num)) { | |
| ans[0] = map.get(num) + 1; | |
| ans[1] = i + 1; |
This file contains hidden or 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
| public int threeSumClosest(int[] num, int target) { | |
| http://www.programcreek.com/2013/02/leetcode-3sum-closest-java/ | |
| int min = Integer.MAX_VALUE; | |
| int result = 0; | |
| Arrays.sort(num); | |
| for (int i = 0; i < num.length; i++) { | |
| int start = i + 1; | |
| int end = num.length - 1; | |
This file contains hidden or 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
| public boolean isValid(String s) { | |
| //http://www.programcreek.com/2012/12/leetcode-valid-parentheses-java/ | |
| if (s == null || s.length() == 0) | |
| return false; | |
| Stack<Character> stk = new Stack<Character>(); | |
| HashMap<Character, Character> hMap = new HashMap<Character, Character>(); | |
| hMap.put('(', ')'); | |
| hMap.put('[', ']'); | |
| hMap.put('{', '}'); |
This file contains hidden or 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
| public ArrayList<String> anagrams(String[] strs) { | |
| //http://blog.csdn.net/linhuanmars/article/details/21664747 | |
| if (strs == null || strs.length == 0) | |
| return null; | |
| ArrayList<String> result = new ArrayList<String>(); | |
| HashMap<String, ArrayList<String>> hMap = new HashMap<String, ArrayList<String>>(); | |
| for (String s : strs) { | |
| char[] cStr = s.toCharArray(); |
This file contains hidden or 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
| public int longestValidParentheses(String s) { | |
| //http://rleetcode.blogspot.com/2014/01/longest-valid-parentheses.html | |
| if (s == null || s.length() < 2) | |
| return 0; | |
| int maxLen = 0; | |
| int last = -1; | |
| Stack<Integer> stk = new Stack<Integer>(); | |
| for (int i = 0; i < s.length(); i++) { |
This file contains hidden or 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
| public ArrayList<String> letterCombinations(String digits) { | |
| final String[] keypads = {"", "", "abc", "def", "ghi", "jkl", "mno", | |
| "pqrs", "tuv", "wxyz"}; | |
| ArrayList<String> result = new ArrayList<String>(); | |
| result.add(""); | |
| if (digits == null || digits.length() == 0) | |
| return result; | |
| for (int i = 0; i < digits.length(); i++) { |
This file contains hidden or 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
| public ArrayList<String> letterCombinations(String digits) { | |
| final String[] keypads = {"", "", "abc", "def", "ghi", "jkl", "mno", | |
| "pqrs", "tuv", "wxyz"}; | |
| ArrayList<String> result = new ArrayList<String>(); | |
| if (digits.equals("")) | |
| result.add(""); | |
| if (digits == null || digits.length() == 0) | |
| return result; | |
This file contains hidden or 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
| public void setZeroes(int[][] matrix) { | |
| if (matrix == null ) | |
| return; | |
| boolean firstRowZero = false; | |
| boolean firstColZero = false; | |
| int rowL = matrix.length; | |
| int colL = matrix[0].length; |
This file contains hidden or 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
| public class Solution { | |
| public String longestPalindrome(String s) { | |
| String longest = ""; | |
| for (int i = 0; i < s.length(); i++) { | |
| String temp = ""; | |
| temp = longestPalindromeHelper(s, i, i); | |
| if (temp.length() > longest.length()) |
This file contains hidden or 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
| //-------------------------solution1------------------------------------------------------- | |
| //http://blog.csdn.net/linhuanmars/article/details/22777711 | |
| public ArrayList<ArrayList<String>> partition(String s) { | |
| ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>(); | |
| if(s==null || s.length()==0) | |
| return res; | |
| helper(s, getDict(s),0,new ArrayList<String>(), res); | |
| return res; | |
| } | |
| private void helper(String s, boolean[][] dict, int start, ArrayList<String> item, ArrayList<ArrayList<String>> res) |