File tree 10 files changed +177
-16
lines changed
10 files changed +177
-16
lines changed Original file line number Diff line number Diff line change @@ -138,20 +138,36 @@ var singleNumber = function (nums) {
138
138
};
139
139
```
140
140
141
+ ### ** TypeScript**
142
+
143
+ ``` ts
144
+ function singleNumber(nums : number []): number {
145
+ return nums .reduce ((r , v ) => r ^ v );
146
+ }
147
+ ```
148
+
141
149
### ** Rust**
142
150
143
151
``` rust
144
152
impl Solution {
145
153
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 ()
151
155
}
152
156
}
153
157
```
154
158
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
+
155
171
### **Swift**
156
172
157
173
```swift
Original file line number Diff line number Diff line change @@ -102,20 +102,36 @@ var singleNumber = function (nums) {
102
102
};
103
103
```
104
104
105
+ ### ** TypeScript**
106
+
107
+ ``` ts
108
+ function singleNumber(nums : number []): number {
109
+ return nums .reduce ((r , v ) => r ^ v );
110
+ }
111
+ ```
112
+
105
113
### ** Rust**
106
114
107
115
``` rust
108
116
impl Solution {
109
117
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 ()
115
119
}
116
120
}
117
121
```
118
122
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
+
119
135
### **Swift**
120
136
121
137
```swift
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change 1
1
impl Solution {
2
2
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 ( )
8
4
}
9
- }
5
+ }
Original file line number Diff line number Diff line change
1
+ function singleNumber ( nums : number [ ] ) : number {
2
+ return nums . reduce ( ( r , v ) => r ^ v ) ;
3
+ }
Original file line number Diff line number Diff line change @@ -122,6 +122,52 @@ public:
122
122
};
123
123
```
124
124
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
+
125
171
### **Swift**
126
172
127
173
```swift
Original file line number Diff line number Diff line change @@ -101,6 +101,52 @@ public:
101
101
};
102
102
```
103
103
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
+
104
150
### **Swift**
105
151
106
152
```swift
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments