哈希表篇 字母异味,数组交集,数字,字符串 哈希表 字母异位 242.有效的字母异位 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Solution { public boolean isAnagram (String s, String t) { if (s.length() != t.length()) { return false ; } HashMap<Character, Integer> map = new HashMap <>(); for (int i = 0 ; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0 ) + 1 ); } for (int i = 0 ; i < t.length(); i++) { char c = t.charAt(i); map.put(c, map.getOrDefault(c, 0 ) - 1 ); if (map.get(c) == 0 ) { map.remove(c); } } return map.size() == 0 ? true : false ; } } class Solution { public boolean isAnagram (String s, String t) { if (s.length() != t.length()) { return false ; } char [] str1 = s.toCharArray(); char [] str2 = t.toCharArray(); Arrays.sort(str1); Arrays.sort(str2); return Arrays.equals(str1, str2); } }
383.赎金信 s字符串是否能由t构成,s中字符只能在t中使用1次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public boolean canConstruct (String s, String t) { int lens = s.length(); int lent = t.length(); if (lens > lent) { return false ; } HashMap<Character, Integer> map = new HashMap <>(); for (int i = 0 ; i < lent; i++) { char c = t.charAt(i); map.put(c, map.getOrDefault(c, 0 ) + 1 ); } for (int i = 0 ; i < lens; i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0 ) - 1 ); if (map.get(c) < 0 ) { return false ; } } return true ; } }
49.字母异位词分组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public List<List<String>> groupAnagrams (String[] strs) { HashMap<String, List<String>> map = new HashMap <>(); for (String s : strs) { char [] array = s.toCharArray(); Arrays.sort(array); String key = new String (array); List<String> value = map.getOrDefault(key, new ArrayList <String>()); value.add(s); map.put(key, value); } return new ArrayList <>(map.values()); } }
438.找到字符串中的所有字母异位词 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Solution { public List<Integer> findAnagrams (String s, String p) { List<Integer> list = new ArrayList <>(); int lens = s.length(); int lenp = p.length(); if (lens < lenp) { return list; } char [] arr = p.toCharArray(); Arrays.sort(arr); p = new String (arr); for (int i = 0 ; i <= lens - lenp; i++) { String str = s.substring(i, i + lenp); if (isAnagram(str, p) == true ) { list.add(i); } } return list; } public boolean isAnagram (String s, String t) { if (s.length() != t.length()) { return false ; } char [] str1 = s.toCharArray(); char [] str2 = t.toCharArray(); Arrays.sort(str1); return Arrays.equals(str1, str2); } }
数组交集 349.两个数组的交集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution { public int [] intersection(int [] nums1, int [] nums2) { if (nums1.length == 0 || nums2.length == 0 ){ return new int [0 ]; } HashSet<Integer> set1 = new HashSet <>(); HashSet<Integer> set2 = new HashSet <>(); for (int i : nums1){ set1.add(i); } for (int i : nums2){ if (set1.contains(i)){ set2.add(i); } } int [] ans = new int [set2.size()]; int j = 0 ; for (int i : set2){ ans[j++] = i; } return ans; } }
350.两个数组的交集II 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution { public int [] intersect(int [] nums1, int [] nums2) { HashMap<Integer, Integer> map = new HashMap <>(); ArrayList<Integer> list = new ArrayList <>(); for (int i : nums1) { map.put(i, map.getOrDefault(i, 0 ) + 1 ); } for (int i : nums2) { if (map.containsKey(i) && map.get(i) > 0 ) { list.add(i); map.put(i, map.get(i) - 1 ); } } int [] ans = new int [list.size()]; int j = 0 ; for (int i : list) { ans[j++] = i; } return ans; } }
数字 202.快乐数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public boolean isHappy (int n) { HashSet<Integer> set = new HashSet <>(); while (n != 1 && !set.contains(n)) { set.add(n); n = nextNumber(n); } return n == 1 ; } public int nextNumber (int n) { int sum = 0 ; while (n != 0 ) { sum += Math.pow(n % 10 , 2 ); n /= 10 ; } return sum; } }
1.两数之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public int [] twoSum(int [] nums, int target) { int n = nums.length; HashMap<Integer, Integer> map = new HashMap <>(); for (int i = 0 ; i < n; i++) { if (map.containsKey(target - nums[i])) { return new int [] { i, map.get(target - nums[i]) }; } map.put(nums[i], i); } return new int [0 ]; } }
454.四数相加 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution { public int fourSumCount (int [] nums1, int [] nums2, int [] nums3, int [] nums4) { HashMap<Integer, Integer> map = new HashMap <>(); for (int i : nums1) { for (int j : nums2) { map.put(i + j, map.getOrDefault(i + j, 0 ) + 1 ); } } int count = 0 ; for (int i : nums3) { for (int j : nums4) { count += map.getOrDefault(-i - j, 0 ); } } return count; } }
383.赎金信
15.三数之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 class Solution { public List<List<Integer>> threeSum (int [] nums) { int n = nums.length; List<List<Integer>> ans = new ArrayList <>(); if (nums == null || n < 3 ) { return ans; } HashMap<Integer, Integer> map = new HashMap <>(); Arrays.sort(nums); for (int i : nums) { map.put(i, map.getOrDefault(i, 0 ) + 1 ); } for (int i = 0 ; i < n - 2 ; i++) { if (i > 0 && nums[i] == nums[i - 1 ]) { continue ; } for (int j = i + 1 ; j < n - 1 ; j++) { int target = -nums[i] - nums[j]; if (map.containsKey(target)) { if (target == nums[i] || target == nums[j]) { if (map.get(target) < 2 ) { continue ; } } if (target == nums[i] && target == nums[j]) { if (map.get(target) < 3 ) { continue ; } } List<Integer> list = new ArrayList <>(); list = Arrays.asList(nums[i], nums[j], target); Collections.sort(list); ans.add(list); } } } Set<List<Integer>> set = new HashSet <>(ans); return new ArrayList <>(set); } } class Solution { public List<List<Integer>> threeSum (int [] nums) { List<List<Integer>> result = new ArrayList <>(); Arrays.sort(nums); for (int i = 0 ; i < nums.length; i++) { if (nums[i] > 0 ) { return result; } if (i > 0 && nums[i] == nums[i - 1 ]) { continue ; } int left = i + 1 ; int right = nums.length - 1 ; while (right > left) { int sum = nums[i] + nums[left] + nums[right]; if (sum > 0 ) { right--; } else if (sum < 0 ) { left++; } else { result.add(Arrays.asList(nums[i], nums[left], nums[right])); while (right > left && nums[right] == nums[right - 1 ]) { right--; } while (right > left && nums[left] == nums[left + 1 ]) { left++; } right--; left++; } } } return result; } }
18.四数之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 class Solution { public List<List<Integer>> fourSum (int [] nums, int target) { int n = nums.length; List<List<Integer>> ans = new ArrayList <>(); if (nums == null || nums.length < 4 ) { return ans; } Arrays.sort(nums); for (int i = 0 ; i < n - 3 ; i++) { if (i > 0 && nums[i] == nums[i - 1 ]) { continue ; } for (int j = i + 1 ; j < n - 2 ; j++) { if (j > i + 1 && nums[j] == nums[j - 1 ]) { continue ; } int left = j + 1 ; int right = n - 1 ; while (left < right) { long sum = (long ) nums[i] + nums[j] + nums[left] + nums[right]; if (sum > target) { right--; } else if (sum < target) { left++; } else { List<Integer> list = new ArrayList <>(); list = Arrays.asList(nums[i], nums[j], nums[left], nums[right]); ans.add(list); while (right > left && nums[right] == nums[right - 1 ]) { right--; } while (right > left && nums[left] == nums[left + 1 ]) { left++; } right--; left++; } } } } return ans; } }
字符串 344.反转字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public void reverseString (char [] s) { int n = s.length; int i = 0 , j = n - 1 ; while (i < j) { char t = s[i]; s[i] = s[j]; s[j] = t; i++; j--; } } }
541.反转字符串II 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution { public String reverseStr (String s, int k) { int n = s.length(); int i = 0 ; char [] arr = s.toCharArray(); while (i < n) { int r = Math.min(i + k - 1 , n - 1 ); arr = subReverse(arr, i, r); i = i + 2 * k; } return new String (arr); } public char [] subReverse(char [] arr, int i, int j) { while (i < j) { char t = arr[i]; arr[i] = arr[j]; arr[j] = t; i++; j--; } return arr; } }
替换数字 将字符串中的数字替换为字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.util.*;public class Main { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); String s = scanner.next(); StringBuilder sb = new StringBuilder (); for (int i = 0 ; i < s.length(); i++) { if (s.charAt(i) >= '0' && s.charAt(i) <= '9' ) { sb.append("number" ); } else { sb.append(s.charAt(i)); } } System.out.println(sb); } }
151.反转字符串中的单词 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public String reverseWords (String s) { s = s.trim(); int n = s.length(); int i = n - 1 , j = n - 1 ; StringBuilder ans = new StringBuilder (); while (i >= 0 ) { while (i >= 0 && s.charAt(i) != ' ' ) { i--; } String sub = s.substring(i + 1 , j + 1 ) + " " ; ans.append(sub); while (i >= 0 && s.charAt(i) == ' ' ) { i--; } j = i; } return ans.toString().trim(); } }
右旋字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.*;class Main { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int k = scan.nextInt(); String s = scan.next(); char arr[] = s.toCharArray(); int n = arr.length; char [] ans = new char [n]; int j = 0 ; for (int i = n - k; i < n; i++) { ans[j++] = arr[i]; } for (int i = 0 ; i < n - k; i++) { ans[j++] = arr[i]; } System.out.println(ans); } }
28.找出字符串中第一个匹配的下标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public int strStr (String h, String n) { int lenh = h.length(); int lenn = n.length(); for (int i = 0 ; i <= lenh - lenn; i++) { int a = i, b = 0 ; while (b < lenn && h.charAt(a) == n.charAt(b)) { a++; b++; } if (b == lenn) { return i; } } return -1 ; } }
459.重复的字串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public boolean repeatedSubstringPattern (String s) { int n = s.length(); for (int i = 1 ; i <= n / 2 ; i++) { if (n % i == 0 ) { String sub = s.substring(0 , i); StringBuilder sb = new StringBuilder (); for (int j = 1 ; j <= n / i; j++) { sb.append(sub); } if (sb.toString().equals(s)) { return true ; } } } return false ; } }