Description

Count how many 1 in binary representation of a 32-bit integer.

Example

Given 32, return 1

Given 5, return 2

Given 1023, return 9

Challenge

If the integer is n bits with m 1 bits. Can you do it in O(m) time?

解题:很简单,但是要考虑范围的问题。代码如下:

public class Solution {
/*
* @param num: An integer
* @return: An integer
*/
public int countOnes(int num) {
// write your code here
int count = 0;
long temp = (long)num;
if(temp < 0){
temp = (long)Math.pow(2,32) + temp;
}
while(temp != 0){
if(temp %2 == 1)
count++;
temp = temp / 2;
}
return count;
}
};

365. Count 1 in Binary【LintCode java】的更多相关文章

  1. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  2. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. 375. Clone Binary Tree【LintCode java】

    Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...

  5. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  6. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  7. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  8. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  9. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

随机推荐

  1. 【luogu P2279 [HNOI2003]消防局的设立】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2279 想怎么贪怎么贪 #include <queue> #include <cstdio& ...

  2. HDU 1018Big Number(大数的阶乘的位数,利用公式)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...

  3. 【转载】对C#DateTime的一些扩展,计算周内第一天,最后一天

    /// <summary> /// DateTime的一些扩展 /// </summary> public class DateTime2 { /// <summary& ...

  4. iOS- CoreData 数据库管理利器!

    1.前文 上次用SQLite3实现了数据管理,这次准备用CoreData来实现. Core Data 是iOS SDK 里的一个很强大的框架,允许程序员以面向对象的方式储存和管理数据.使用Core D ...

  5. LeetCode 简单 -旋转字符串(796)

    给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结果就是'bcdea' .如果在若干次旋转操作之后,A 能变成B ...

  6. windows下nginx的安装

    一. 下载 http://nginx.org/    (下载后解压) 二. 修改配置文件 nginx配置文件在 nginx-1.8.0\conf\nginx.conf http { gzip on; ...

  7. Mac进度条卡在100%

  8. MFC非模态添加进程控件方法二(自定义消息方法)

    以下内容有大部分转载自CSDN,经过自己整理完成. 该文主要的方法为在非模态对话框中自定义一组消息函数,然后在主窗口中开辟一个线程通过线程把消息传递给子窗口进行更新. 在子窗口类中定义消息如下: /* ...

  9. Ruby中类的进阶(继承,private, public, protect)

    类中的public,protect,private public method class Point def test end end 这样定义的test方法就是一个public方法可以在类内外使用 ...

  10. python新手之字典增删改查

    一.字典的定义 city_list = { 'beijin':"北京",'shanghai':"上海" } print(city_list) 二.字典添加一个元 ...