关闭

leetcode之Counting Bits

103人阅读 评论(0) 收藏 举报
分类:

贴写的三种代码,当然最后一种是参考别人的代码

第一种:没有考虑分组:

mport java.util.Scanner;


public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
for(int i = 0; i <= num; i++)
{
int count = 0;
int j = i;
while(j/2 >= 0 && j >0)
{
if(j != j/2*2)
count += 1;
j = j/2; 
}
sum[i]=count;
 
}
return sum;

}
public static void main(String args[])
{
Scanner n = new Scanner(System.in);
int num = n.nextInt();
int k = Integer.SIZE;
int[] sum = new int[num + 1];
sum = countBits(num);
for(int i =0; i <= num; i ++)
System.out.print(sum[i]);
 
}
       
   }

第二种考虑分组 (2的n次方倍):

import java.util.Scanner;


public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
int k  = 1;
if(num ==0)
sum[0] = 0;
else if(num ==1)
sum[1] = 1;
 
else
{
sum[0] = 0;
sum[1] = 1;
int i = (int) Math.pow(2, k);
for( ; i + 1< (i*2) && i<=num; i +=2)
{
if(i == num)
sum[i]=sum[i/2];
else
{
sum[i]=sum[i/2];
sum[i+1]=sum[i/2]+1;
}
}
}

return sum;

}
public static void main(String args[])
{
Scanner n = new Scanner(System.in);
int num = n.nextInt();
int k = Integer.SIZE;
int[] sum = new int[num + 1];
sum = countBits(num);
for(int i =0; i <= num; i ++)
System.out.print(sum[i]);
 
}
       
   }

第三种:

import java.util.Scanner;


public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
sum[0] =0;
for(int i = 0; i <= num; i++)
if(i%2 == 0)
sum[i] = sum[i/2];
else
sum[i] = sum[i/2]+1;

return sum;

}
}

0
0
查看评论

[leetcode-338]Counting Bits(java)

原文地址:https://leetcode.com/problems/counting-bits/分析:这道题要求给定N,然后计算从0到N之间所有的数的bit为1的数,并要求时间复杂度为O(N),因此对于每个数,然后求它的bit数的方法是不可取的。 所幸,当我们把数从0到N写出来时很容易发现规律,...
  • zdavb
  • zdavb
  • 2016-03-27 20:42
  • 1152

[leetcode] 338. Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in thei...
  • TstsUgeg
  • TstsUgeg
  • 2016-03-18 11:57
  • 2026

leetcode [python] 【338】Counting Bits

leetcode题解
  • Sophie94
  • Sophie94
  • 2016-04-07 16:19
  • 383

leetcode笔记:Counting Bits

题目大意是,给定一个非负整数num,对于每一个满足0 ≤ i ≤ num的数字i,计算这些数字的二进制表示中1的个数,并以数组vector的形式返回。
  • liyuefeilong
  • liyuefeilong
  • 2016-04-07 15:29
  • 2838

338. Counting Bits [medium] (Python)

题目链接https://leetcode.com/problems/counting-bits/题目原文 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate...
  • coder_orz
  • coder_orz
  • 2016-07-29 13:27
  • 928

LeetCode之Counting Bits

class Solution { public: vector countBits(int num) { if (num == 0){ data.push_back(0); return data; } int level = ceil(log2(num+1)); in...
  • caoyan_12727
  • caoyan_12727
  • 2016-03-29 17:58
  • 310

leetcode之Counting Bits

这题是用来找出来一个数字化为2进制是有多少个1的,题目的提示很明显,就是要去找规律。规律其实很简单。就是2的n次方之后的都是1-2的n次方-1的值再加上1。难得写个能打败90%多的解。class Solution(object): def countBits(self, num): ...
  • howtogetout
  • howtogetout
  • 2016-06-18 01:23
  • 172

338. Counting Bits 数字的二进制中1的个数

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in thei...
  • u010005161
  • u010005161
  • 2016-05-07 21:31
  • 1593

LeetCode---Counting Bits解题分析

题意描述:给定一个非负整数num,则返回区间[0,num]中每个数字对应的二进制中1的个数。比如“num=5”,返回[0, 1, 1, 2, 1, 2] 解题思路一:这里借鉴上一题的解法,一个函数实现计算数字的二进制形式中1的个数,然后在另一个函数中调用进而求出区间内每个数字的二进制形式中1的个数 ...
  • u012050154
  • u012050154
  • 2016-06-04 15:09
  • 380

LeetCode 第 338 题 (Counting Bits)

LeetCode 第 338 题 (Counting Bits) Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s i...
  • liyuanbhu
  • liyuanbhu
  • 2016-04-19 09:30
  • 1708
    个人资料
    • 访问:415次
    • 积分:33
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档