训练营练习-Day7&哈希2+双指针N数之和

Leetcode 454 力扣

解法:哈希判断四数之和的目标值减去两数之和是否包含,两数之和的变形题

public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
    Map<Integer,Integer> map = new HashMap<>();
    int res =0;
    for (int i = 0; i < nums1.length; i++) {
        for (int j = 0; j < nums2.length; j++) {
            int temp = nums1[i] +nums2[j]; // 两数之和先存入哈希
            if (map.containsKey(temp)){
                map.put(temp,map.get(temp) +1);
            }else {
                map.put(temp,1);
            }
        }
    }

    for (int i = 0; i < nums3.length; i++) {
        for (int j = 0; j < nums4.length; j++) {
            int temp = nums3[i] +nums4[j];
            if (map.containsKey(0 - temp)){ // 目标值减两数之和是否在哈希中
                res += map.get(0-temp); // 存在累计计数
            }
        }
    }
    return res;
}

Leetcode 383 力扣

解法:有效字母异位词变形题。在于哈希数组统计消除字符后count细节判断

public static boolean canConstruct(String ransomNote, String magazine) {
    int[] records = new int[26];
    for (int i = 0; i < ransomNote.length(); i++) { // count>0 说明ransomNote字符比magazine多
        records[ransomNote.charAt(i) - 'a']++;
    }
    for (int i = 0; i < magazine.length(); i++) {
        records[magazine.charAt(i) - 'a']--;
    }

    for (int count : records) {
        if (count > 0) { // 这里是要思考的点,count为0完全匹配,count>0和count<0看谁是++就是谁字符多
            return false;
        }
    }
    return true;
}

Leetcode 15 力扣

说实话这题之前做过两次,再次做还是自己AC不了,梦想走了。去重细节还需要再吃透。

下次做还是不会做,还是细节地方需要多练多巩固。

 

解法:固定一个数,然后使用双指针+剪枝去重

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    Arrays.sort(nums); // 先排序
    for (int i = 0; i < nums.length; i++) { // 固定i
        if (nums[i] > 0) {  // 大于0直接返回
            return result;
        }
        if (i > 0 && nums[i] == nums[i - 1]) { // nums[i] == nums[i + 1]会排除掉结果集[-1,-1,2]
            continue;
        }
        int left = i + 1;
        int right = nums.length - 1;
        while (right > left) { // 循环条件=号带不带,当left=right,只有两个元素
            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[left] == nums[left+1]){ // 后对left去重
                    left++;
                }
                while (right>left && nums[right] == nums[right-1]){ // 后对right去重
                    right--;
                }
                // left 和 right去重后,left和right继续移动收集下一个结果集
                right--;
                left++;
            }
        }
    }
    return result;
}

Leetcode 18 力扣

解法:三数之和变形题,target为可输入,考虑为负数情况,固定两个数+双指针+剪枝去重

 

public List<List<Integer>> fourSum(int[] nums, int target) { // target为输入值,存在负数情况
    Arrays.sort(nums); // 先排序
    List<List<Integer>> result =new ArrayList<>();
    for (int i = 0; i < nums.length; i++) { // 固定i
        if (nums[i] > target && nums[i] >0 && target>0){ // 剪枝操作,负数情况得保证排序后nums[i] >0才行
            return result;
        }
        if (i>0 && nums[i] == nums[i-1]){ // a去重
            continue;
        }
        for (int j = i+1; j < nums.length; j++) { // 固定j
            if (j >i+1 && nums[j] == nums[j-1]){ // b去重
                continue;
            }
            int left = j+1;
            int right = nums.length-1;
            while (right >left){ // 双指针移动
                long sum = nums[i] + nums[j] + nums[left] + nums[right];
                if (sum >target){
                    right--;
                } else if (sum <target) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right])); // 收集结果集
                    while (right >left && nums[left] == nums[left+1]){ // left去重
                        left++;
                    }
                    while (right>left && nums[right] == nums[right-1]){ // right去重
                        right--;
                    }
                    // left 和 right去重后,left和right继续移动收集下一个结果集
                    left++;
                    right--;
                }
            }
        }
    }
    return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈希表可以用来解决两之和的问题。在C语言中,我们可以使用哈希表来记录每个元素的值和对应的索引。然后遍历组,对于每个元素,我们可以检查哈希表中是否存在目标值减去当前元素的差值,如果存在,则找到了符合条件的两个。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int value; int index; } HashNode; int* twoSum(int* nums, int numsSize, int target, int* returnSize) { HashNode* hashTable = (HashNode*)malloc(numsSize * sizeof(HashNode)); int* result = (int*)malloc(2 * sizeof(int)); for (int i = 0; i < numsSize; i++) { hashTable[i].value = -1; hashTable[i].index = -1; } for (int i = 0; i < numsSize; i++) { int complement = target - nums[i]; int hashIndex = abs(complement) % numsSize; while (hashTable[hashIndex].value != -1 && hashTable[hashIndex].value != complement) { hashIndex = (hashIndex + 1) % numsSize; } if (hashTable[hashIndex].value == complement) { result[0] = hashTable[hashIndex].index; result[1] = i; *returnSize = 2; break; } hashTable[hashIndex].value = nums[i]; hashTable[hashIndex].index = i; } free(hashTable); return result; } int main() { int nums[] = {2, 7, 11, 15}; int target = 9; int returnSize; int* result = twoSum(nums, sizeof(nums) / sizeof(nums[0]), target, &returnSize); printf("Indices: %d, %d\n", result[0], result[1]); free(result); return 0; } ``` 这里定义了一个哈希表结构 `HashNode`,包含值和索引两个字段。`twoSum` 函接受一个整组 `nums`、组长度 `numsSize`、目标值 `target` 和指向返回结果大小的指针 `returnSize`。函使用哈希表记录每个元素的值和索引,然后遍历组进行查找,找到符合条件的两个后,将它们的索引保存在 `result` 组中,并通过 `returnSize` 返回结果大小。 在主函中,我们使用示例组 `{2, 7, 11, 15}`,目标值为 `9`。执行 `twoSum` 函后,会得到符合条件的两个组中的索引,并打印输出。 希望能帮到你!如有疑问,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值