Skip to content

Commit 0d48dc4

Browse files
authored
feat: update solutions for lc No.2515 (#5161)
1 parent 43d8cf8 commit 0d48dc4

10 files changed

Lines changed: 132 additions & 206 deletions

File tree

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md

Lines changed: 42 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ tags:
8181

8282
### 方法一:一次遍历
8383

84-
遍历数组,找到与 target 相等的单词,计算其与 startIndex 的距离 $t$,则此时的最短距离为 $min(t, n - t)$,我们只需要不断更新最小值即可。
84+
我们遍历数组 $\textit{words}$,找到与 $\textit{target}$ 相等的单词,计算其与 $\textit{startIndex}$ 的距离 $t$,则此时的最短距离为 $\min(t, n - t)$,我们只需要不断更新最小值即可。
8585

86-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
86+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
8787

8888
<!-- tabs:start -->
8989

9090
#### Python3
9191

9292
```python
9393
class Solution:
94-
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
94+
def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:
9595
n = len(words)
9696
ans = n
9797
for i, w in enumerate(words):
@@ -105,12 +105,11 @@ class Solution:
105105

106106
```java
107107
class Solution {
108-
public int closetTarget(String[] words, String target, int startIndex) {
108+
public int closestTarget(String[] words, String target, int startIndex) {
109109
int n = words.length;
110110
int ans = n;
111-
for (int i = 0; i < n; ++i) {
112-
String w = words[i];
113-
if (w.equals(target)) {
111+
for (int i = 0; i < n; i++) {
112+
if (words[i].equals(target)) {
114113
int t = Math.abs(i - startIndex);
115114
ans = Math.min(ans, Math.min(t, n - t));
116115
}
@@ -125,14 +124,13 @@ class Solution {
125124
```cpp
126125
class Solution {
127126
public:
128-
int closetTarget(vector<string>& words, string target, int startIndex) {
127+
int closestTarget(vector<string>& words, string target, int startIndex) {
129128
int n = words.size();
130129
int ans = n;
131-
for (int i = 0; i < n; ++i) {
132-
auto w = words[i];
133-
if (w == target) {
130+
for (int i = 0; i < n; i++) {
131+
if (words[i] == target) {
134132
int t = abs(i - startIndex);
135-
ans = min(ans, min(t, n - t));
133+
ans = min({ans, t, n - t});
136134
}
137135
}
138136
return ans == n ? -1 : ans;
@@ -143,106 +141,77 @@ public:
143141
#### Go
144142
145143
```go
146-
func closetTarget(words []string, target string, startIndex int) int {
144+
func closestTarget(words []string, target string, startIndex int) int {
147145
n := len(words)
148146
ans := n
149147
for i, w := range words {
150148
if w == target {
151-
t := abs(i - startIndex)
152-
ans = min(ans, min(t, n-t))
149+
t := i - startIndex
150+
if t < 0 {
151+
t = -t
152+
}
153+
ans = min(ans, t, n-t)
153154
}
154155
}
155156
if ans == n {
156157
return -1
157158
}
158159
return ans
159160
}
160-
161-
func abs(x int) int {
162-
if x < 0 {
163-
return -x
164-
}
165-
return x
166-
}
167161
```
168162

169163
#### TypeScript
170164

171165
```ts
172-
function closetTarget(words: string[], target: string, startIndex: number): number {
166+
function closestTarget(words: string[], target: string, startIndex: number): number {
173167
const n = words.length;
174-
for (let i = 0; i <= n >> 1; i++) {
175-
if (words[(startIndex - i + n) % n] === target || words[(startIndex + i) % n] === target) {
176-
return i;
168+
let ans = n;
169+
for (let i = 0; i < n; i++) {
170+
if (words[i] === target) {
171+
const t = Math.abs(i - startIndex);
172+
ans = Math.min(ans, t, n - t);
177173
}
178174
}
179-
return -1;
175+
return ans === n ? -1 : ans;
180176
}
181177
```
182178

183179
#### Rust
184180

185181
```rust
186182
impl Solution {
187-
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
188-
let start_index = start_index as usize;
189-
let n = words.len();
190-
for i in 0..=n >> 1 {
191-
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
192-
{
193-
return i as i32;
183+
pub fn closest_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
184+
let n = words.len() as i32;
185+
let mut ans = n;
186+
for (i, w) in words.iter().enumerate() {
187+
if w == &target {
188+
let t = (i as i32 - start_index).abs();
189+
ans = ans.min(t.min(n - t));
194190
}
195191
}
196-
-1
192+
if ans == n { -1 } else { ans }
197193
}
198194
}
199195
```
200196

201197
#### C
202198

203199
```c
204-
int closetTarget(char** words, int wordsSize, char* target, int startIndex) {
205-
for (int i = 0; i <= wordsSize >> 1; i++) {
206-
if (strcmp(words[(startIndex - i + wordsSize) % wordsSize], target) == 0 || strcmp(words[(startIndex + i) % wordsSize], target) == 0) {
207-
return i;
208-
}
209-
}
210-
return -1;
211-
}
212-
```
213-
214-
<!-- tabs:end -->
215-
216-
<!-- solution:end -->
217-
218-
<!-- solution:start -->
200+
#define min(a, b) ((a) < (b) ? (a) : (b))
219201

220-
### 方法二
202+
int closestTarget(char** words, int wordsSize, char* target, int startIndex) {
203+
int n = wordsSize;
204+
int ans = n;
221205

222-
<!-- tabs:start -->
223-
224-
#### Rust
225-
226-
```rust
227-
use std::cmp::min;
228-
229-
impl Solution {
230-
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
231-
let mut ans = words.len();
232-
233-
for (i, w) in words.iter().enumerate() {
234-
if *w == target {
235-
let t = ((i as i32) - start_index).abs();
236-
ans = min(ans, min(t as usize, words.len() - (t as usize)));
237-
}
206+
for (int i = 0; i < n; i++) {
207+
if (strcmp(words[i], target) == 0) {
208+
int t = abs(i - startIndex);
209+
int dist = min(t, n - t);
210+
ans = min(ans, dist);
238211
}
239-
240-
if ans == words.len() {
241-
return -1;
242-
}
243-
244-
ans as i32
245212
}
213+
214+
return ans == n ? -1 : ans;
246215
}
247216
```
248217

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ The shortest distance to reach &quot;leetcode&quot; is 1.</pre>
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Single Traversal
81+
82+
We traverse the array $\textit{words}$$,$ find the words equal to $\textit{target}$, and compute their distance $t$ from $\textit{startIndex}$. The shortest distance in this case is $\min(t, n - t)$, so we only need to keep updating the minimum value.
83+
84+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
8185

8286
<!-- tabs:start -->
8387

8488
#### Python3
8589

8690
```python
8791
class Solution:
88-
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
92+
def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:
8993
n = len(words)
9094
ans = n
9195
for i, w in enumerate(words):
@@ -99,12 +103,11 @@ class Solution:
99103

100104
```java
101105
class Solution {
102-
public int closetTarget(String[] words, String target, int startIndex) {
106+
public int closestTarget(String[] words, String target, int startIndex) {
103107
int n = words.length;
104108
int ans = n;
105-
for (int i = 0; i < n; ++i) {
106-
String w = words[i];
107-
if (w.equals(target)) {
109+
for (int i = 0; i < n; i++) {
110+
if (words[i].equals(target)) {
108111
int t = Math.abs(i - startIndex);
109112
ans = Math.min(ans, Math.min(t, n - t));
110113
}
@@ -119,14 +122,13 @@ class Solution {
119122
```cpp
120123
class Solution {
121124
public:
122-
int closetTarget(vector<string>& words, string target, int startIndex) {
125+
int closestTarget(vector<string>& words, string target, int startIndex) {
123126
int n = words.size();
124127
int ans = n;
125-
for (int i = 0; i < n; ++i) {
126-
auto w = words[i];
127-
if (w == target) {
128+
for (int i = 0; i < n; i++) {
129+
if (words[i] == target) {
128130
int t = abs(i - startIndex);
129-
ans = min(ans, min(t, n - t));
131+
ans = min({ans, t, n - t});
130132
}
131133
}
132134
return ans == n ? -1 : ans;
@@ -137,106 +139,77 @@ public:
137139
#### Go
138140
139141
```go
140-
func closetTarget(words []string, target string, startIndex int) int {
142+
func closestTarget(words []string, target string, startIndex int) int {
141143
n := len(words)
142144
ans := n
143145
for i, w := range words {
144146
if w == target {
145-
t := abs(i - startIndex)
146-
ans = min(ans, min(t, n-t))
147+
t := i - startIndex
148+
if t < 0 {
149+
t = -t
150+
}
151+
ans = min(ans, t, n-t)
147152
}
148153
}
149154
if ans == n {
150155
return -1
151156
}
152157
return ans
153158
}
154-
155-
func abs(x int) int {
156-
if x < 0 {
157-
return -x
158-
}
159-
return x
160-
}
161159
```
162160

163161
#### TypeScript
164162

165163
```ts
166-
function closetTarget(words: string[], target: string, startIndex: number): number {
164+
function closestTarget(words: string[], target: string, startIndex: number): number {
167165
const n = words.length;
168-
for (let i = 0; i <= n >> 1; i++) {
169-
if (words[(startIndex - i + n) % n] === target || words[(startIndex + i) % n] === target) {
170-
return i;
166+
let ans = n;
167+
for (let i = 0; i < n; i++) {
168+
if (words[i] === target) {
169+
const t = Math.abs(i - startIndex);
170+
ans = Math.min(ans, t, n - t);
171171
}
172172
}
173-
return -1;
173+
return ans === n ? -1 : ans;
174174
}
175175
```
176176

177177
#### Rust
178178

179179
```rust
180180
impl Solution {
181-
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
182-
let start_index = start_index as usize;
183-
let n = words.len();
184-
for i in 0..=n >> 1 {
185-
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
186-
{
187-
return i as i32;
181+
pub fn closest_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
182+
let n = words.len() as i32;
183+
let mut ans = n;
184+
for (i, w) in words.iter().enumerate() {
185+
if w == &target {
186+
let t = (i as i32 - start_index).abs();
187+
ans = ans.min(t.min(n - t));
188188
}
189189
}
190-
-1
190+
if ans == n { -1 } else { ans }
191191
}
192192
}
193193
```
194194

195195
#### C
196196

197197
```c
198-
int closetTarget(char** words, int wordsSize, char* target, int startIndex) {
199-
for (int i = 0; i <= wordsSize >> 1; i++) {
200-
if (strcmp(words[(startIndex - i + wordsSize) % wordsSize], target) == 0 || strcmp(words[(startIndex + i) % wordsSize], target) == 0) {
201-
return i;
202-
}
203-
}
204-
return -1;
205-
}
206-
```
207-
208-
<!-- tabs:end -->
209-
210-
<!-- solution:end -->
211-
212-
<!-- solution:start -->
213-
214-
### Solution 2
215-
216-
<!-- tabs:start -->
198+
#define min(a, b) ((a) < (b) ? (a) : (b))
217199

218-
#### Rust
219-
220-
```rust
221-
use std::cmp::min;
200+
int closestTarget(char** words, int wordsSize, char* target, int startIndex) {
201+
int n = wordsSize;
202+
int ans = n;
222203

223-
impl Solution {
224-
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
225-
let mut ans = words.len();
226-
227-
for (i, w) in words.iter().enumerate() {
228-
if *w == target {
229-
let t = ((i as i32) - start_index).abs();
230-
ans = min(ans, min(t as usize, words.len() - (t as usize)));
231-
}
204+
for (int i = 0; i < n; i++) {
205+
if (strcmp(words[i], target) == 0) {
206+
int t = abs(i - startIndex);
207+
int dist = min(t, n - t);
208+
ans = min(ans, dist);
232209
}
233-
234-
if ans == words.len() {
235-
return -1;
236-
}
237-
238-
ans as i32
239210
}
211+
212+
return ans == n ? -1 : ans;
240213
}
241214
```
242215

0 commit comments

Comments
 (0)