3、哈希表
1、有效的字母异位词
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
int[] freq_s = new int[26];
int[] freq_t = new int[26];
for (char c : s.toCharArray()) freq_s[c - 'a']++;
for (char c : t.toCharArray()) freq_t[c - 'a']++;
for (int i = 0; i < 26; i++) {
if (freq_s[i] != freq_t[i]) return false;
}
return true;
}
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
freq[t.charAt(i) - 'a']--;
}
for (int i : freq) {
if (i != 0) return false;
}
return true;
}
2、两个数组的交集
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
for (int i : nums1) set.add(i);
Set<Integer> res = new HashSet<>();
for (int i : nums2) {
if (set.contains(i)) res.add(i);
}
int[] ret = new int[res.size()];
int i = 0;
for (int num : res) ret[i++] = num;
return ret;
}
3、快乐数
public boolean isHappy(int n) {
// visit 中存放的是已经看过的数字
Set<Integer> visit = new HashSet<>();
while (n != 1 && !visit.contains(n)) {
visit.add(n);
n = getNextNumber(n);
}
return n == 1;
}
private int getNextNumber(int n) {
int sum = 0;
while (n != 0) {
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
4、两数之和
1 - 两数之和
返回的是索引
public int[] twoSum(int[] nums, int target) {
// num : index
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int need = target - nums[i];
if (map.containsKey(need)) return new int[]{i, map.get(need)};
map.put(nums[i], i);
}
throw new RuntimeException("无解");
}
5、四数相加 II
454 - 四数相加 II
返回和为 0 的个数
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
// sum : 个数
Map<Integer, Integer> map = new HashMap<>();
for (int i : nums1) {
for (int j : nums2) {
int sum = i + j;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
int res = 0;
for (int k : nums3) {
for (int l : nums4) {
int sum = k + l;
res += map.getOrDefault(-sum, 0);
}
}
return res;
}
6、赎金信
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) return false;
int[] freq_r = new int[26];
int[] freq_m = new int[26];
for (char c : ransomNote.toCharArray()) freq_r[c - 'a']++;
for (char c : magazine.toCharArray()) freq_m[c - 'a']++;
for (int i = 0; i < 26; i++) {
if (freq_r[i] > freq_m[i]) return false;
}
return true;
}
7、三数之和
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
// nums[i] + nums[p1] + nums[p2] = 0
// nums[i] <= nums[p1] <= nums[p2]
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) break;
if (i > 0 && nums[i] == nums[i - 1]) continue;
int p1 = i + 1;
int p2 = nums.length - 1;
while (p1 < p2) {
int v1 = nums[p1];
int v2 = nums[p2];
int sum = nums[i] + v1 + v2;
if (sum < 0) {
while (p1 < p2 && nums[p1] == v1) p1++;
} else if (sum > 0) {
while (p1 < p2 && nums[p2] == v2) p2--;
} else {
res.add(new ArrayList<Integer>(Arrays.asList(nums[i], v1, v2)));
while (p1 < p2 && nums[p1] == v1) p1++;
while (p1 < p2 && nums[p2] == v2) p2--;
}
}
}
return res;
}
8、四数之和
18 - 四数之和
返回和为 0 的四元组值
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
// nums[i] + nums[j] + nums[p1] + nums[p2] = target
// nums[i] <= nums[j] <= nums[p1] <= nums[p2]
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) continue;
int p1 = j + 1;
int p2 = nums.length - 1;
while (p1 < p2) {
long v1 = nums[p1];
long v2 = nums[p2];
long sum = nums[i] + nums[j] + v1 + v2;
if (sum < target) {
while (p1 < p2 && nums[p1] == v1) p1++;
} else if (sum > target) {
while (p1 < p2 && nums[p2] == v2) p2--;
} else {
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], (int) v1, (int) v2)));
while (p1 < p2 && nums[p1] == v1) p1++;
while (p1 < p2 && nums[p2] == v2) p2--;
}
}
}
}
return res;
}
public List<List<Integer>> fourSum(int[] nums, int target) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
// nums[i] + nums[j] + nums[p1] + nums[p2] = target
// nums[i] <= nums[j] <= nums[p1] <= nums[p2]
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
// 提前终止
if ((long) 4 * nums[i] > target) break;
if ((long) nums[i] + nums[n - 1] + nums[n - 2] + nums[n - 3] < target) continue;
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) continue;
// 提前终止
if ((long) 3 * nums[j] + nums[i] > target) break;
if ((long) nums[i] + nums[j] + nums[n - 1] + nums[n - 2] < target) continue;
int p1 = j + 1;
int p2 = nums.length - 1;
while (p1 < p2) {
long v1 = nums[p1];
long v2 = nums[p2];
long sum = nums[i] + nums[j] + v1 + v2;
if (sum < target) {
while (p1 < p2 && nums[p1] == v1) p1++;
} else if (sum > target) {
while (p1 < p2 && nums[p2] == v2) p2--;
} else {
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], (int) v1, (int) v2)));
while (p1 < p2 && nums[p1] == v1) p1++;
while (p1 < p2 && nums[p2] == v2) p2--;
}
}
}
}
return res;
}
本文来自博客园,作者:lidongdongdong~,转载请注明原文链接:https://www.cnblogs.com/lidong422339/p/17440646.html