File tree 3 files changed +125
-0
lines changed
solution/0800-0899/0821.Shortest Distance to a Character
3 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,57 @@ function shortestToChar(s: string, c: string): number[] {
84
84
}
85
85
```
86
86
87
+ ``` ts
88
+ function shortestToChar(s : string , c : string ): number [] {
89
+ const n = s .length ;
90
+ const idxs = [];
91
+ for (let i = 0 ; i < n ; i ++ ) {
92
+ if (s [i ] === c ) {
93
+ idxs .push (i );
94
+ }
95
+ }
96
+ idxs .push (Infinity );
97
+
98
+ const res = new Array (n );
99
+ let i = 0 ;
100
+ for (let j = 0 ; j < n ; j ++ ) {
101
+ if (Math .abs (idxs [i ] - j ) > Math .abs (idxs [i + 1 ] - j )) {
102
+ i ++ ;
103
+ }
104
+ res [j ] = Math .abs (idxs [i ] - j );
105
+ }
106
+ return res ;
107
+ }
108
+ ```
109
+
110
+ ### ** Rust**
111
+
112
+ ``` rust
113
+ impl Solution {
114
+ pub fn shortest_to_char (s : String , c : char ) -> Vec <i32 > {
115
+ let c = c as u8 ;
116
+ let s = s . as_bytes ();
117
+ let n = s . len ();
118
+ let mut res = vec! [i32 :: MAX ; n ];
119
+ let mut pre = i32 :: MAX ;
120
+ for i in 0 .. n {
121
+ if s [i ] == c {
122
+ pre = i as i32 ;
123
+ }
124
+ res [i ] = i32 :: abs (i as i32 - pre );
125
+ }
126
+ pre = i32 :: MAX ;
127
+ for i in (0 .. n ). rev () {
128
+ if s [i ] == c {
129
+ pre = i as i32 ;
130
+ }
131
+ res [i ] = res [i ]. min (i32 :: abs (i as i32 - pre ));
132
+ }
133
+ res
134
+ }
135
+ }
136
+ ```
137
+
87
138
### ** ...**
88
139
89
140
```
Original file line number Diff line number Diff line change @@ -73,6 +73,57 @@ function shortestToChar(s: string, c: string): number[] {
73
73
}
74
74
```
75
75
76
+ ``` ts
77
+ function shortestToChar(s : string , c : string ): number [] {
78
+ const n = s .length ;
79
+ const idxs = [];
80
+ for (let i = 0 ; i < n ; i ++ ) {
81
+ if (s [i ] === c ) {
82
+ idxs .push (i );
83
+ }
84
+ }
85
+ idxs .push (Infinity );
86
+
87
+ const res = new Array (n );
88
+ let i = 0 ;
89
+ for (let j = 0 ; j < n ; j ++ ) {
90
+ if (Math .abs (idxs [i ] - j ) > Math .abs (idxs [i + 1 ] - j )) {
91
+ i ++ ;
92
+ }
93
+ res [j ] = Math .abs (idxs [i ] - j );
94
+ }
95
+ return res ;
96
+ }
97
+ ```
98
+
99
+ ### ** Rust**
100
+
101
+ ``` rust
102
+ impl Solution {
103
+ pub fn shortest_to_char (s : String , c : char ) -> Vec <i32 > {
104
+ let c = c as u8 ;
105
+ let s = s . as_bytes ();
106
+ let n = s . len ();
107
+ let mut res = vec! [i32 :: MAX ; n ];
108
+ let mut pre = i32 :: MAX ;
109
+ for i in 0 .. n {
110
+ if s [i ] == c {
111
+ pre = i as i32 ;
112
+ }
113
+ res [i ] = i32 :: abs (i as i32 - pre );
114
+ }
115
+ pre = i32 :: MAX ;
116
+ for i in (0 .. n ). rev () {
117
+ if s [i ] == c {
118
+ pre = i as i32 ;
119
+ }
120
+ res [i ] = res [i ]. min (i32 :: abs (i as i32 - pre ));
121
+ }
122
+ res
123
+ }
124
+ }
125
+ ```
126
+
76
127
### ** ...**
77
128
78
129
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn shortest_to_char ( s : String , c : char ) -> Vec < i32 > {
3
+ let c = c as u8 ;
4
+ let s = s. as_bytes ( ) ;
5
+ let n = s. len ( ) ;
6
+ let mut res = vec ! [ i32 :: MAX ; n] ;
7
+ let mut pre = i32:: MAX ;
8
+ for i in 0 ..n {
9
+ if s[ i] == c {
10
+ pre = i as i32 ;
11
+ }
12
+ res[ i] = i32:: abs ( i as i32 - pre) ;
13
+ }
14
+ pre = i32:: MAX ;
15
+ for i in ( 0 ..n) . rev ( ) {
16
+ if s[ i] == c {
17
+ pre = i as i32 ;
18
+ }
19
+ res[ i] = res[ i] . min ( i32:: abs ( i as i32 - pre) ) ;
20
+ }
21
+ res
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments