5 prove safety offer series: the number of 1s in binary

Let me talk about the concept under the original code, anti-code and complement. In a first computer with a positive or negative number indicates a positive number of 0, a negative number. All being equal number of three yards. For negative numbers, this three yards will not affect its sign. Anti code to all the bits, plus the inverted complement to 1.

Where am thinking read topic input a number n, to find a number of cycles is determined by k, so that the k th> n = 2, then the determination is equal to, if they are equal, the output 1, if not equal, and calculates n the difference between 2 k-1 is referred to as power sig, output is obtained as 1 + f (sig), code is as follows:

 1 #include<iostream>
 2 #include<vector>
 3 #include<math.h>
 4 using namespace std;
 5 class Solution {
 6 public:
 7     int  NumberOf1(int n) {
 8         int i = 0;
 9         while (pow(2, i) < n)
10         {
11             i++;
12         }
13         if (pow(2, i) == n)
14         {
15             return 1;
16         }
17         else {
18             int sig = n - pow(2, i - 1);
19             int re;
20             re = 1 + NumberOf1(sig);
21             return re;
22         }
23 
24     }
25 };
26 int main()
27 {
28     Solution so;
29     int re = so.NumberOf1(20);
30     cout << re<<endl;
31     return 0;
32 }

My own test results are correct, the results being given that overtime, emmmmmmm ......

Then looked at the simple answer is feeling good, this is based on the answers modification:

 1 class Solution {
 2 public:
 3     int  NumberOf1(int n) {
 4         int i = 1,count=0;
 5         while (n)
 6         {
 7             if (i&n)
 8             {
 9                 count++;
10             }
11             n = n >> 1;
12         }
13         return count;
14 
15     }
16 };

However, goose, or time out ......

I changed a bit better, but I did not find the problem ....... This question is in fact, regardless of positive and negative, do not think too much. Tucao about online programming spicy chicken cow customer network. I'm debugging does not work with vc ++ code that actually passed.

 1 class Solution {
 2 public:
 3     int NumberOf1(int n)
 4     {
 5         int count = 0;
 6         int flag = 1;
 7         while (flag)
 8         {
 9             if (n&flag)
10             {
11                 count++;
12             }
13             flag = flag << 1;
14         }
15         return count;
16     }
17 };

Read the answer is really super simple idea, just to n and n-1 and do arithmetic, you can eliminate the far right of the figure, and so on, until the last 1 eliminates the end of the cycle, this should be it the optimal solution of questions:

 1     public:
 2         int NumberOf1(int n)
 3         {
 4             int count = 0;
 5             while (n)
 6             {
 7                 count++;
 8                 n = n&(n - 1);
 9             }
10             return count;
11         }

STL has a direct function can be used to, I remember it ......

1 class Solution {
2     public:
3         int NumberOf1(int n)
4         {
5             bitset<32> bit(n);
6             return bit.count();
7 
8         }

 

Guess you like

Origin www.cnblogs.com/neverland0718/p/10972782.html