Skip to content

Commit b1543aa

Browse files
Xuemeng ZhangXuemeng Zhang
authored andcommitted
May 6, 2018
1 parent 20c9780 commit b1543aa

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

137_Single_Number_II.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
3+
4+
Note:
5+
6+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
7+
8+
Example 1:
9+
10+
Input: [2,2,3,2]
11+
Output: 3
12+
Example 2:
13+
14+
Input: [0,1,0,1,0,1,99]
15+
Output: 99
16+
*/
17+
#include <vector>
18+
using namespace std;
19+
20+
class Solution {
21+
public:
22+
int singleNumber(vector<int>& nums) {
23+
int output = 0;
24+
for(size_t i=0; i<32; ++i){
25+
int cnt = 0;
26+
for(size_t j=0; j<nums.size(); ++j){
27+
if( (nums[j]>>i) & 0x1 ){
28+
cnt++;
29+
cnt %= 3;
30+
}
31+
}
32+
if(cnt) output |= (1<<i);
33+
}
34+
return output;
35+
}
36+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
3+
4+
Example:
5+
6+
Input: [5,7]
7+
Output: 4
8+
*/
9+
10+
class Solution {
11+
public:
12+
int rangeBitwiseAnd(int m, int n) {
13+
int output = 0;
14+
15+
for(int i=0; i<31; ++i){
16+
int bit_mask = 1<<i;
17+
if( (m&bit_mask) && \
18+
(n&bit_mask) && \
19+
((n-m)<bit_mask) )
20+
output |= bit_mask;
21+
}
22+
23+
return output;
24+
}
25+
};

260_Single_Number_III.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice.
3+
Find the two elements that appear only once.
4+
5+
For example:
6+
7+
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
8+
9+
Note:
10+
The order of the result is not important. So in the above example, [5, 3] is also correct.
11+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
12+
*/
13+
#include <vector>
14+
using namespace std;
15+
16+
class Solution {
17+
public:
18+
vector<int> singleNumber(vector<int>& nums) {
19+
int temp = 0;
20+
size_t bit_pos = 0;
21+
for(size_t i=0; i<nums.size(); ++i) temp ^= nums[i];
22+
for(size_t i=0; i<32; ++i){
23+
if(temp&(1<<i)){
24+
bit_pos = i;
25+
break;
26+
}
27+
}
28+
int mask = 1<<bit_pos;
29+
int temp1 = 0, temp2 = 0;
30+
for(size_t i=0; i<nums.size(); ++i){
31+
if(nums[i]&mask) temp1 ^= nums[i];
32+
else temp2 ^= nums[i];
33+
}
34+
35+
vector<int> output;
36+
output.push_back(temp1);
37+
output.push_back(temp2);
38+
return output;
39+
}
40+
};

338_Counting_Bits.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num
3+
calculate the number of 1's in their binary representation and return them as an array.
4+
5+
Example:
6+
For num = 5 you should return [0,1,1,2,1,2].
7+
8+
Follow up:
9+
10+
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can
11+
you do it in linear time O(n) /possibly in a single pass? Space complexity should be O(n).
12+
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount
13+
in c++ or in any other language.
14+
*/
15+
#include <vector>
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
vector<int> countBits(int num) {
21+
vector<int> output;
22+
int lead_one_bit = 0;
23+
24+
for(int i=0; i<=num; ++i){
25+
if(0==i) output.push_back(0);
26+
else if(1==i) output.push_back(1);
27+
else{
28+
if( i==(1<<(lead_one_bit+1)) )
29+
lead_one_bit++;
30+
int temp = 1 + output[i-(1<<lead_one_bit)];
31+
output.push_back(temp);
32+
}
33+
}
34+
35+
return output;
36+
}
37+
};

397_Integer_Replacement.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Given a positive integer n and you can do operations as follow:
3+
4+
If n is even, replace n with n/2.
5+
If n is odd, you can replace n with either n + 1 or n - 1.
6+
What is the minimum number of replacements needed for n to become 1?
7+
8+
Example 1:
9+
10+
Input:
11+
8
12+
13+
Output:
14+
3
15+
16+
Explanation:
17+
8 -> 4 -> 2 -> 1
18+
Example 2:
19+
20+
Input:
21+
7
22+
23+
Output:
24+
4
25+
26+
Explanation:
27+
7 -> 8 -> 4 -> 2 -> 1
28+
or
29+
7 -> 6 -> 3 -> 2 -> 1
30+
*/
31+
#include "limits.h"
32+
33+
class Solution {
34+
public:
35+
int integerReplacement(int n) {
36+
if(1==n) return 0;
37+
else if(n%2){
38+
int temp1 = (n==INT_MAX) ? 2 + integerReplacement(1<<30) : \
39+
2 + integerReplacement((n+1)/2);
40+
int temp2 = 2 + integerReplacement((n-1)/2);
41+
return (temp1<temp2)?temp1:temp2;
42+
}
43+
return 1 + integerReplacement(n/2);
44+
}
45+
};

0 commit comments

Comments
 (0)