File tree Expand file tree Collapse file tree 9 files changed +286
-36
lines changed
0200-0299/0202.Happy Number
0300-0399/0350.Intersection of Two Arrays II Expand file tree Collapse file tree 9 files changed +286
-36
lines changed Original file line number Diff line number Diff line change @@ -107,25 +107,72 @@ class Solution {
107
107
}
108
108
```
109
109
110
- ### ** Rust **
110
+ ### ** C++ **
111
111
112
- ``` rust
113
- impl Solution {
114
- fn get_next (mut n : i32 ) -> i32 {
115
- let mut res = 0 ;
116
- while n != 0 {
117
- res += (n % 10 ). pow (2 );
118
- n /= 10 ;
112
+ ``` cpp
113
+ class Solution {
114
+ public:
115
+ bool isHappy(int n) {
116
+ auto getNext = [ ] (int n) {
117
+ int res = 0;
118
+ while (n) {
119
+ res += pow(n % 10, 2);
120
+ n /= 10;
121
+ }
122
+ return res;
123
+ };
124
+ int slow = n;
125
+ int fast = getNext(n);
126
+ while (slow != fast) {
127
+ slow = getNext(slow);
128
+ fast = getNext(getNext(fast));
129
+ }
130
+ return slow == 1;
131
+ }
132
+ };
133
+ ```
134
+
135
+ ### **TypeScript**
136
+
137
+ ```ts
138
+ function isHappy(n: number): boolean {
139
+ const getNext = (n: number) => {
140
+ let res = 0;
141
+ while (n !== 0) {
142
+ res += (n % 10) ** 2;
143
+ n = Math.floor(n / 10);
119
144
}
120
- res
145
+ return res;
146
+ };
147
+
148
+ let slow = n;
149
+ let fast = getNext(n);
150
+ while (slow !== fast) {
151
+ slow = getNext(slow);
152
+ fast = getNext(getNext(fast));
121
153
}
154
+ return fast === 1;
155
+ }
156
+ ```
122
157
158
+ ### ** Rust**
159
+
160
+ ``` rust
161
+ impl Solution {
123
162
pub fn is_happy (n : i32 ) -> bool {
163
+ let get_next = | mut n : i32 | {
164
+ let mut res = 0 ;
165
+ while n != 0 {
166
+ res += (n % 10 ). pow (2 );
167
+ n /= 10 ;
168
+ }
169
+ res
170
+ };
124
171
let mut slow = n ;
125
- let mut fast = Self :: get_next (n );
172
+ let mut fast = get_next (n );
126
173
while slow != fast {
127
- slow = Self :: get_next (slow );
128
- fast = Self :: get_next (Self :: get_next (fast ));
174
+ slow = get_next (slow );
175
+ fast = get_next (get_next (fast ));
129
176
}
130
177
slow == 1
131
178
}
Original file line number Diff line number Diff line change @@ -91,25 +91,72 @@ class Solution {
91
91
}
92
92
```
93
93
94
- ### ** Rust **
94
+ ### ** C++ **
95
95
96
- ``` rust
97
- impl Solution {
98
- fn get_next (mut n : i32 ) -> i32 {
99
- let mut res = 0 ;
100
- while n != 0 {
101
- res += (n % 10 ). pow (2 );
102
- n /= 10 ;
96
+ ``` cpp
97
+ class Solution {
98
+ public:
99
+ bool isHappy(int n) {
100
+ auto getNext = [ ] (int n) {
101
+ int res = 0;
102
+ while (n) {
103
+ res += pow(n % 10, 2);
104
+ n /= 10;
105
+ }
106
+ return res;
107
+ };
108
+ int slow = n;
109
+ int fast = getNext(n);
110
+ while (slow != fast) {
111
+ slow = getNext(slow);
112
+ fast = getNext(getNext(fast));
113
+ }
114
+ return slow == 1;
115
+ }
116
+ };
117
+ ```
118
+
119
+ ### **TypeScript**
120
+
121
+ ```ts
122
+ function isHappy(n: number): boolean {
123
+ const getNext = (n: number) => {
124
+ let res = 0;
125
+ while (n !== 0) {
126
+ res += (n % 10) ** 2;
127
+ n = Math.floor(n / 10);
103
128
}
104
- res
129
+ return res;
130
+ };
131
+
132
+ let slow = n;
133
+ let fast = getNext(n);
134
+ while (slow !== fast) {
135
+ slow = getNext(slow);
136
+ fast = getNext(getNext(fast));
105
137
}
138
+ return fast === 1;
139
+ }
140
+ ```
106
141
142
+ ### ** Rust**
143
+
144
+ ``` rust
145
+ impl Solution {
107
146
pub fn is_happy (n : i32 ) -> bool {
147
+ let get_next = | mut n : i32 | {
148
+ let mut res = 0 ;
149
+ while n != 0 {
150
+ res += (n % 10 ). pow (2 );
151
+ n /= 10 ;
152
+ }
153
+ res
154
+ };
108
155
let mut slow = n ;
109
- let mut fast = Self :: get_next (n );
156
+ let mut fast = get_next (n );
110
157
while slow != fast {
111
- slow = Self :: get_next (slow );
112
- fast = Self :: get_next (Self :: get_next (fast ));
158
+ slow = get_next (slow );
159
+ fast = get_next (get_next (fast ));
113
160
}
114
161
slow == 1
115
162
}
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isHappy (int n) {
4
+ auto getNext = [](int n) {
5
+ int res = 0 ;
6
+ while (n) {
7
+ res += pow (n % 10 , 2 );
8
+ n /= 10 ;
9
+ }
10
+ return res;
11
+ };
12
+ int slow = n;
13
+ int fast = getNext (n);
14
+ while (slow != fast) {
15
+ slow = getNext (slow);
16
+ fast = getNext (getNext (fast));
17
+ }
18
+ return slow == 1 ;
19
+ }
20
+ };
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
- fn get_next ( mut n : i32 ) -> i32 {
3
- let mut res = 0 ;
4
- while n != 0 {
5
- res += ( n % 10 ) . pow ( 2 ) ;
6
- n /= 10 ;
7
- }
8
- res
9
- }
10
-
11
2
pub fn is_happy ( n : i32 ) -> bool {
3
+ let get_next = |mut n : i32 | {
4
+ let mut res = 0 ;
5
+ while n != 0 {
6
+ res += ( n % 10 ) . pow ( 2 ) ;
7
+ n /= 10 ;
8
+ }
9
+ res
10
+ } ;
12
11
let mut slow = n;
13
- let mut fast = Self :: get_next ( n) ;
12
+ let mut fast = get_next ( n) ;
14
13
while slow != fast {
15
- slow = Self :: get_next ( slow) ;
16
- fast = Self :: get_next ( Self :: get_next ( fast) ) ;
14
+ slow = get_next ( slow) ;
15
+ fast = get_next ( get_next ( fast) ) ;
17
16
}
18
17
slow == 1
19
18
}
Original file line number Diff line number Diff line change
1
+ function isHappy ( n : number ) : boolean {
2
+ const getNext = ( n : number ) => {
3
+ let res = 0 ;
4
+ while ( n !== 0 ) {
5
+ res += ( n % 10 ) ** 2 ;
6
+ n = Math . floor ( n / 10 ) ;
7
+ }
8
+ return res ;
9
+ } ;
10
+
11
+ let slow = n ;
12
+ let fast = getNext ( n ) ;
13
+ while ( slow !== fast ) {
14
+ slow = getNext ( slow ) ;
15
+ fast = getNext ( getNext ( fast ) ) ;
16
+ }
17
+ return fast === 1 ;
18
+ }
Original file line number Diff line number Diff line change @@ -158,6 +158,49 @@ func intersect(nums1 []int, nums2 []int) []int {
158
158
}
159
159
```
160
160
161
+ ### ** TypeScript**
162
+
163
+ ``` ts
164
+ function intersect(nums1 : number [], nums2 : number []): number [] {
165
+ const map = new Map <number , number >();
166
+ for (const num of nums1 ) {
167
+ map .set (num , (map .get (num ) ?? 0 ) + 1 );
168
+ }
169
+
170
+ const res = [];
171
+ for (const num of nums2 ) {
172
+ if (map .has (num ) && map .get (num ) !== 0 ) {
173
+ res .push (num );
174
+ map .set (num , map .get (num ) - 1 );
175
+ }
176
+ }
177
+ return res ;
178
+ }
179
+ ```
180
+
181
+ ### ** Rust**
182
+
183
+ ``` rust
184
+ use std :: collections :: HashMap ;
185
+ impl Solution {
186
+ pub fn intersect (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <i32 > {
187
+ let mut map = HashMap :: new ();
188
+ for num in nums1 . iter () {
189
+ * map . entry (num ). or_insert (0 ) += 1 ;
190
+ }
191
+
192
+ let mut res = vec! [];
193
+ for num in nums2 . iter () {
194
+ if map . contains_key (num ) && map . get (num ). unwrap () != & 0 {
195
+ map . insert (num , map . get (& num ). unwrap () - 1 );
196
+ res . push (* num );
197
+ }
198
+ }
199
+ res
200
+ }
201
+ }
202
+ ```
203
+
161
204
### ** ...**
162
205
163
206
```
Original file line number Diff line number Diff line change @@ -147,6 +147,49 @@ func intersect(nums1 []int, nums2 []int) []int {
147
147
}
148
148
```
149
149
150
+ ### ** TypeScript**
151
+
152
+ ``` ts
153
+ function intersect(nums1 : number [], nums2 : number []): number [] {
154
+ const map = new Map <number , number >();
155
+ for (const num of nums1 ) {
156
+ map .set (num , (map .get (num ) ?? 0 ) + 1 );
157
+ }
158
+
159
+ const res = [];
160
+ for (const num of nums2 ) {
161
+ if (map .has (num ) && map .get (num ) !== 0 ) {
162
+ res .push (num );
163
+ map .set (num , map .get (num ) - 1 );
164
+ }
165
+ }
166
+ return res ;
167
+ }
168
+ ```
169
+
170
+ ### ** Rust**
171
+
172
+ ``` rust
173
+ use std :: collections :: HashMap ;
174
+ impl Solution {
175
+ pub fn intersect (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <i32 > {
176
+ let mut map = HashMap :: new ();
177
+ for num in nums1 . iter () {
178
+ * map . entry (num ). or_insert (0 ) += 1 ;
179
+ }
180
+
181
+ let mut res = vec! [];
182
+ for num in nums2 . iter () {
183
+ if map . contains_key (num ) && map . get (num ). unwrap () != & 0 {
184
+ map . insert (num , map . get (& num ). unwrap () - 1 );
185
+ res . push (* num );
186
+ }
187
+ }
188
+ res
189
+ }
190
+ }
191
+ ```
192
+
150
193
### ** ...**
151
194
152
195
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+ impl Solution {
3
+ pub fn intersect ( nums1 : Vec < i32 > , nums2 : Vec < i32 > ) -> Vec < i32 > {
4
+ let mut map = HashMap :: new ( ) ;
5
+ for num in nums1. iter ( ) {
6
+ * map. entry ( num) . or_insert ( 0 ) += 1 ;
7
+ }
8
+
9
+ let mut res = vec ! [ ] ;
10
+ for num in nums2. iter ( ) {
11
+ if map. contains_key ( num) && map. get ( num) . unwrap ( ) != & 0 {
12
+ map. insert ( num, map. get ( & num) . unwrap ( ) - 1 ) ;
13
+ res. push ( * num) ;
14
+ }
15
+ }
16
+ res
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ function intersect ( nums1 : number [ ] , nums2 : number [ ] ) : number [ ] {
2
+ const map = new Map < number , number > ( ) ;
3
+ for ( const num of nums1 ) {
4
+ map . set ( num , ( map . get ( num ) ?? 0 ) + 1 ) ;
5
+ }
6
+
7
+ const res = [ ] ;
8
+ for ( const num of nums2 ) {
9
+ if ( map . has ( num ) && map . get ( num ) !== 0 ) {
10
+ res . push ( num ) ;
11
+ map . set ( num , map . get ( num ) - 1 ) ;
12
+ }
13
+ }
14
+ return res ;
15
+ }
You can’t perform that action at this time.
0 commit comments