Skip to content

Commit cf791fa

Browse files
committed
feat: add solutions to lc problems: No.0136, 0137
- No.0136.Single Number - No.0137.Single Number II
1 parent 562b046 commit cf791fa

File tree

10 files changed

+177
-16
lines changed

10 files changed

+177
-16
lines changed

solution/0100-0199/0136.Single Number/README.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,36 @@ var singleNumber = function (nums) {
138138
};
139139
```
140140

141+
### **TypeScript**
142+
143+
```ts
144+
function singleNumber(nums: number[]): number {
145+
return nums.reduce((r, v) => r ^ v);
146+
}
147+
```
148+
141149
### **Rust**
142150

143151
```rust
144152
impl Solution {
145153
pub fn single_number(nums: Vec<i32>) -> i32 {
146-
let mut result = 0;
147-
for num in nums {
148-
result ^= num;
149-
}
150-
result
154+
nums.into_iter().reduce(|r, v| r ^ v).unwrap()
151155
}
152156
}
153157
```
154158

159+
### **C**
160+
161+
```c
162+
int singleNumber(int *nums, int numsSize) {
163+
int ans = 0;
164+
for (int i = 0; i < numsSize; i++) {
165+
ans ^= nums[i];
166+
}
167+
return ans;
168+
}
169+
```
170+
155171
### **Swift**
156172
157173
```swift

solution/0100-0199/0136.Single Number/README_EN.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,36 @@ var singleNumber = function (nums) {
102102
};
103103
```
104104

105+
### **TypeScript**
106+
107+
```ts
108+
function singleNumber(nums: number[]): number {
109+
return nums.reduce((r, v) => r ^ v);
110+
}
111+
```
112+
105113
### **Rust**
106114

107115
```rust
108116
impl Solution {
109117
pub fn single_number(nums: Vec<i32>) -> i32 {
110-
let mut result = 0;
111-
for num in nums {
112-
result ^= num;
113-
}
114-
result
118+
nums.into_iter().reduce(|r, v| r ^ v).unwrap()
115119
}
116120
}
117121
```
118122

123+
### **C**
124+
125+
```c
126+
int singleNumber(int *nums, int numsSize) {
127+
int ans = 0;
128+
for (int i = 0; i < numsSize; i++) {
129+
ans ^= nums[i];
130+
}
131+
return ans;
132+
}
133+
```
134+
119135
### **Swift**
120136
121137
```swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int singleNumber(int *nums, int numsSize) {
2+
int ans = 0;
3+
for (int i = 0; i < numsSize; i++) {
4+
ans ^= nums[i];
5+
}
6+
return ans;
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
impl Solution {
22
pub fn single_number(nums: Vec<i32>) -> i32 {
3-
let mut result = 0;
4-
for num in nums {
5-
result ^= num;
6-
}
7-
result
3+
nums.into_iter().reduce(|r, v| r ^ v).unwrap()
84
}
9-
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function singleNumber(nums: number[]): number {
2+
return nums.reduce((r, v) => r ^ v);
3+
}

solution/0100-0199/0137.Single Number II/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,52 @@ public:
122122
};
123123
```
124124
125+
### **TypeScript**
126+
127+
```ts
128+
function singleNumber(nums: number[]): number {
129+
let ans = 0;
130+
for (let i = 0; i < 32; i++) {
131+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
132+
ans |= count % 3 << i;
133+
}
134+
return ans;
135+
}
136+
```
137+
138+
### **Rust**
139+
140+
```rust
141+
impl Solution {
142+
pub fn single_number(nums: Vec<i32>) -> i32 {
143+
let mut ans = 0;
144+
for i in 0..32 {
145+
let count = nums.iter().map(|v| v >> i & 1).sum::<i32>();
146+
ans |= count % 3 << i;
147+
}
148+
ans
149+
}
150+
}
151+
```
152+
153+
### **C**
154+
155+
```c
156+
int singleNumber(int *nums, int numsSize) {
157+
int ans = 0;
158+
for (int i = 0; i < 32; i++) {
159+
int count = 0;
160+
for (int j = 0; j < numsSize; j++) {
161+
if (nums[j] >> i & 1) {
162+
count++;
163+
}
164+
}
165+
ans |= (uint)(count % 3) << i;
166+
}
167+
return ans;
168+
}
169+
```
170+
125171
### **Swift**
126172
127173
```swift

solution/0100-0199/0137.Single Number II/README_EN.md

+46
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,52 @@ public:
101101
};
102102
```
103103
104+
### **TypeScript**
105+
106+
```ts
107+
function singleNumber(nums: number[]): number {
108+
let ans = 0;
109+
for (let i = 0; i < 32; i++) {
110+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
111+
ans |= count % 3 << i;
112+
}
113+
return ans;
114+
}
115+
```
116+
117+
### **Rust**
118+
119+
```rust
120+
impl Solution {
121+
pub fn single_number(nums: Vec<i32>) -> i32 {
122+
let mut ans = 0;
123+
for i in 0..32 {
124+
let count = nums.iter().map(|v| v >> i & 1).sum::<i32>();
125+
ans |= count % 3 << i;
126+
}
127+
ans
128+
}
129+
}
130+
```
131+
132+
### **C**
133+
134+
```c
135+
int singleNumber(int *nums, int numsSize) {
136+
int ans = 0;
137+
for (int i = 0; i < 32; i++) {
138+
int count = 0;
139+
for (int j = 0; j < numsSize; j++) {
140+
if (nums[j] >> i & 1) {
141+
count++;
142+
}
143+
}
144+
ans |= (uint)(count % 3) << i;
145+
}
146+
return ans;
147+
}
148+
```
149+
104150
### **Swift**
105151
106152
```swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int singleNumber(int *nums, int numsSize) {
2+
int ans = 0;
3+
for (int i = 0; i < 32; i++) {
4+
int count = 0;
5+
for (int j = 0; j < numsSize; j++) {
6+
if (nums[j] >> i & 1) {
7+
count++;
8+
}
9+
}
10+
ans |= (uint)(count % 3) << i;
11+
}
12+
return ans;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn single_number(nums: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
for i in 0..32 {
5+
let count = nums.iter().map(|v| v >> i & 1).sum::<i32>();
6+
ans |= count % 3 << i;
7+
}
8+
ans
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function singleNumber(nums: number[]): number {
2+
let ans = 0;
3+
for (let i = 0; i < 32; i++) {
4+
const count = nums.reduce((r, v) => r + ((v >> i) & 1), 0);
5+
ans |= count % 3 << i;
6+
}
7+
return ans;
8+
}

0 commit comments

Comments
 (0)