Skip to content

Commit 7d12a0d

Browse files
committed
feat: add solutions to lc problem: No.0136
No.0136.Single Number
1 parent 41118c7 commit 7d12a0d

File tree

6 files changed

+49
-51
lines changed

6 files changed

+49
-51
lines changed

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ class Solution {
105105
public:
106106
int singleNumber(vector<int>& nums) {
107107
int ans = 0;
108-
for (int v : nums) ans ^= v;
108+
for (int v : nums) {
109+
ans ^= v;
110+
}
109111
return ans;
110112
}
111113
};
@@ -130,11 +132,7 @@ func singleNumber(nums []int) (ans int) {
130132
* @return {number}
131133
*/
132134
var singleNumber = function (nums) {
133-
let ans = 0;
134-
for (const v of nums) {
135-
ans ^= v;
136-
}
137-
return ans;
135+
return nums.reduce((a, b) => a ^ b);
138136
};
139137
```
140138

@@ -173,14 +171,17 @@ int singleNumber(int* nums, int numsSize) {
173171
```swift
174172
class Solution {
175173
func singleNumber(_ nums: [Int]) -> Int {
176-
var a = nums.sorted()
177-
var n = a.count
178-
for i in stride(from: 0, through: n - 2, by: 2) {
179-
if a[i] != a[i + 1] {
180-
return a[i]
181-
}
182-
}
183-
return a[n - 1]
174+
return nums.reduce(0, ^)
175+
}
176+
}
177+
```
178+
179+
### **C#**
180+
181+
```cs
182+
public class Solution {
183+
public int SingleNumber(int[] nums) {
184+
return nums.Aggregate(0, (a, b) => a ^ b);
184185
}
185186
}
186187
```

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class Solution {
6969
public:
7070
int singleNumber(vector<int>& nums) {
7171
int ans = 0;
72-
for (int v : nums) ans ^= v;
72+
for (int v : nums) {
73+
ans ^= v;
74+
}
7375
return ans;
7476
}
7577
};
@@ -94,11 +96,7 @@ func singleNumber(nums []int) (ans int) {
9496
* @return {number}
9597
*/
9698
var singleNumber = function (nums) {
97-
let ans = 0;
98-
for (const v of nums) {
99-
ans ^= v;
100-
}
101-
return ans;
99+
return nums.reduce((a, b) => a ^ b);
102100
};
103101
```
104102

@@ -137,14 +135,17 @@ int singleNumber(int* nums, int numsSize) {
137135
```swift
138136
class Solution {
139137
func singleNumber(_ nums: [Int]) -> Int {
140-
var a = nums.sorted()
141-
var n = a.count
142-
for i in stride(from: 0, through: n - 2, by: 2) {
143-
if a[i] != a[i + 1] {
144-
return a[i]
145-
}
146-
}
147-
return a[n - 1]
138+
return nums.reduce(0, ^)
139+
}
140+
}
141+
```
142+
143+
### **C#**
144+
145+
```cs
146+
public class Solution {
147+
public int SingleNumber(int[] nums) {
148+
return nums.Aggregate(0, (a, b) => a ^ b);
148149
}
149150
}
150151
```
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class Solution {
2-
public:
3-
int singleNumber(vector<int>& nums) {
4-
int ans = 0;
5-
for (int v : nums) ans ^= v;
6-
return ans;
7-
}
1+
class Solution {
2+
public:
3+
int singleNumber(vector<int>& nums) {
4+
int ans = 0;
5+
for (int v : nums) {
6+
ans ^= v;
7+
}
8+
return ans;
9+
}
810
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public int SingleNumber(int[] nums) {
3+
return nums.Aggregate(0, (a, b) => a ^ b);
4+
}
5+
}

solution/0100-0199/0136.Single Number/Solution.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
* @return {number}
44
*/
55
var singleNumber = function (nums) {
6-
let ans = 0;
7-
for (const v of nums) {
8-
ans ^= v;
9-
}
10-
return ans;
6+
return nums.reduce((a, b) => a ^ b);
117
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
class Solution {
2-
func singleNumber(_ nums: [Int]) -> Int {
3-
var a = nums.sorted()
4-
var n = a.count
5-
for i in stride(from: 0, through: n - 2, by: 2) {
6-
if a[i] != a[i + 1] {
7-
return a[i]
8-
}
9-
}
10-
return a[n - 1]
11-
}
1+
class Solution {
2+
func singleNumber(_ nums: [Int]) -> Int {
3+
return nums.reduce(0, ^)
4+
}
125
}

0 commit comments

Comments
 (0)