美文网首页
算法|hash

算法|hash

作者: 激扬飞雪 | 来源:发表于2023-01-26 19:13 被阅读0次

560. 和为 K 的子数组

class Solution {
    public int subarraySum(int[] nums, int k) {
        int preSum = 0;
        int result = 0;
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        hashMap.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            preSum += nums[i];
            if (hashMap.containsKey(preSum - k)) {
                result += hashMap.get(preSum - k);
            }
            hashMap.put(preSum, hashMap.getOrDefault(preSum, 0) + 1);
        }
        return result;
    }
}

974. 和可被 K 整除的子数组

class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        int result = 0;
        int preSum = 0;
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        hashMap.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            preSum += nums[i];
            int y = (preSum % k + k) % k;
            result += hashMap.getOrDefault(y, 0);
            hashMap.put(y, hashMap.getOrDefault(y, 0) + 1);
        }
        return result;
    }
}

12. 整数转罗马数字

class Solution {
    public String intToRoman(int num) {
         int[] nums = new int[] {1000,900,500,400,100,90,50,40,10,9,5,4,1};
         String[] strs = new String[] {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
         StringBuilder result = new StringBuilder();
         for (int i = 0; i < nums.length; i++) {
             while (num >= nums[i]) {
                 result.append(strs[i]);;
                 num = num - nums[i];
             }
         }
         return result.toString();
    }
}

41. 缺失的第一个正数

class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        boolean[] flags = new boolean[n + 1];
        for (int i = 0; i < n; i++) {
            if (nums[i] > 0 && nums[i] <= n) {
                flags[nums[i]] = true;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (!flags[i]) return i;
        }
        return n + 1;
    }
}
class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            // System.out.println(i + " " + nums[i] + " " + nums[nums[i] - 1]);
            while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
                //交换
                int temp = nums[i];
                nums[i] = nums[temp - 1];
                nums[temp - 1] = temp;
            }
        }
        for (int i = 0; i < n; i++) {
            if (nums[i] != i + 1) return i + 1;
        }
        return n + 1;
    }
}
class Solution {
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] <= 0) nums[i] = n + 1;
        }
    
        for (int i = 0; i < n; i++) {
            int index = Math.abs(nums[i]);
            if (index <= n) {
                nums[index - 1] = -Math.abs(nums[index - 1]);
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.println(nums[i]);
            if (nums[i] > 0) return i + 1;
        }
        return n + 1;
    }
}

268. 丢失的数字

class Solution {
    public int missingNumber(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] != i) return i;
        }
        return n;
    }
}
class Solution {
    public int missingNumber(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();
        for (int num:nums) {
            hashSet.add(num);
        }
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (!hashSet.contains(i)) return i;
        }
        return n;
    }
}
class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            //交换到相应位置
            while (nums[i] >= 0 && nums[i] < n && nums[i] != nums[nums[i]]) {
                int temp = nums[i];
                nums[i] = nums[temp];
                nums[temp] = temp;
            }
        }
        for (int i = 0; i < n; i++) {
            if (nums[i] != i) return i;
        }
        return n;
    }
}

287. 寻找重复数

class Solution {
    public int findDuplicate(int[] nums) {
        int slow = 0;
        int fast = 0;
        while (true) {
            slow = nums[slow];
            fast = nums[nums[fast]];
            if (slow == fast) {
                slow = 0;
                while (slow != fast) {
                    slow = nums[slow];
                    fast = nums[fast];
                }
                return slow;
            }
        }
    }
}

442. 数组中重复的数据

class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++){
            int v = Math.abs(nums[i]);
            if (nums[v - 1] > 0) {
                nums[v - 1] = -nums[v - 1];
            } else {
                result.add(v);
            }
        }
        return result;
    }
}

剑指 Offer 03. 数组中重复的数字

class Solution {
    public int findRepeatNumber(int[] nums) {
        for (int i = 0; i < nums.length; i++){
            while (nums[i] != nums[nums[i]]){
                //没在相应的位置,交换
                int temp = nums[i];
                nums[i] = nums[temp];
                nums[temp] = temp;
            }
            if (nums[i] != i) return nums[i];
        }
        return -1;
    }
}

相关文章

  • 分布式集群架构场景化解决方案

    一致性hash算法hash算法应用场景普通hash算法存在的问题一致性hash算法手写一致性hash算法nginx...

  • 哈希算法

    哈希算法 什么是hash函数?常见的hash算法hashlib的用法hash算法的用途 什么是hash函数? 哈希...

  • IOS 逆向开发(二)密码学 HASH

    1. HASH算法简介 1.1 HASH是什么? Hash算法(也叫散列算法) Hash,一般翻译做“散列”,也有...

  • 负载均衡中的一致性hash算法

    hash简介 说到底,他是一种hash算法,那什么是hash算法?hash算法是一种散列算法,常用的比如MD5。抽...

  • 哈希(散列函数)

    力扣题库极客时间 Hash算法也被称为散列算法,Hash算法虽然被称为算法,但实际上它更像是一种思想。Hash算法...

  • Hash一致性算法浅析

    Ngnix负载均衡策略包含Hash算法,就是通过Hash算法将请求hash求值,根据hash值定向到服务器。 假定...

  • 一致性hash算法

    Hash 算法也叫做散列算法,他可以让任意长度的数据M映射成为长度固定的值H。 Hash算法的作用 Hash算法的...

  • 一致性hash算法

    一致性hash算法 1.hash算法 先说一下hash算法,hash算法是将任意长度的二进制值映射为固定长度的二进...

  • Hash算法

    数据结构与算法分析:大纲数据结构:数组算法:hash算法算法:排序算法Java实现 1 Hash算法? 将任意长度...

  • 1. IOS 字典研究

    一: Hash 算法 1. hash算法 hash称为散列表,其实本质上还是一个数组。主要是通过 hash(key...

网友评论

      本文标题:算法|hash

      本文链接:https://www.haomeiwen.com/subject/nnajhdtx.html