Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91d485d

Browse files
daliang111willrlzhangyanglbme
authored
feat: add cpp solution to lcof2 problem: No.004 (doocs#560)
Co-authored-by: willrlzhang <willrlzhang@tencent.com> Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 05246b4 commit 91d485d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎lcof2/剑指 Offer II 004. 只出现一次的数字/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,38 @@ func singleNumber(nums []int) int {
107107
}
108108
```
109109

110+
### **C++**
111+
112+
```cpp
113+
class Solution
114+
{
115+
public:
116+
int singleNumber( vector<int> & nums )
117+
{
118+
int ans = 0;
119+
for ( int i = 0; i < 32; i++ )
120+
{
121+
int cnt = 0;
122+
for ( int j = 0; j < nums.size(); j++ )
123+
{
124+
cnt += ((nums[j] >> i) & 1);
125+
}
126+
127+
cnt %= 3;
128+
ans |= (cnt << i);
129+
}
130+
131+
return(ans);
132+
}
133+
};
134+
```
135+
110136
### **...**
111137
112138
```
113139

114140
```
115141
142+
143+
116144
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
class Solution
3+
{
4+
public:
5+
int singleNumber( vector<int> & nums )
6+
{
7+
int ans = 0;
8+
for ( int i = 0; i < 32; i++ )
9+
{
10+
int cnt = 0;
11+
for ( int j = 0; j < nums.size(); j++ )
12+
{
13+
cnt += ((nums[j] >> i) & 1);
14+
}
15+
16+
cnt %= 3;
17+
ans |= (cnt << i);
18+
}
19+
20+
return(ans);
21+
}
22+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /