Hashtable
1. Intersection of two arrays
一个hash表存一数组,一hash表存结果
1 public int[] intersection(int[] nums1, int[] nums2) { 2 // Write your code here 3 HashSet<Integer> set1 = new HashSet<>(); 4 for (int i = 0; i < nums1.length; i++) 5 { 6 set1.add(nums1[i]); 7 } 8 HashSet<Integer> set2 = new HashSet<>(); 9 for (int i = 0; i < nums2.length; i++) 10 { 11 if (set1.contains(nums2[i]) && !set2.contains(nums2[i])) 12 { 13 set2.add(nums2[i]); 14 } 15 } 16 int[] res = new int[set2.size()]; 17 int index = 0; 18 for (int i : set2) 19 { 20 res[index++] = i; 21 } 22 return res; 23 }
2. Intersection of two arrays II 数的个数也算
找map记录一的个数,到2如有减去后添加到列表
1 public int[] intersection(int[] nums1, int[] nums2) { 2 // Write your code here 3 HashMap<Integer, Integer> map1 = new HashMap<>(); 4 for (int i = 0; i < nums1.length; i++) 5 { 6 map1.put(nums1[i], map1.containsKey(nums1[i]) ? map1.get(nums1[i]) + 1: 1); 7 } 8 List<Integer> res = new ArrayList<>(); 9 for (int i = 0; i < nums2.length; i++) 10 { 11 if (map1.containsKey(nums2[i]) && map1.get(nums2[i]) > 0) 12 { 13 res.add(nums2[i]); 14 map1.put(nums2[i],map1.get(nums2[i]) - 1); 15 } 16 } 17 int[] item = new int[res.size()]; 18 for (int i = 0; i < res.size(); i++) { 19 item[i] = res.get(i); 20 } 21 return item; 22 }
3.Huppy Numbera
如果是,则必重复,快慢指针
public boolean isHappy(int n) { // Write your code here if (n <= 0) { return false; } int fast = n; int slow = n; do { slow = calc(slow); fast = calc(fast); fast = calc(fast); if (fast == 1) { return true; } }while (fast != slow); return false; } public int calc(int n) { int sum = 0; while (n != 0) { sum += (n % 10) * (n % 10); n /= 10; } return sum; }
4. Max poins on line
public int maxPoints(Point[] points) { if (points == null|| points.length == 0) { return 0; } int max = 1; double radio = 0.0; for (int i = 0; i < points.length - 1; i++) { int numofsame = 1; int localmax = 0; HashMap<Double, Integer> map = new HashMap<>(); for (int j = i + 1; j < points.length; j++) { if (points[i].x == points[j].x && points[i].y == points[j].y) { numofsame ++; continue; } else if(points[i].x == points[j].x) { radio = Double.MAX_VALUE; } else if (points[i].y == points[j].y) { radio = 0.0; } else { radio = (double)(points[i].y - points[j].y) / (double)(points[i].x - points[j].x); } map.put(radio, map.containsKey(radio) ? map.get(radio) + 1 : 1); } for (Integer num : map.values()) { if (localmax < num) { localmax = num; } } localmax = localmax + numofsame; max = localmax > max ? localmax : max; } return max; }
5 Longest subString without Repeating Character
记录左右边界,最大长度,和里面的字母
public int lengthOfLongestSubstring(String s) { if(s==null || s.length()==0) return 0; HashSet<Character> set = new HashSet<Character>(); int max = 0; int walker = 0; int runner = 0; while(runner<s.length()) { if(set.contains(s.charAt(runner))) { if(max<runner-walker) { max = runner-walker; } while(s.charAt(walker)!=s.charAt(runner)) { set.remove(s.charAt(walker)); walker++; } walker++; } else { set.add(s.charAt(runner)); } runner++; } max = Math.max(max,runner-walker); return max; }
public int lengthOfLongestSubstring(String s) { if(s==null || s.length()==0) return 0; int[] res = new int[256]; int i = 0; int j = 0; int max = 0; for (i = 0; i < s.length(); i++) { while (j < s.length() && res[s.charAt(j)] == 0) { max = Math.max(max, j - i + 1); res[s.charAt(j)] = 1; j++; } res[s.charAt(i)] = 0; } return max; }
6 Anagrams
hashMap 记录有相同字符的 序列
public ArrayList<String> anagrams(String[] strs) { ArrayList<String> res = new ArrayList<String>(); if (strs == null || strs.length == 0) { return res; } HashMap<String, ArrayList<String>> map = new HashMap<>(); for (int i = 0; i < strs.length; i++) { char[] item = strs[i].toCharArray(); Arrays.sort(item); String str = new String(item); if (map.containsKey(str)) { ArrayList<String> list = map.get(str); list.add(strs[i]); } else { ArrayList<String> list = new ArrayList<String>(); list.add(strs[i]); map.put(str,list); } } for (ArrayList<String> list: map.values()) { if (list.size() > 1) { res.addAll(list); } } return res; }
7,Subarray Sum
hashMap 记录和 和坐标 找相同返回,更新hashmap
public ArrayList<Integer> subarraySum(int[] nums) { // write your code here ArrayList<Integer> res = new ArrayList<>(); HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, -1); int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; if (map.containsKey(sum)) { res.add(map.get(sum) + 1); res.add(i); return res; } map.put(sum, i); } return res; }
8, Rehashing
按照原数组遍历,如到一个不为null,继续next
public ListNode[] rehashing(ListNode[] hashTable) { // write your code here if (hashTable == null || hashTable.length == 0) { return hashTable; } int newLen = hashTable.length * 2; ListNode[] newTable = new ListNode[newLen]; for (int i = 0; i < hashTable.length; i++) { while (hashTable[i] != null) { int newIndex = (hashTable[i].val % newLen + newLen) % newLen; if (newTable[newIndex] == null) { ListNode item = new ListNode(hashTable[i].val); newTable[newIndex] = item; }else { ListNode item = newTable[newIndex]; while (item.next != null) { item = item.next; } item.next = new ListNode(hashTable[i].val); } hashTable[i] = hashTable[i].next; } } return newTable; }
9.Hash Function
按哈希函数做, long 避免溢出
public int hashCode(char[] key,int HASH_SIZE) { // write your code here long res = 0; for (int i = 0; i < key.length; i++) { res = (res * 33 + key[i]) % HASH_SIZE; } return (int)res; }
10. Copy List with Random Pointer
一原一副,连随便,切开 切开错
public RandomListNode copyRandomList(RandomListNode head) { if (head == null) { return head; } RandomListNode help = head; while (help != null) { RandomListNode item = new RandomListNode(help.label); item.next = help.next; help.next = item; help = item.next; } help = head; while (help != null) { if (help.random != null) { help.next.random = help.random.next; } help = help.next.next; } RandomListNode newHead = head.next; help = head; while (help != null) { RandomListNode node = help.next; help.next = node.next; if (node.next != null) { node.next = node.next.next; } help = help.next; } return newHead; }
11.Two Sum
数组两值,hashMap 记录元素
public int[] twoSum(int[] numbers, int target) { if (numbers == null || numbers.length < 2) { return null; } int[] res = new int[2]; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { res[0] = map.get(target - numbers[i]) + 1; res[1] = i + 1; return res; } map.put(numbers[i], i); } return res; }
12. Majority Number III
建k-1hashmap ,如多删, 找到值,
public int majorityNumber(ArrayList<Integer> nums, int k) { // write your code HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int e : nums) { map.put(e, map.containsKey(e)? map.get(e)+1:1); if (map.size() >= k) { remove(map); } } if (map.size() == 0) { return Integer.MIN_VALUE; } for (int e: map.keySet()) { map.put(e,0); } for (int e : nums) { if (map.containsKey(e)) { map.put(e, map.get(e) + 1); } } int max = 0; int num = 0; for(int e : map.keySet()) { if (max < map.get(e)) { max = map.get(e); num = e; } } return num; } public void remove(HashMap<Integer, Integer> map) { ArrayList<Integer> list = new ArrayList<>(); for(int e : map.keySet()) { if (map.get(e) == 1) { list.add(e); } } for (int e : list) { map.remove(e); } }
13 Minimum Window Substring
维护字典。个数,如有,计算个数,如等,左移,
public String minWindow(String s, String t) { // write your code if (s == null || s.length() == 0) { return ""; } //把t中的元素放入字典中 HashMap<Character, Integer> hm = new HashMap<Character, Integer>(); for (int i = 0; i < t.length(); i++) { char c = t.charAt(i); hm.put(c,hm.containsKey(c)? hm.get(c)+1:1); } int count = 0; int l = 0; int r = 0; int minL = s.length() + 1; int minStart = 0; for (r = 0; r < s.length(); r++) { if (hm.containsKey(s.charAt(r))) { hm.put(s.charAt(r), hm.get(s.charAt(r)) - 1); if (hm.get(s.charAt(r)) >= 0) { count ++; } while (count == t.length()) { if (minL > r - l + 1) { minL = r - l + 1; minStart = l; } if (hm.containsKey(s.charAt(l))) { hm.put(s.charAt(l), hm.get(s.charAt(l)) + 1); if (hm.get(s.charAt(l)) > 0) { count --; } } l++; } } } if (minL > s.length()) { return ""; } return s.substring(minStart, minStart + minL); }