File tree Expand file tree Collapse file tree 13 files changed +304
-0
lines changed
2441.Largest Positive Integer That Exists With Its Negative
2442.Count Number of Distinct Integers After Reverse Operations
2443.Sum of Number and Its Reverse Expand file tree Collapse file tree 13 files changed +304
-0
lines changed Original file line number Diff line number Diff line change @@ -130,7 +130,34 @@ func findMaxK(nums []int) int {
130
130
### ** TypeScript**
131
131
132
132
``` ts
133
+ function findMaxK(nums : number []): number {
134
+ const set = new Set (nums );
135
+ let res = - 1 ;
136
+ for (const num of set ) {
137
+ if (set .has (- num )) {
138
+ res = Math .max (num , res );
139
+ }
140
+ }
141
+ return res ;
142
+ }
143
+ ```
133
144
145
+ ### ** Rust**
146
+
147
+ ``` rust
148
+ use std :: collections :: HashSet ;
149
+ impl Solution {
150
+ pub fn find_max_k (nums : Vec <i32 >) -> i32 {
151
+ let set = nums . into_iter (). collect :: <HashSet <i32 >>();
152
+ let mut res = - 1 ;
153
+ for & num in set . iter () {
154
+ if set . contains (& (- num )) {
155
+ res = res . max (num );
156
+ }
157
+ }
158
+ res
159
+ }
160
+ }
134
161
```
135
162
136
163
### ** ...**
Original file line number Diff line number Diff line change @@ -118,7 +118,34 @@ func findMaxK(nums []int) int {
118
118
### ** TypeScript**
119
119
120
120
``` ts
121
+ function findMaxK(nums : number []): number {
122
+ const set = new Set (nums );
123
+ let res = - 1 ;
124
+ for (const num of set ) {
125
+ if (set .has (- num )) {
126
+ res = Math .max (num , res );
127
+ }
128
+ }
129
+ return res ;
130
+ }
131
+ ```
121
132
133
+ ### ** Rust**
134
+
135
+ ``` rust
136
+ use std :: collections :: HashSet ;
137
+ impl Solution {
138
+ pub fn find_max_k (nums : Vec <i32 >) -> i32 {
139
+ let set = nums . into_iter (). collect :: <HashSet <i32 >>();
140
+ let mut res = - 1 ;
141
+ for & num in set . iter () {
142
+ if set . contains (& (- num )) {
143
+ res = res . max (num );
144
+ }
145
+ }
146
+ res
147
+ }
148
+ }
122
149
```
123
150
124
151
### ** ...**
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+ impl Solution {
3
+ pub fn find_max_k ( nums : Vec < i32 > ) -> i32 {
4
+ let set = nums. into_iter ( ) . collect :: < HashSet < i32 > > ( ) ;
5
+ let mut res = -1 ;
6
+ for & num in set. iter ( ) {
7
+ if set. contains ( & ( -num) ) {
8
+ res = res. max ( num) ;
9
+ }
10
+ }
11
+ res
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ function findMaxK ( nums : number [ ] ) : number {
2
+ const set = new Set ( nums ) ;
3
+ let res = - 1 ;
4
+ for ( const num of set ) {
5
+ if ( set . has ( - num ) ) {
6
+ res = Math . max ( num , res ) ;
7
+ }
8
+ }
9
+ return res ;
10
+ }
Original file line number Diff line number Diff line change @@ -134,7 +134,37 @@ func countDistinctIntegers(nums []int) int {
134
134
### ** TypeScript**
135
135
136
136
``` ts
137
+ function countDistinctIntegers(nums : number []): number {
138
+ const n = nums .length ;
139
+ for (let i = 0 ; i < n ; i ++ ) {
140
+ nums .push (Number ([... (nums [i ] + ' ' )].reverse ().join (' ' )));
141
+ }
142
+ return new Set (nums ).size ;
143
+ }
144
+ ```
137
145
146
+ ### ** Rust**
147
+
148
+ ``` rust
149
+ use std :: collections :: HashSet ;
150
+ impl Solution {
151
+ pub fn count_distinct_integers (nums : Vec <i32 >) -> i32 {
152
+ let mut set = HashSet :: new ();
153
+ for i in 0 .. nums . len () {
154
+ let mut num = nums [i ];
155
+ set . insert (num );
156
+ set . insert ({
157
+ let mut item = 0 ;
158
+ while num > 0 {
159
+ item = item * 10 + num % 10 ;
160
+ num /= 10 ;
161
+ }
162
+ item
163
+ });
164
+ }
165
+ set . len () as i32
166
+ }
167
+ }
138
168
```
139
169
140
170
### ** ...**
Original file line number Diff line number Diff line change @@ -118,7 +118,37 @@ func countDistinctIntegers(nums []int) int {
118
118
### ** TypeScript**
119
119
120
120
``` ts
121
+ function countDistinctIntegers(nums : number []): number {
122
+ const n = nums .length ;
123
+ for (let i = 0 ; i < n ; i ++ ) {
124
+ nums .push (Number ([... (nums [i ] + ' ' )].reverse ().join (' ' )));
125
+ }
126
+ return new Set (nums ).size ;
127
+ }
128
+ ```
121
129
130
+ ### ** Rust**
131
+
132
+ ``` rust
133
+ use std :: collections :: HashSet ;
134
+ impl Solution {
135
+ pub fn count_distinct_integers (nums : Vec <i32 >) -> i32 {
136
+ let mut set = HashSet :: new ();
137
+ for i in 0 .. nums . len () {
138
+ let mut num = nums [i ];
139
+ set . insert (num );
140
+ set . insert ({
141
+ let mut item = 0 ;
142
+ while num > 0 {
143
+ item = item * 10 + num % 10 ;
144
+ num /= 10 ;
145
+ }
146
+ item
147
+ });
148
+ }
149
+ set . len () as i32
150
+ }
151
+ }
122
152
```
123
153
124
154
### ** ...**
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+ impl Solution {
3
+ pub fn count_distinct_integers ( nums : Vec < i32 > ) -> i32 {
4
+ let mut set = HashSet :: new ( ) ;
5
+ for i in 0 ..nums. len ( ) {
6
+ let mut num = nums[ i] ;
7
+ set. insert ( num) ;
8
+ set. insert ( {
9
+ let mut item = 0 ;
10
+ while num > 0 {
11
+ item = item * 10 + num % 10 ;
12
+ num /= 10 ;
13
+ }
14
+ item
15
+ } ) ;
16
+ }
17
+ set. len ( ) as i32
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ function countDistinctIntegers ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
3
+ for ( let i = 0 ; i < n ; i ++ ) {
4
+ nums . push ( Number ( [ ...( nums [ i ] + '' ) ] . reverse ( ) . join ( '' ) ) ) ;
5
+ }
6
+ return new Set ( nums ) . size ;
7
+ }
Original file line number Diff line number Diff line change @@ -129,10 +129,60 @@ func sumOfNumberAndReverse(num int) bool {
129
129
}
130
130
```
131
131
132
+ ### ** C**
133
+
134
+ ``` c
135
+ bool sumOfNumberAndReverse (int num) {
136
+ for (int i = 0; i <= num; i++) {
137
+ int t = i;
138
+ int j = 0;
139
+ while (t > 0) {
140
+ j = j * 10 + t % 10;
141
+ t /= 10;
142
+ }
143
+ if (i + j == num) {
144
+ return 1;
145
+ }
146
+ }
147
+ return 0;
148
+ }
149
+ ```
150
+
132
151
### **TypeScript**
133
152
134
153
```ts
154
+ function sumOfNumberAndReverse(num: number): boolean {
155
+ for (let i = 0; i <= num; i++) {
156
+ if (i + Number([...(i + '')].reverse().join('')) === num) {
157
+ return true;
158
+ }
159
+ }
160
+ return false;
161
+ }
162
+ ```
135
163
164
+ ### ** Rust**
165
+
166
+ ``` rust
167
+ impl Solution {
168
+ pub fn sum_of_number_and_reverse (num : i32 ) -> bool {
169
+ for i in 0 ..= num {
170
+ if i + {
171
+ let mut t = i ;
172
+ let mut j = 0 ;
173
+ while t > 0 {
174
+ j = j * 10 + t % 10 ;
175
+ t /= 10 ;
176
+ }
177
+ j
178
+ } == num
179
+ {
180
+ return true ;
181
+ }
182
+ }
183
+ false
184
+ }
185
+ }
136
186
```
137
187
138
188
### ** ...**
Original file line number Diff line number Diff line change @@ -111,10 +111,60 @@ func sumOfNumberAndReverse(num int) bool {
111
111
}
112
112
```
113
113
114
+ ### ** C**
115
+
116
+ ``` c
117
+ bool sumOfNumberAndReverse (int num) {
118
+ for (int i = 0; i <= num; i++) {
119
+ int t = i;
120
+ int j = 0;
121
+ while (t > 0) {
122
+ j = j * 10 + t % 10;
123
+ t /= 10;
124
+ }
125
+ if (i + j == num) {
126
+ return 1;
127
+ }
128
+ }
129
+ return 0;
130
+ }
131
+ ```
132
+
114
133
### **TypeScript**
115
134
116
135
```ts
136
+ function sumOfNumberAndReverse(num: number): boolean {
137
+ for (let i = 0; i <= num; i++) {
138
+ if (i + Number([...(i + '')].reverse().join('')) === num) {
139
+ return true;
140
+ }
141
+ }
142
+ return false;
143
+ }
144
+ ```
117
145
146
+ ### ** Rust**
147
+
148
+ ``` rust
149
+ impl Solution {
150
+ pub fn sum_of_number_and_reverse (num : i32 ) -> bool {
151
+ for i in 0 ..= num {
152
+ if i + {
153
+ let mut t = i ;
154
+ let mut j = 0 ;
155
+ while t > 0 {
156
+ j = j * 10 + t % 10 ;
157
+ t /= 10 ;
158
+ }
159
+ j
160
+ } == num
161
+ {
162
+ return true ;
163
+ }
164
+ }
165
+ false
166
+ }
167
+ }
118
168
```
119
169
120
170
### ** ...**
You can’t perform that action at this time.
0 commit comments