題目
136 Single Number (Easy)
137 Single Number II (Medium)
260 Single Number III (Medium)
268 Missing Number (Easy)
389 Find the Difference (Easy)
190 Reverse Bits (Easy)
191 Number of 1 Bits (Easy)
231 Power of Two (Easy)
405 Convert a Number to Hexadecimal (Easy)
342 Power of Four (Easy)
326 Power of Three (Easy)
1009 Complement of Base 10 Integer (Easy)
371 Sum of Two Integers (Medium)
397 Integer Replacement (Medium)
89 Gray Code
461 Hamming Distance
1342 Number of Steps to Reduce a Number to Zero
868 Binary Gap
1486 XOR Operation in an Array
1720 Decode XORed Array
476 Number Complement
201 Bitwise AND of Numbers Range
[補充]
2119 A Number After a Double Reversal
338 Counting Bits
7 Reverse Integer
29 Divide Two Integers (Medium)
67 Add Binary (Easy)
387 First Unique Character in a String (Easy)
1356 Sort Integers by The Number of 1 Bits (Easy)
693 Binary Number with Alternating Bits (Easy)
762 Prime Number of Set Bits in Binary Representation (Easy)
without
- 兩數字相加,但硬體不支援
+
-
1
2
3
4
5
6
7int getSum(int a, int b) {
while(a){
int carry = (a&b&0x7fffffff)<<1, sum = a^b;
a = carry, b = sum;
}
return b;
} - 一數字是否為三的倍數,但硬體不支援
*
/
1
2
3
4
5
6
7
8
9
10int input = 12;
int remainder = 0;
while(input != 0){
remainder += input & 0x3;
input = input>>2;
if(input == 0 && remainder >= 3){
input = remainder-3;
remainder = 0;
}
}1
2
3
4
5
6
7
8
9
10
11// binary search
bool multipleOf3 (int n){
int l = 0 ;
int h = n ;
while(h-l>1){
int m = l + (h-l)/2 ;
if (3*m <= n) l = m ;
else h = m ;
}
return 3*l == n ;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14// bit Manipulation
// log(n)
bool IsThreesMultiple(int number) {
if (number < 10)
return number == 0 || number == 3 || number == 6 || number == 9;
int oddSums = 0, evenSums = 0;
for (int i = 31; i >= 0; i--)
if (i & 1)
oddSums += (number & (1 << i) ? 1 : 0);
else
evenSums += (number & (1 << i) ? 1 : 0);
return IsThreesMultiple(oddSums - evenSums >= 0 ? oddSums - evenSums : evenSums - oddSums);
} - 從兩個數字中找出最大的一個而不使用判斷描述
1
2
3
4int max(int a, int b)
{
return ((a + b) + abs((a - b))) / 2;
} - 整數變號
1
2
3
4
5
6
7
8void negative(int& x)
{
return ~x + 1; // -x;
}
void negative(int& x)
{
return (x ^ -1) + 1; // -x;
}
技巧
- 善用xor a^a=0 a^0 = a,且滿足交換率。1720
- a^a=0 a^0 = a 可以抓出陣列中唯一出現一次的元素,其餘出現兩次。136
- ret ^= i ^ *(nums + i); 0~n 陣列中哪個數字是
- n = n & (n - 1); 可以用來找 n在二進位有多少個1 191, 231
- 0~n 陣列中 標記陣列中拜訪過元素為負數,就可以知道哪個數字重複了
- 快慢指針,如果有重複的值會被找出,且不會修改陣列元素
leetcode
hackerrank
Lonely Integer
1 | int lonelyinteger(int a_count, int* a) { |
Maximizing XOR
1 | int maximizingXor(int l, int r) { |
Counter game
1 | char* counterGame(unsigned long long int n){ |
Xor-sequence
Sum vs XOR
1 | long sumXor(long n) { |
The Great XOR
1 | long theGreatXor(long x) { |
Yet Another Minimax Problem
1 | void nextPermutation(int n, int *a) { |
Sansa and XOR
1 | int sansaXor(int arr_count, int* arr) { |
AND Product
1 | long andProduct(long a, long b) { |
Time Complexity: Primality
1 | string primality(int n) { |
十進制轉為二進制,並表示
1 |
面試題目
- get-set-clear-inverse 第18位元的bit值
set 一般set 是設定為1
inverse = flip = Toggling
a. write a function to get bit18 value of an unsigned integer data and return 0 or 1
b. write a function to set bit18 value of an unsigned integer data
c. write a function to clear bit18 value of an unsigned integer data
d. write a function to inverse bit18 value of an unsigned integer data
1 | int get(unsigned int n){ |
- union
1 | struct BYTE_struct{ |
請問flag.All 的結果
fa224511
迴圈印出的數字 + 陷阱
1
2
3
4unsigned int i;
for(i=10;i>=10 ; i--){
printf("%d\n", i);
}會印出負的數字,死回圈
解釋下列function功能
1
2
3
4
5
6
7
8
9
10
11
12
13// 二進制中有多少個1
int func(int x){
int a = 0;
while(x){
a++;
x = x&(x-1);
}
return a;
}
// 是否為2的次方
bool func(unsigned int n){
return (n&(n-1) ==0);
}big and little endian + 指標
memory space | value |
---|---|
0x1000 | 0x01 |
0x1001 | 0x23 |
0x1002 | 0x45 |
0x1003 | 0x67 |
0x1004 | 0x89 |
0x1005 | 0xAB |
0x1006 | 0xCD |
0x1007 | 0xEF |
1 | unsigned int *ptr = 0x1000; |
- 指標
1 | int a[] = {1,2,3,4,5,6}; |
- 觀念 變數存活範圍
1 |
|