zoukankan      html  css  js  c++  java
  • 判断存在重复元素 断舍离

    存在重复元素
    给定一个整数数组,判断是否存在重复元素。

    如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。

    示例 1:

    输入: [1,2,3,1]
    输出: true
    示例 2:

    输入: [1,2,3,4]
    输出: false
    示例 3:

    输入: [1,1,1,3,3,4,3,2,4,2]
    输出: true
    相关标签
    数组
    哈希表
    排序

    作者:力扣 (LeetCode)
    链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x248f5/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

      

    class Solution {
        public boolean containsDuplicate(int[] nums) {
            Arrays.sort(nums);
            for (int ind = 1; ind < nums.length; ind++) {
                if (nums[ind] == nums[ind - 1]) {
                    return true;
                }
            } 
            return false;
        }
    }
     
     
    -------使用bitmap 思路实现,(假设入参最大值 <800000)
    class Solution {
        public boolean containsDuplicate(int[] nums) {
            byte[] bitmap = new byte[200000];
            for(int num : nums) {
                int arridx = num >> 3;
                if(arridx < 0) { //自定义业务逻辑:负数使用 另一半数组空间【100000-200000】
                    arridx = 100000 + arridx*-1;
                }
                int position = num & 7;
                if((bitmap[arridx] & (1 << position)) != 0) {
                    return true;
                }
                 bitmap[arridx] |= (1 << position);
            }
            return false;

        }
    }
    博客地址: https://www.cnblogs.com/java2sap/
    世界丰富多彩,知识天花乱坠。
    ---如果有帮到你,点个赞吧~
  • 相关阅读:
    递归练习题1
    爬虫模块之Beautiful Soup4
    python中的简易表格prettytable
    ubuntu中安装和使用quant-lib
    一个金融软件的基础功能分布
    ONLY_FULL_GROUP_BY 牛皮癣怎么治
    pandas
    pandas行筛选/列筛选(条件筛选/范围筛选)/计算
    conda 的 proxy设置
    openpyxl 安装失败的处理 (缺少 et_xmlfile )
  • 原文地址:https://www.cnblogs.com/java2sap/p/15588510.html
Copyright © 2011-2022 走看看