leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number「建议收藏」

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

大家好,又见面了,我是全栈君。

一:Number of 1 Bits

题目:

Write a function that takes an unsigned integer and returns the number of ’1′ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11′ has binary representation 00000000000000000000000000001011, so the function should return 3.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

解法一:

此题关键是怎样推断一个数字的第i为是否为0  即: x& (1<<i)

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        for(int i = 0; i < 32; i++){
            if((n & (1<<i)) != 0)count++;
        }
        return count;
        
    }
};

解法二:此解关键在于明确n&(n-1)会n最后一位1消除,这样循环下去就能够求出n的位数中为1的个数

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n > 0){
            n &= n-1;
            count ++;
        }
        return count;
    }
};

二:
Bitwise AND of Numbers Range

题目:

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.

分析:此题提供两种解法:1:当m到n之前假设跨过了1,2,4,8等2^i次方的数字时(即推断m与n是否具有同样的最高位),则会为0,否则顺序将m到n相与。

解法二:利用上题中的思路。n&(n-1)会消除n中最后一个1,如1100000100当与n-1按位与时便会消除最后一个1,赋值给n(这样就减免了非常多不必要按位与的过程)

解法一:

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int bitm = 0, bitn = 0;
        for(int i =0; i < 31; i++){
            if(m & (1<<i))bitm = i;
            if(n & (1<<i))bitn = i;
        }
        if(bitm == bitn){
            int sum = m;
            for(int i = m; i < n; i++)  // 为了防止 2147483647+1 超过范围
                sum = (sum & i);
            sum = (sum & n);
            return sum;
        }
        else return 0;
    }
};

解法二:

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        while(n > m){
            n &= n-1;
        }
        return n;
    }
};

三:
Happy Number

题目:

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

分析:此题关键是用一个set或者map来存储该数字是否已经出现过————hash_map+math

class Solution {
public:
    bool isHappy(int n) {
        while(n != 1){
            if(hset.count(n)) return false;    // 通过hashtable 推断是否出现过
            hset.insert(n);
            int sum = 0;
            while(n != 0){    // 求元素的各个位置平方和
                int mod = n%10;
                n = n/10;
                sum += mod * mod;
            }
            n = sum;
        }
        return true;
        
    }
private:
    set<int> hset;
};

四:Single Number

题目:

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:此题关键在于用到异或

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ans = 0;
        for(int i = 0; i < nums.size(); i++)
            ans ^= nums[i];
        return ans;
        
    }
};

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115212.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 简单无序列表_无序列表怎么横排

    简单无序列表_无序列表怎么横排实现如下图所示的简单无序列表,并为每个列表项添加超链接。每个列表项的宽度为50px,高度为30px,列表中的文字在水平和垂直方向上均居中显示,超链接初始状态和访问后的状态均为黑色无下划线,鼠标经过和点

    2022年8月2日
  • java中 servlet和jsp的概念及区别

    java中 servlet和jsp的概念及区别林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka目录一了解Servlet的概念二Servlet技术功能三Servlet技术特点四Servlet生命周期五servlet工作过程六Servlet与JSP区别七Servlet代码结构八Servlet第一个程序一、了解Servlet的概念

    2022年6月14日
  • 手把手教你学SVN

    手把手教你学SVN注意转载须保留原文链接(http://www.cnblogs.com/wzhiq896/p/6822713.html)作者:wangwen896SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS、CVS,它采用了分支管理系统,它的设计目标就是取代CVS。互联网上很多版本控制服务已从CVS迁移到Subversion。对于很多新手来…

    2022年5月6日
  • asp实现用户注册登录代码(用Javaweb制作登录注册页面)

    最近在写asp课程设计,网站登录注册的功能怎么能少,捣鼓了两天终于弄出点东西来了。环境:Windows10+VS2015+自带LocalDB看一下效果:1、注册页面:如果用户重名:2、登录页:3、注册或者登录好了会跳到Home页面并且显示当前的用户下面看看关键代码:①注册前台页面Register….

    2022年4月15日
  • Python – 编写可视化界面(Python+PyCharm+PyQt)

    Python – 编写可视化界面(Python+PyCharm+PyQt)Python编写可视化界面最近开始学习Python,但只限于看理论,编几行代码,觉得没有意思,就想能不能用Python编写可视化的界面。遂查找了相关资料,发现了PyQt,由于前一段时间刚看过Qt,而且对Qt的印象很好,于是觉得用PyQt应该是一个比较愉快的选择。1、前言PyQt的版本需要与Python的版本保持一致,在这里我用的PyQT的版本是PyQt5-5.6-gp…

    2022年5月17日
  • IM通讯 即时通讯 交友源码 聊天源码

    IM通讯 即时通讯 交友源码 聊天源码IM通讯即时通讯交友源码聊天源码web端+wap端+H5端+安卓端+IOS端+全开源编号:16150665877560702可可网络

    2022年5月14日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号