大奋
算法一坨,学习一下,主要跟着这个仓库刷。
leetcode 242 有效的字母异位词
哈希表。JAVA中的char居然是两个字节的。默认是UTF-16字符,夸脏哦。
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Integer> hashMap = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i ++) {
char ch = s.charAt(i);
hashMap.put(ch, hashMap.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < t.length(); i ++) {
char ch = t.charAt(i);
hashMap.put(ch, hashMap.getOrDefault(ch, 0) - 1);
if (hashMap.get(ch) < 0) {
return false;
}
}
return true;
}
}
leetcode 1002 查找共用字符
对每个字符串做字符频率统计。再维护一个最小字符频率的minFreq数组,记录当前所处理过的字符串中,a-z中每个字符共同出现过的最小次数。
class Solution {
public List<String> commonChars(String[] words) {
int[] minFreq = new int[26];
int[] freq = new int[26];
Arrays.fill(minFreq, Integer.MAX_VALUE);
Arrays.fill(freq, 0);
for (String word : words) {
for (int i = 0; i < word.length(); i ++) {
freq[word.charAt(i) - 'a'] += 1;
}
for (int i = 0; i < freq.length; i ++) {
minFreq[i] = Math.min(minFreq[i], freq[i]);
}
Arrays.fill(freq, 0);
}
List<String> result = new ArrayList<String>();
for (int i = 0; i < minFreq.length; i ++) {
while (minFreq[i] > 0) {
result.add(String.valueOf((char) (i + 'a')));
minFreq[i] --;
}
}
return result;
}
}
leetcode 349 两个数组的交集
和上一题一样,统计数字频率
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] freq = new int[1001];
int[] minFreq = new int[1001];
Arrays.fill(freq, 0);
Arrays.fill(minFreq, Integer.MAX_VALUE);
ArrayList<int[]> arrays = new ArrayList<int[]>();
arrays.add(nums1);
arrays.add(nums2);
for (int[] array : arrays) {
for (int i = 0; i < array.length; i ++) {
freq[array[i]] += 1;
}
for (int i = 0; i < minFreq.length; i ++) {
minFreq[i] = Math.min(minFreq[i], freq[i]);
}
Arrays.fill(freq, 0);
}
int[] result = new int[1001];
int j = 0;
for (int i = 0; i < minFreq.length; i ++) {
if (minFreq[i] > 0) {
result[j ++] = i;
}
}
return Arrays.copyOfRange(result, 0, j);
}
}
如果没限制数字范围的话,就用HashSet做
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<Integer>();
Set<Integer> resultSet = new HashSet<Integer>();
for (int x : nums1) {
set.add(x);
}
for (int x : nums2) {
if (set.contains(x)) {
resultSet.add(x);
}
}
int[] result = new int[resultSet.size()];
int j = 0;
for (int x : resultSet) {
result[j ++] = x;
}
return result;
}
}
leetcode 202 快乐数
哈希表判断有没有1,有的话退出循环,返回true,没有的话就算下一个平方和,再判断是不是在set里,在的话说明循环了,返回false。官方的快慢指针和数学方法想不到,之后二刷再说吧。
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<Integer>();
while (!set.contains(1)) {
n = computeSquareSum(n);
if (set.contains(n)) {
return false;
}
set.add(n);
}
return true;
}
private int computeSquareSum(int x) {
int squareSum = 0;
while (x > 0) {
squareSum += (x % 10) * (x % 10);
x = x / 10;
}
return squareSum;
}
}
leetcode 1 两数之和
经典中的经典了,当初第一次写leetcode的时候,这题就写了好久。
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i ++) {
if (map.containsKey(target - nums[i])) {
return new int[]{i, map.get(target - nums[i])};
}
map.put(nums[i], i);
}
return null;
}
}
leetcode 454 四数相加 II
分治加哈希。可以拓展到n数相加。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int a : nums1) {
for (int b : nums2) {
map.put(a + b, map.getOrDefault(a + b, 0) + 1);
}
}
int result = 0;
for (int c : nums3) {
for (int d : nums4) {
if (map.containsKey(- c - d)) {
result += map.get(- c - d);
}
}
}
return result;
}
}
leetcode 383 赎金信
写的有点重量级了,如果只有字母的话,开一个char[]当作哈希表就行了
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < magazine.length(); i ++) {
map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) + 1);
}
for (int i = 0; i < ransomNote.length(); i ++) {
if (map.containsKey(ransomNote.charAt(i)) && map.get(ransomNote.charAt(i)) > 0) {
map.put(ransomNote.charAt(i), map.get(ransomNote.charAt(i)) - 1);
}
else {
return false;
}
}
return true;
}
}
leetcode 15 三数之和
如果在找满足和为0的k之后,就往左去除重复nums[k]时,会导致nums[j] == nums[k],这时候会把这样的j排除掉。所以要在找到三数之和之后再去除重复nums[k]
比如[-8, -5, 4, 4, 4, 4, 4]
nums[i] == -8, nums[j] == -5, nums[k] == 4, 这时候直接去除k的重复,k的索引就到2了,这时候就无解了,所以需要找到三数之和才能对nums[k]去重
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length - 2; i ++) {
if (i - 1 >= 0 && nums[i] == nums[i - 1]) { // 如果跟上一个重复,就跳过,i ++
continue;
}
int k = nums.length - 1;
for (int j = i + 1; j < nums.length - 1; j ++) {
if (j - 1 > i && nums[j] == nums[j - 1]) {
continue;
}
while (k > j && nums[i] + nums[j] + nums[k] > 0) {
k --;
}
if (k == j) {
break;
}
if (nums[i] + nums[j] + nums[k] == 0) {
result.add(List.of(nums[i], nums[j], nums[k]));
// 找到了再排除重复的nums[k]
while (k - 1 > j && nums[k - 1] == nums[k]) {
k --;
}
}
}
}
return result;
}
}
leetcode 18 四数之和
和上一题一样,就是多一层循环
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length - 3; i ++) {
if (i - 1 >= 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j ++) {
if (j - 1 > i && nums[j] == nums[j - 1]) {
continue;
}
int m = nums.length - 1;
for (int k = j + 1; k < nums.length - 1; k ++) {
if (k - 1 > j && nums[k] == nums[k - 1]) {
continue;
}
while (m > k && (long) nums[i] + nums[j] + nums[k] + nums[m] > target) {
m --;
}
if (m == k) {
break;
}
if (nums[i] + nums[j] + nums[k] + nums[m] == target) {
result.add(List.of(nums[i], nums[j], nums[k], nums[m]));
// 找到了再排除重复的nums[m]
while (m - 1 > k && nums[m - 1] == nums[m]) {
m --;
}
}
}
}
}
return result;
}
}
哈希表这一块好像还算比较简单的
1万+




被折叠的 条评论
为什么被折叠?



