File tree 3 files changed +136
-0
lines changed
solution/2700-2799/2729.Check if The Number is Fascinating
3 files changed +136
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,66 @@ function isFascinating(n: number): boolean {
122
122
}
123
123
```
124
124
125
+ ### ** Rust**
126
+
127
+ ``` rust
128
+ impl Solution {
129
+ pub fn is_fascinating (n : i32 ) -> bool {
130
+ let s = format! (" {}{}{}" , n , n * 2 , n * 3 );
131
+
132
+ let mut cnt = vec! [0 ; 10 ];
133
+ for c in s . chars () {
134
+ let t = c as usize - '0' as usize ;
135
+ cnt [t ] += 1 ;
136
+ if cnt [t ] > 1 {
137
+ return false ;
138
+ }
139
+ }
140
+
141
+ cnt [0 ] == 0 && s . len () == 9
142
+ }
143
+ }
144
+ ```
145
+
146
+ ``` rust
147
+ use std :: collections :: HashMap ;
148
+
149
+ impl Solution {
150
+ pub fn is_fascinating (mut n : i32 ) -> bool {
151
+ let mut i = n * 2 ;
152
+ let mut j = n * 3 ;
153
+
154
+ let mut hash = HashMap :: new ();
155
+
156
+ while n != 0 {
157
+ let cnt = hash . entry (n % 10 ). or_insert (0 );
158
+ * cnt += 1 ;
159
+ n /= 10 ;
160
+ }
161
+
162
+ while i != 0 {
163
+ let cnt = hash . entry (i % 10 ). or_insert (0 );
164
+ * cnt += 1 ;
165
+ i /= 10 ;
166
+ }
167
+
168
+ while j != 0 {
169
+ let cnt = hash . entry (j % 10 ). or_insert (0 );
170
+ * cnt += 1 ;
171
+ j /= 10 ;
172
+ }
173
+
174
+ for k in 1 ..= 9 {
175
+ if ! hash . contains_key (& k ) || hash [& k ] > 1 {
176
+ return false
177
+ }
178
+ }
179
+
180
+ ! hash . contains_key (& 0 )
181
+ }
182
+ }
183
+ ```
184
+
125
185
### ** ...**
126
186
127
187
```
Original file line number Diff line number Diff line change @@ -108,6 +108,66 @@ function isFascinating(n: number): boolean {
108
108
}
109
109
```
110
110
111
+ ### ** Rust**
112
+
113
+ ``` rust
114
+ impl Solution {
115
+ pub fn is_fascinating (n : i32 ) -> bool {
116
+ let s = format! (" {}{}{}" , n , n * 2 , n * 3 );
117
+
118
+ let mut cnt = vec! [0 ; 10 ];
119
+ for c in s . chars () {
120
+ let t = c as usize - '0' as usize ;
121
+ cnt [t ] += 1 ;
122
+ if cnt [t ] > 1 {
123
+ return false ;
124
+ }
125
+ }
126
+
127
+ cnt [0 ] == 0 && s . len () == 9
128
+ }
129
+ }
130
+ ```
131
+
132
+ ``` rust
133
+ use std :: collections :: HashMap ;
134
+
135
+ impl Solution {
136
+ pub fn is_fascinating (mut n : i32 ) -> bool {
137
+ let mut i = n * 2 ;
138
+ let mut j = n * 3 ;
139
+
140
+ let mut hash = HashMap :: new ();
141
+
142
+ while n != 0 {
143
+ let cnt = hash . entry (n % 10 ). or_insert (0 );
144
+ * cnt += 1 ;
145
+ n /= 10 ;
146
+ }
147
+
148
+ while i != 0 {
149
+ let cnt = hash . entry (i % 10 ). or_insert (0 );
150
+ * cnt += 1 ;
151
+ i /= 10 ;
152
+ }
153
+
154
+ while j != 0 {
155
+ let cnt = hash . entry (j % 10 ). or_insert (0 );
156
+ * cnt += 1 ;
157
+ j /= 10 ;
158
+ }
159
+
160
+ for k in 1 ..= 9 {
161
+ if ! hash . contains_key (& k ) || hash [& k ] > 1 {
162
+ return false
163
+ }
164
+ }
165
+
166
+ ! hash . contains_key (& 0 )
167
+ }
168
+ }
169
+ ```
170
+
111
171
### ** ...**
112
172
113
173
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn is_fascinating ( n : i32 ) -> bool {
3
+ let s = format ! ( "{}{}{}" , n, n * 2 , n * 3 ) ;
4
+
5
+ let mut cnt = vec ! [ 0 ; 10 ] ;
6
+ for c in s. chars ( ) {
7
+ let t = c as usize - '0' as usize ;
8
+ cnt[ t] += 1 ;
9
+ if cnt[ t] > 1 {
10
+ return false ;
11
+ }
12
+ }
13
+
14
+ cnt[ 0 ] == 0 && s. len ( ) == 9
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments