Skip to content

Commit fcb1026

Browse files
committed
🚀 06-Jul-2020
1 parent 84cef1f commit fcb1026

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order.
2+
3+
Example:
4+
5+
Given the following matrix:
6+
7+
[
8+
[ 1, 2, 3 ],
9+
[ 4, 5, 6 ],
10+
[ 7, 8, 9 ]
11+
]
12+
You should return
13+
14+
[1, 2, 3, 6, 9, 8, 7, 4, 5]
15+
16+
17+
18+
19+
20+
21+
vector<int> Solution::spiralOrder(const vector<vector<int> > &ar) {
22+
int m=ar.size();
23+
int n=ar[0].size();
24+
vector<int>res;
25+
int t=0, b=m-1, l=0, r=n-1;
26+
int dir=0; // direction r d l u
27+
while(t<=b && l<=r){
28+
if(dir==0){
29+
for(int i=l; i<=r;i++) res.push_back(ar[t][i]);
30+
t++;
31+
dir=1;
32+
continue;
33+
}
34+
if(dir==1){
35+
for(int i=t; i<=b;i++) res.push_back(ar[i][r]);
36+
r--;
37+
dir=2;
38+
continue;
39+
}
40+
if(dir==2){
41+
for(int i=r; i>=l; i--) res.push_back(ar[b][i]);
42+
b--;
43+
dir=3;
44+
continue;
45+
}
46+
if(dir==3){
47+
for(int i=b; i>=t;i--) res.push_back(ar[i][l]);
48+
l++;
49+
dir=0;
50+
continue;
51+
}
52+
}
53+
return res;
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
2+
3+
4+
5+
Example 1:
6+
7+
Input: 00000000000000000000000000001011
8+
Output: 3
9+
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
10+
Example 2:
11+
12+
Input: 00000000000000000000000010000000
13+
Output: 1
14+
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
15+
Example 3:
16+
17+
Input: 11111111111111111111111111111101
18+
Output: 31
19+
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
20+
21+
22+
Note:
23+
24+
Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
25+
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.
26+
27+
28+
Follow up:
29+
30+
If this function is called many times, how would you optimize it?
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
class Solution {
41+
public:
42+
int hammingWeight(uint32_t n) {
43+
int cnt=0;
44+
int tmp=32;
45+
while(tmp--){
46+
bool a=n & 1;
47+
if(a) cnt++;
48+
n=n>>1;
49+
}
50+
return cnt;
51+
}
52+
};
53+
54+
55+
56+
57+
58+
59+
class Solution {
60+
public:
61+
int hammingWeight(uint32_t n) {
62+
return __builtin_popcount(n);
63+
}
64+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
2+
3+
Given two integers x and y, calculate the Hamming distance.
4+
5+
Note:
6+
0 ≤ x, y < 231.
7+
8+
Example:
9+
10+
Input: x = 1, y = 4
11+
12+
Output: 2
13+
14+
Explanation:
15+
1 (0 0 0 1)
16+
4 (0 1 0 0)
17+
↑ ↑
18+
19+
The above arrows point to positions where the corresponding bits are different.
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int hammingDistance(int x, int y) {
33+
int n=32, cnt=0;
34+
while(n--){
35+
bool a=x & 1;
36+
bool b=y & 1;
37+
if(a!=b) cnt++;
38+
x=x>>1;
39+
y=y>>1;
40+
}
41+
return cnt;
42+
}
43+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
2+
3+
Given two integers x and y, calculate the Hamming distance.
4+
5+
Note:
6+
0 ≤ x, y < 231.
7+
8+
Example:
9+
10+
Input: x = 1, y = 4
11+
12+
Output: 2
13+
14+
Explanation:
15+
1 (0 0 0 1)
16+
4 (0 1 0 0)
17+
↑ ↑
18+
19+
The above arrows point to positions where the corresponding bits are different.
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int hammingDistance(int x, int y) {
33+
int n=32, cnt=0;
34+
while(n--){
35+
bool a=x & 1;
36+
bool b=y & 1;
37+
if(a!=b) cnt++;
38+
x=x>>1;
39+
y=y>>1;
40+
}
41+
return cnt;
42+
}
43+
};
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
class Solution {
54+
public:
55+
int hammingDistance(int x, int y) {
56+
int res=x^y;
57+
int cnt=0;
58+
while(res>0){
59+
cnt+=res & 1;
60+
res>>=1;
61+
}
62+
return cnt;
63+
}
64+
};

0 commit comments

Comments
 (0)