代码随想录训练营第三十五期|第5天|哈希表part02|

454. 四数相加 II - 力扣(LeetCode)

.因为要求的是4个数的和为0的组合,先以两两数组求和,这样就和two sum差不多解法了

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int n1 : nums1) {
            for (int n2 : nums2) {
                int sum = n1 + n2;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }

        int res = 0;
        for (int n3 : nums3) {
            for (int n4 : nums4) {
                int remain = 0 - n3 - n4;
                res += map.getOrDefault(remain, 0);
            }
        }
        return res;
    }
}

383. 赎金信 - 力扣(LeetCode)

和Anagram那题思路一样

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) return false;
        
        int[] count = new int[26];
        for (int i = 0; i < magazine.length(); i++) {
            count[magazine.charAt(i) - 'a']++;
        }

        for (int i = 0; i < ransomNote.length(); i++) {
            count[ransomNote.charAt(i) - 'a']--;
        }

        for (int n : count) {
            if (n < 0) return false;
        }

        return true;
    }
}

15. 三数之和 - 力扣(LeetCode)

1.把数组sort一下

2. 在for循环中用双指针

3.要留意去重

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums.length < 3) return res;
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            int left = i + 1;
            int right = nums.length - 1;

            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum < 0) {
                    left++;
                } else if (sum > 0) {
                    right--;
                } else {
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
}

18. 四数之和 - 力扣(LeetCode)

和三数字和思路差不多,在双重for循环中进行双指针

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0 && nums[i] > target) return res;
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                int left = j + 1;
                int right = nums.length - 1;

                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum < target) {
                        left++;
                    } else if (sum > target) {
                        right--;
                    } else {
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是一种常用的数据结构,用于存储和快速检索键值对。在Verilog代码中实现哈可以通过以下步骤: 1. 定义哈的参数:需要定义哈的大小和每个哈桶的宽度。哈的大小应该是一个质数,以减少冲突。 2. 定义哈桶:使用Verilog中的reg类型数组来示每个哈桶。每个桶应包含至少两个元素:键和对应的值。 3. 实现哈函数:哈函数将键值映射到正确的哈桶位置。常见的哈函数有取模运算、乘法散列等。这一步需要根据具体情况进行选择。 4. 实现插入操作:根据哈函数计算出正确的哈桶位置,并将键值对存储到对应的位置。 5. 实现查找操作:根据给定的键,使用哈函数计算出对应的哈桶位置,并将存储在该位置的值返回。 以下是一个简单的哈Verilog代码的示例: ```verilog module hash_table( input wire [N-1:0] key, input wire [M-1:0] value, input wire insert, input wire find, output wire [M-1:0] result ); parameter N = 8; // 键的宽度 parameter M = 16; // 值的宽度 parameter SIZE = 16; // 哈大小 parameter BUCKET_WIDTH = N + M; // 哈桶宽度 reg [BUCKET_WIDTH-1:0] table [0:SIZE-1]; // 哈 // 哈函数 function automatic integer hash; hash = key % SIZE; endfunction always @(posedge insert) begin integer location; location = hash(); table[location] = {key, value}; end always @(posedge find) begin integer location; location = hash(); result <= table[location][N-1:0]; end endmodule ``` 在以上Verilog代码中,模块`hash_table`接受键、值、插入信号和查找信号作为输入,通过`result`输出查找到的值。哈函数采用简单的取模运算来实现。插入操作在`posedge insert`时触发,根据哈函数计算出正确的桶位置,并将键值对存储到对应的位置。查找操作在`posedge find`时触发,根据哈函数计算出正确的桶位置,然后返回存储在该位置的值。 需要注意的是,以上代码是一个简单的示例,可能并不适用于所有情况。在实际应用中,需要根据具体需求做出相应的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值