365. Count 1 in Binary【LintCode java】
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】的更多相关文章
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 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 ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 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 ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
随机推荐
- 解决pycharm无法导入本地包的问题
在用python写爬虫程序时,import 行无法通过,具体情况如下: pycharm运行程序后,程序pass了,但是出现了警告,如下图所示: 这是由于该程序不在根目录下,无法导入本地包,解决办法如下 ...
- ffmpeg教程
转:http://blog.sina.com.cn/s/blog_51396f890100nd91.html 概要 电影文件有很多基本的组成部分.首先,文件本身被称为容器Container,容器的类 ...
- 设置UI控件的Layer属性(边框可见,边框颜色,边框宽度,边框圆角)
设置UI控件的Layer属性 #import "ViewController.h" @interface ViewController () @property (strong, ...
- mobBUS
1.今天听陈刚说起modBUS通信协议,这个还是第一次听说,究竟是什么东东,还是上网查查看吧 2.网上有C语言程序. http://blog.163.com/li_g888@126/blog/stat ...
- HTML&CSS 问题
1.子div使用浮动,父div高度自适应(个人感觉好用) 方法: css: <style> .clear{ clear:both} </style> html:在父div关闭之 ...
- 初窥UIKit Dynamics
原文来自这里. iOS7中可以方便的给物体添加动态物理特性,主要使用到UIDynamicAnimator,UIDynamicBehavior以及实现了UIDynamicItem协议的对象.在iOS7中 ...
- Error Note1:错误修复笔记
1.遍历同时修改数组内容导致崩溃 bugly上bug提醒如下图所示,经检查发现,可能是页面上数据加载惹得祸. 页面加载过程是这样的,首先进入页面,初始化页面,将本地数据加载到array中刷新table ...
- Stream的顺序流与并行流
/** * @auther hhh * @date 2019/1/2 22:52 * @description */ public class StreamAPI2 { /** * 流的特性:支持并行 ...
- 20145202马超 2016-2017-2 《Java程序设计》第二次实验
去年完成的一部分(http://www.cnblogs.com/tuolemi/p/5728826.html) 今年我又从新做的,这是分别5个问题做出来的结果 下面这个是去年没有做的,是用来建模的,感 ...
- APP如何发布到Google play 商店
APP如何发布到Google play 商店?以及有哪些需要注意的点 2015-05-13 10:07 19773人阅读 评论(1) 收藏 举报 分类: iPhone游戏开发(330) 链接:ht ...