首页 > 其他分享 >leetcode-1356-easy

leetcode-1356-easy

时间:2022-10-31 20:55:37浏览次数:50  
标签:map arr int 1356 c1 easy array bits leetcode

Sort Integers by The Number Of 1 Bits

You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

Return the array after sorting it.

Example 1:

Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Explantion: [0] is the only integer with 0 bits.
[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]
Example 2:

Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output: [1,2,4,8,16,32,64,128,256,512,1024]
Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
Constraints:

1 <= arr.length <= 500
0 <= arr[i] <= 104

思路一:先统计数字的 bit 位数,然后用排序

public int[] sortByBits(int[] arr) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i : arr) {
        map.put(i, Integer.bitCount(i));
    }

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length - 1; j++) {
            int c1 = map.get(arr[j]);
            int c2 = map.get(arr[j + 1]);
            if (c1 > c2 || (c1 == c2 && arr[j] > arr[j + 1])) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
        }
    }

    return arr;
}

标签:map,arr,int,1356,c1,easy,array,bits,leetcode
From: https://www.cnblogs.com/iyiluo/p/16845760.html

相关文章

  • AI人脸检测识别EasyCVR视频融合平台告警预案的配置操作与使用
    我们在前期的文章中为大家介绍了EasyCVR新增的告警预案功能,感兴趣的用户可以戳这篇文章:《AI人脸检测智能视频融合平台EasyCVR新增告警预案功能》。  告警预案可以根......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • leetcode-1-easy
    TwoSumGivenanarrayofintegersnumsandanintegertarget,returnindicesofthetwonumberssuchthattheyadduptotarget.Youmayassumethateachinp......
  • C++&Python 描述 LeetCode 1.两数之和
    C++&Python描述LeetCode1.两数之和  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、、GitHub、B站、华为开发者论坛等平台分享一些技术博文。放弃不难,但坚持......
  • SpringBoot如何用easyPOI导出excel文件
    在工作中,经常需要我们用Java代码导出一些数据,保存在Excel中。这是非常实用的Excel导出功能,如果我们用SpringBoot结合EasyPOI框架,可以非常方便地实现这个功能。在maven项目中......
  • dp-leetcode152
    动态规划问题,存在重叠子问题/***<p>给你一个整数数组<code>nums</code>&nbsp;,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数......
  • LeetCode刷题记录.Day1
    二分查找基础:二分查找题目链接 LoadingQuestion...-力扣(LeetCode)最开始的题解:classSolution{public:intsearch(vector<int>&nums,inttarget){......
  • LeetCode15. 三数之和
    题意找出数组中三个和为0的数字方法哈希表代码classSolution{public:vector<vector<int>>threeSum(vector<int>&nums){vector<vector<int>>resu......
  • easyexcel 导出 excel 表格数据
    创建一个Springboot项目easyexcel导出excel表格数据创建之后,pom.xml配置<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.o......