zoukankan      html  css  js  c++  java
  • C#学习笔记之——二进制中1的个数

    输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

    此题我第一个想法是把这个数转换成二进制存为字符串,然后通过遍历找有多少个‘1’的方法,后来看到还有很多人有其他的方法,感觉拓展了知识面。


    下面是我的最初的方法:

            public int NumberOf1(int n)
            {
                // write code here
                int count = 0;//次数
                string a = Convert.ToString(n, 2);//转换成二进制数存入一个空字符串
                for (int i = 0; i < a.Length; i++) //遍历找有多少个‘1’
                {
                    if (a[i] == '1')
                    {
                        count++;
                    }
                }
                return count;
            }

    这里有一个Convert.ToString(),当你在vs里面打的时候你会发现括号里面可以写的很多,可以直接将某种数据转换成string类型,但它还可以是Convert.ToString(int,2)将一个转换成二进制后变为string。这里就讲到string的一个用法了,它实例的对象可以直接用[i]来表示第i个字符。


    下面这个方法也是基于转换成二进制存入字符串的情况后,如何找有多少个‘1’。但相对效率比较低。用replace,去掉"1",再比较长度

            public int NumberOf1(int n)
            {
                // write code here
                //int count = 0;
                string a = Convert.ToString(n, 2);
                string r = a.Replace("1", "");
                int len = a.Length - r.Length;
                return len;
            }

    下面这个方法也是基于转换成二进制存入字符串的情况后,用正则表达式找

            public int NumberOf1(int n)
            {
                string a = Convert.ToString(n, 2);
                Regex rege = new Regex("1", RegexOptions.Compiled);
                int count = rege.Matches(a).Count;
                return count;
            }

    用IndexOf和while查找

            public int NumberOf1(int n)
            {
                string a = Convert.ToString(n, 2);
                string key = "1";
                int index = 0;
                int count = 0;
                while((index = a.IndexOf(key, index)) != -1)
                {
                    count++;
                    index = index + key.Length;
                }
                return count;
            }

    foreach

            public int NumberOf1(int n)
            {
                int count = 0;
                string a = Convert.ToString(n, 2);
                foreach (var i in a)
                {
                    if(i.Equals('1'))
                    {
                        count++;
                    }
                }
                return count;
            }

    这种操作,很厉害,位的与运算

            public int NumberOf1(int n)
            {
                // write code here
                int num = 0;
                int s = n;
    
                while (s != 0)
                {
                    s &= (s - 1);
                    num += 1;
                }
                return num;
            }

    还有左移操作的方式

            public int NumberOf1(int n)
            {
                int count = 0;
                int flag = 1;
                while (flag != 0)
                {
                    if ((n & flag) != 0)
                    {
                        count++;
                    }
                    flag = flag << 1;
                }
                return count;
            }
    

    再来一个操作

            public int NumberOf1(int n)
            {
                // write code here
                int index = 0;
                if (n < 0)
                {
                    n = n & 0x7FFFFFFF;
                    ++index;
                }
                while (n > 0)
                {
                    index += n & 1;
                    n = n >> 1;
                }
                return index;
            }

    如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

    如果您想转载本博客,请注明出处

    如果您对本文有意见或者建议,欢迎留言

    感谢您的阅读,请关注我的后续博客

  • 相关阅读:
    ADB命令大全
    Backup your Android without root or custom recovery -- adb backup
    Content portal for Pocketables Tasker articles
    Is there a way to detect if call is in progress? Phone Event
    Tasker to proximity screen off
    Tasker to detect application running in background
    Tasker to create toggle widget for ES ftp service -- Send Intent
    Tasker to proximity screen on
    Tasker to answer incoming call by pressing power button
    Tasker to stop Poweramp control for the headset while there is an incoming SMS
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852144.html
Copyright © 2011-2022 走看看