classSolution{ publicint[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { returnnewint[] { map.get(complement), i }; } map.put(nums[i], i); } thrownew IllegalArgumentException("No two sum solution"); } }
public String longestPalindrome(String s){ if (s == null || s.length() < 1) return""; int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); }
privateintexpandAroundCenter(String s, int left, int right){ int L = left, R = right; while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { L--; R++; } return R - L - 1; }
// Hash table that takes care of the mappings. private HashMap<Character, Character> mappings;
// Initialize hash map with mappings. This simply makes the code easier to read. publicSolution(){ this.mappings = new HashMap<Character, Character>(); this.mappings.put(')', '('); this.mappings.put('}', '{'); this.mappings.put(']', '['); }
publicbooleanisValid(String s){
// Initialize a stack to be used in the algorithm. Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) { char c = s.charAt(i);
// If the current character is a closing bracket. if (this.mappings.containsKey(c)) {
// Get the top element of the stack. If the stack is empty, set a dummy value of '#' char topElement = stack.empty() ? '#' : stack.pop();
// If the mapping for this bracket doesn't match the stack's top element, return false. if (topElement != this.mappings.get(c)) { returnfalse; } } else { // If it was an opening bracket, push to the stack. stack.push(c); } }
// If the stack still contains elements, then it is an invalid expression. return stack.isEmpty(); } }
import java.util.Random; classSolution{ int [] nums;
publicvoidswap(int a, int b){ int tmp = this.nums[a]; this.nums[a] = this.nums[b]; this.nums[b] = tmp; }
publicintpartition(int left, int right, int pivot_index){ int pivot = this.nums[pivot_index]; // 1. move pivot to end swap(pivot_index, right); int store_index = left;
// 2. move all smaller elements to the left for (int i = left; i <= right; i++) { if (this.nums[i] < pivot) { swap(store_index, i); store_index++; } }
// 3. move pivot to its final place swap(store_index, right);
return store_index; }
publicintquickselect(int left, int right, int k_smallest){ /* Returns the k-th smallest element of list within left..right. */
if (left == right) // If the list contains only one element, returnthis.nums[left]; // return that element
// select a random pivot_index Random random_num = new Random(); int pivot_index = left + random_num.nextInt(right - left); pivot_index = partition(left, right, pivot_index);
// the pivot is on (N - k)th smallest position if (k_smallest == pivot_index) returnthis.nums[k_smallest]; // go left side elseif (k_smallest < pivot_index) return quickselect(left, pivot_index - 1, k_smallest); // go right side return quickselect(pivot_index + 1, right, k_smallest); }
publicintfindKthLargest(int[] nums, int k){ this.nums = nums; int size = nums.length; // kth largest is (N - k)th smallest return quickselect(0, size - 1, size - k); } }
// Inserts a word into the trie. publicvoidinsert(String word){ TrieNode node = root; for (int i = 0; i < word.length(); i++) { char currentChar = word.charAt(i); if (!node.containsKey(currentChar)) { node.put(currentChar, new TrieNode()); } node = node.get(currentChar); } node.setEnd(); }
// search a prefix or whole key in trie and // returns the node where search ends private TrieNode searchPrefix(String word){ TrieNode node = root; for (int i = 0; i < word.length(); i++) { char curLetter = word.charAt(i); if (node.containsKey(curLetter)) { node = node.get(curLetter); } else { returnnull; } } return node; }
// Returns if the word is in the trie. publicbooleansearch(String word){ TrieNode node = searchPrefix(word); return node != null && node.isEnd(); } }