File tree 23 files changed +472
-111
lines changed
lcof2/剑指 Offer II 004. 只出现一次的数字
0100-0199/0137.Single Number II
0700-0799/0717.1-bit and 2-bit Characters
23 files changed +472
-111
lines changed Original file line number Diff line number Diff line change 44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
- 统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值
47
+ 统计所有数字每个位中 1 出现的次数,对于某个位,1 出现的次数一定是 3 的倍数 +1 或 0。对这个数 %3 得到的结果就是那个出现一次的数字在该位上的值。
48
48
49
49
<!-- tabs:start -->
50
50
56
56
class Solution :
57
57
def singleNumber (self , nums : List[int ]) -> int :
58
58
ans = 0
59
- for i in range (0 , 32 ):
59
+ for i in range (32 ):
60
60
cnt = sum (num >> i & 1 for num in nums)
61
61
if cnt % 3 :
62
62
if i == 31 :
@@ -109,26 +109,22 @@ func singleNumber(nums []int) int {
109
109
### ** C++**
110
110
111
111
``` cpp
112
- class Solution
113
- {
114
- public:
115
- int singleNumber( vector<int> & nums )
112
+ class Solution {
113
+ public:
114
+ int singleNumber(vector<int >& nums) {
115
+ int ans = 0;
116
+ for (int i = 0; i < 32; ++i)
116
117
{
117
- int ans = 0;
118
- for ( int i = 0; i < 32; i++ )
118
+ int cnt = 0;
119
+ for (int num : nums )
119
120
{
120
- int cnt = 0;
121
- for ( int j = 0; j < nums.size(); j++ )
122
- {
123
- cnt += ((nums[j] >> i) & 1);
124
- }
125
-
126
- cnt %= 3;
127
- ans |= (cnt << i);
121
+ cnt += ((num >> i) & 1);
128
122
}
129
-
130
- return ( ans) ;
123
+ cnt %= 3;
124
+ ans |= cnt << i ;
131
125
}
126
+ return ans;
127
+ }
132
128
};
133
129
```
134
130
Original file line number Diff line number Diff line change 1
- class Solution
2
- {
3
- public:
4
- int singleNumber ( vector<int > & nums )
1
+ class Solution {
2
+ public:
3
+ int singleNumber (vector<int >& nums) {
4
+ int ans = 0 ;
5
+ for (int i = 0 ; i < 32 ; ++i)
5
6
{
6
- int ans = 0 ;
7
- for ( int i = 0 ; i < 32 ; i++ )
7
+ int cnt = 0 ;
8
+ for (int num : nums )
8
9
{
9
- int cnt = 0 ;
10
- for ( int j = 0 ; j < nums.size (); j++ )
11
- {
12
- cnt += ((nums[j] >> i) & 1 );
13
- }
14
-
15
- cnt %= 3 ;
16
- ans |= (cnt << i);
10
+ cnt += ((num >> i) & 1 );
17
11
}
18
-
19
- return ( ans) ;
12
+ cnt %= 3 ;
13
+ ans |= cnt << i ;
20
14
}
15
+ return ans;
16
+ }
21
17
};
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def singleNumber (self , nums : List [int ]) -> int :
3
3
ans = 0
4
- for i in range (0 , 32 ):
4
+ for i in range (32 ):
5
5
cnt = sum (num >> i & 1 for num in nums )
6
6
if cnt % 3 :
7
7
if i == 31 :
Original file line number Diff line number Diff line change 45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
+ ` G(i) = i ^ (i/2) ` 。
49
+
48
50
<!-- tabs:start -->
49
51
50
52
### ** Python3**
51
53
52
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
55
54
56
``` python
55
-
57
+ class Solution :
58
+ def grayCode (self , n : int ) -> List[int ]:
59
+ return [i ^ (i >> 1 ) for i in range (1 << n)]
56
60
```
57
61
58
62
### ** Java**
59
63
60
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
65
62
66
``` java
67
+ class Solution {
68
+ public List<Integer > grayCode (int n ) {
69
+ List<Integer > ans = new ArrayList<> ();
70
+ for (int i = 0 ; i < 1 << n; ++ i) {
71
+ ans. add(i ^ (i >> 1 ));
72
+ }
73
+ return ans;
74
+ }
75
+ }
76
+ ```
77
+
78
+ ### ** C++**
79
+
80
+ ``` cpp
81
+ class Solution {
82
+ public:
83
+ vector<int > grayCode(int n) {
84
+ vector<int > ans;
85
+ for (int i = 0; i < 1 << n; ++i) ans.push_back(i ^ (i >> 1));
86
+ return ans;
87
+ }
88
+ };
89
+ ```
90
+
91
+ ### **Go**
92
+
93
+ ```go
94
+ func grayCode(n int) []int {
95
+ var ans []int
96
+ for i := 0; i < 1<<n; i++ {
97
+ ans = append(ans, i^(i>>1))
98
+ }
99
+ return ans
100
+ }
101
+ ```
63
102
103
+ ### ** JavaScript**
104
+
105
+ ``` js
106
+ /**
107
+ * @param {number} n
108
+ * @return {number[]}
109
+ */
110
+ var grayCode = function (n ) {
111
+ let ans = [];
112
+ for (let i = 0 ; i < 1 << n; ++ i) {
113
+ ans .push (i ^ (i >> 1 ));
114
+ }
115
+ return ans;
116
+ };
64
117
```
65
118
66
119
### ** ...**
Original file line number Diff line number Diff line change 44
44
45
45
## Solutions
46
46
47
+ ` G(i) = i ^ (i/2) ` .
48
+
47
49
<!-- tabs:start -->
48
50
49
51
### ** Python3**
50
52
51
53
``` python
52
-
54
+ class Solution :
55
+ def grayCode (self , n : int ) -> List[int ]:
56
+ return [i ^ (i >> 1 ) for i in range (1 << n)]
53
57
```
54
58
55
59
### ** Java**
56
60
57
61
``` java
62
+ class Solution {
63
+ public List<Integer > grayCode (int n ) {
64
+ List<Integer > ans = new ArrayList<> ();
65
+ for (int i = 0 ; i < 1 << n; ++ i) {
66
+ ans. add(i ^ (i >> 1 ));
67
+ }
68
+ return ans;
69
+ }
70
+ }
71
+ ```
72
+
73
+ ### ** C++**
74
+
75
+ ``` cpp
76
+ class Solution {
77
+ public:
78
+ vector<int > grayCode(int n) {
79
+ vector<int > ans;
80
+ for (int i = 0; i < 1 << n; ++i) ans.push_back(i ^ (i >> 1));
81
+ return ans;
82
+ }
83
+ };
84
+ ```
85
+
86
+ ### **Go**
87
+
88
+ ```go
89
+ func grayCode(n int) []int {
90
+ var ans []int
91
+ for i := 0; i < 1<<n; i++ {
92
+ ans = append(ans, i^(i>>1))
93
+ }
94
+ return ans
95
+ }
96
+ ```
58
97
98
+ ### ** JavaScript**
99
+
100
+ ``` js
101
+ /**
102
+ * @param {number} n
103
+ * @return {number[]}
104
+ */
105
+ var grayCode = function (n ) {
106
+ let ans = [];
107
+ for (let i = 0 ; i < 1 << n; ++ i) {
108
+ ans .push (i ^ (i >> 1 ));
109
+ }
110
+ return ans;
111
+ };
59
112
```
60
113
61
114
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
vector<int > grayCode (int n) {
4
- vector<int > res;
5
- for (int i = 0 ; i < (1 << n); ++i) {
6
- res.push_back ((i >> 1 ) ^ i);
7
- }
8
- return res;
4
+ vector<int > ans;
5
+ for (int i = 0 ; i < 1 << n; ++i) ans.push_back (i ^ (i >> 1 ));
6
+ return ans;
9
7
}
10
8
};
Original file line number Diff line number Diff line change
1
+ func grayCode (n int ) []int {
2
+ var ans []int
3
+ for i := 0 ; i < 1 << n ; i ++ {
4
+ ans = append (ans , i ^ (i >> 1 ))
5
+ }
6
+ return ans
7
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <Integer > grayCode (int n ) {
3
- List <Integer > re = new ArrayList <>();
4
- for (int i = 0 ; i < (1 << n ); i ++) re .add (i ^ (i >> 1 ));
5
- return re ;
3
+ List <Integer > ans = new ArrayList <>();
4
+ for (int i = 0 ; i < 1 << n ; ++i ) {
5
+ ans .add (i ^ (i >> 1 ));
6
+ }
7
+ return ans ;
6
8
}
7
9
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @return {number[] }
4
+ */
5
+ var grayCode = function ( n ) {
6
+ let ans = [ ] ;
7
+ for ( let i = 0 ; i < 1 << n ; ++ i ) {
8
+ ans . push ( i ^ ( i >> 1 ) ) ;
9
+ }
10
+ return ans ;
11
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def grayCode (self , n : int ) -> List [int ]:
3
+ return [i ^ (i >> 1 ) for i in range (1 << n )]
You can’t perform that action at this time.
0 commit comments