当前位置: 首页 > news >正文

【leetcode】经典算法题-Counting Bits

题目描述:

给定一个数字n,统计0~n之间的数字二进制的1的个数,并用数组输出

例子:

For num = 5 you should return [0,1,1,2,1,2].

要求:

  • 算法复杂复o(n)
  • 空间复杂度o(n)

原文描述:

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

Example:

For num = 5 you should return [0,1,1,2,1,2].

Follow up:

It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?

Space complexity should be O(n).

Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

思路分析:

  • 根据题目的要求,时间和空间复杂度,明显是要动态规划的方法
  • 得出推到公式:f(n) = 不大于f(n)的最大的2的次方+f(k),k一定是在前面出现的,用数组记录,直接查询
  • 举例f(5) = f(4)+ f(1),注意2de次方都是一个1,而且是最高位,f(5) = 1+f(1),f(6) = 1+f(2)直到f(8) = 1

代码:

public class Solution {
    public int[] countBits(int num) {
        int[] res = new int[num+1];
        int pow2 = 1,before =1;
        for(int i=1;i<=num;i++){
            if (i == pow2){
                before = res[i] = 1;
                pow2 <<= 1;
            }
            else{
                res[i] = res[before] + 1;
                before += 1;
            }
        }
        return res;
    }
}

我的微信二维码如下,欢迎交流讨论

这里写图片描述

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

这里写图片描述

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

转载于:https://www.cnblogs.com/fengsehng/p/6048679.html

相关文章:

  • SQL--常用命令
  • JDK1.7新特性(1):Switch和数字
  • ios开发图片轮播器以及定时器小问题
  • Ubuntu里面软件的安装与卸载
  • ubuntu 设置DNS
  • jquery ajax 传数据到后台乱码的处理方法
  • CSS样式
  • NuGet 学习笔记(1)--Nuget安装使用
  • Part5核心初始化_lesson2---设置svc模式
  • 几个常用的CSS3样式代码以及不兼容的解决办法
  • 报个到
  • iOS: NSArray的方法arrayByAddingObjectsFromArray:
  • excel转化为Json
  • dispatch_after 导致controller没有及时释放
  • poj 2763: [JLOI2011]飞行路线(spfa分层图最短路)
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • 0基础学习移动端适配
  • Asm.js的简单介绍
  • ES6简单总结(搭配简单的讲解和小案例)
  • JavaScript的使用你知道几种?(上)
  • js数组之filter
  • Leetcode 27 Remove Element
  • Lsb图片隐写
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • 从tcpdump抓包看TCP/IP协议
  • 记录一下第一次使用npm
  • 数据仓库的几种建模方法
  • 异常机制详解
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 仓管云——企业云erp功能有哪些?
  • 如何在招聘中考核.NET架构师
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (四)模仿学习-完成后台管理页面查询
  • (五)关系数据库标准语言SQL
  • (原)Matlab的svmtrain和svmclassify
  • (转)LINQ之路
  • ***原理与防范
  • .NET Framework 4.6.2改进了WPF和安全性
  • .Net Remoting常用部署结构
  • .Net 中Partitioner static与dynamic的性能对比
  • .NET分布式缓存Memcached从入门到实战
  • .NET实现之(自动更新)
  • ::什么意思
  • :not(:first-child)和:not(:last-child)的用法
  • :中兴通讯为何成功
  • @selector(..)警告提示
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [BZOJ4554][TJOI2016HEOI2016]游戏(匈牙利)
  • [C#][DevPress]事件委托的使用