Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0821 (#1538)
Browse files Browse the repository at this point in the history
No.0821.Shortest Distance to a Character
  • Loading branch information
yanglbme authored Aug 29, 2023
1 parent 354f9d9 commit b1bfc3b
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 419 deletions.
271 changes: 78 additions & 193 deletions solution/0800-0899/0821.Shortest Distance to a Character/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@

**方法一:两次遍历**

两次遍历,找出每个字符左侧和右侧最近的 c,算出最短距离
我们先创建一个长度为 $n$ 的答案数组 $ans$

**方法二:BFS**
接下来,我们从左到右遍历字符串 $s$,记录最近出现的字符 $c$ 的位置 $pre$,那么对于位置 $i$,答案就是 $i - pre$,即 $ans[i] = i - pre$。

在字符串 s 中找出所有字符 c 对应的下标,并放入队列 q
然后,我们从右到左遍历字符串 $s$,记录最近出现的字符 $c$ 的位置 $suf$,那么对于位置 $i$,答案就是 $suf - i$,即 $ans[i] = \min(ans[i], suf - i)$

BFS 向左右两边扩散,找出最短距离。
最后返回答案数组 $ans$ 即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -66,34 +68,17 @@ BFS 向左右两边扩散,找出最短距离。
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
n = len(s)
ans = [0] * n
j = inf
ans = [n] * n
pre = -inf
for i, ch in enumerate(s):
if ch == c:
j = i
ans[i] = abs(i - j)
j = inf
pre = i
ans[i] = min(ans[i], i - pre)
suf = inf
for i in range(n - 1, -1, -1):
if s[i] == c:
j = i
ans[i] = min(ans[i], abs(i - j))
return ans
```

```python
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
q = deque([i for i, ch in enumerate(s) if ch == c])
ans = [0 if ch == c else -1 for ch in s]
d = 0
while q:
d += 1
for _ in range(len(q)):
i = q.popleft()
for j in (i - 1, i + 1):
if 0 <= j < len(s) and ans[j] == -1:
ans[j] = d
q.append(j)
suf = i
ans[i] = min(ans[i], suf - i)
return ans
```

Expand All @@ -106,94 +91,103 @@ class Solution {
public int[] shortestToChar(String s, char c) {
int n = s.length();
int[] ans = new int[n];
for (int i = 0, j = Integer.MAX_VALUE; i < n; ++i) {
final int inf = 1 << 30;
Arrays.fill(ans, inf);
for (int i = 0, pre = -inf; i < n; ++i) {
if (s.charAt(i) == c) {
j = i;
pre = i;
}
ans[i] = Math.abs(i - j);
ans[i] = Math.min(ans[i], i - pre);
}
for (int i = n - 1, j = Integer.MAX_VALUE; i >= 0; --i) {
for (int i = n - 1, suf = inf; i >= 0; --i) {
if (s.charAt(i) == c) {
j = i;
suf = i;
}
ans[i] = Math.min(ans[i], Math.abs(i - j));
ans[i] = Math.min(ans[i], suf - i);
}
return ans;
}
}
```

```java
### **C++**

```cpp
class Solution {
public int[] shortestToChar(String s, char c) {
Deque<Integer> q = new ArrayDeque<>();
int n = s.length();
int[] ans = new int[n];
Arrays.fill(ans, -1);
for (int i = 0; i < n; ++i) {
if (s.charAt(i) == c) {
q.offer(i);
ans[i] = 0;
public:
vector<int> shortestToChar(string s, char c) {
int n = s.size();
const int inf = 1 << 30;
vector<int> ans(n, inf);
for (int i = 0, pre = -inf; i < n; ++i) {
if (s[i] == c) {
pre = i;
}
ans[i] = min(ans[i], i - pre);
}
int d = 0;
while (!q.isEmpty()) {
++d;
for (int t = q.size(); t > 0; --t) {
int i = q.poll();
for (int j : Arrays.asList(i - 1, i + 1)) {
if (j >= 0 && j < n && ans[j] == -1) {
ans[j] = d;
q.offer(j);
}
}
for (int i = n - 1, suf = inf; ~i; --i) {
if (s[i] == c) {
suf = i;
}
ans[i] = min(ans[i], suf - i);
}
return ans;
}
}
};
```
### **TypeScript**
### **Go**
```ts
function shortestToChar(s: string, c: string): number[] {
const n = s.length;
let ans = [];
let pre = Infinity;
for (let i = 0; i < n; i++) {
if (s.charAt(i) == c) pre = i;
ans[i] = Math.abs(pre - i);
}
pre = Infinity;
for (let i = n - 1; i > -1; i--) {
if (s.charAt(i) == c) pre = i;
ans[i] = Math.min(Math.abs(pre - i), ans[i]);
}
return ans;
```go
func shortestToChar(s string, c byte) []int {
n := len(s)
ans := make([]int, n)
const inf int = 1 << 30
pre := -inf
for i := range s {
if s[i] == c {
pre = i
}
ans[i] = i - pre
}
suf := inf
for i := n - 1; i >= 0; i-- {
if s[i] == c {
suf = i
}
ans[i] = min(ans[i], suf-i)
}
return ans
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function shortestToChar(s: string, c: string): number[] {
const n = s.length;
const idxs = [];
for (let i = 0; i < n; i++) {
const inf = 1 << 30;
const ans: number[] = new Array(n).fill(inf);
for (let i = 0, pre = -inf; i < n; ++i) {
if (s[i] === c) {
idxs.push(i);
pre = i;
}
ans[i] = i - pre;
}
idxs.push(Infinity);

const res = new Array(n);
let i = 0;
for (let j = 0; j < n; j++) {
if (Math.abs(idxs[i] - j) > Math.abs(idxs[i + 1] - j)) {
i++;
for (let i = n - 1, suf = inf; i >= 0; --i) {
if (s[i] === c) {
suf = i;
}
res[j] = Math.abs(idxs[i] - j);
ans[i] = Math.min(ans[i], suf - i);
}
return res;
return ans;
}
```

Expand Down Expand Up @@ -225,115 +219,6 @@ impl Solution {
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> shortestToChar(string s, char c) {
int n = s.size();
vector<int> ans(n);
for (int i = 0, j = INT_MAX; i < n; ++i) {
if (s[i] == c) j = i;
ans[i] = abs(i - j);
}
for (int i = n - 1, j = INT_MAX; i >= 0; --i) {
if (s[i] == c) j = i;
ans[i] = min(ans[i], abs(i - j));
}
return ans;
}
};
```
```cpp
class Solution {
public:
vector<int> shortestToChar(string s, char c) {
int n = s.size();
vector<int> ans(n, -1);
queue<int> q;
for (int i = 0; i < n; ++i) {
if (s[i] == c) {
q.push(i);
ans[i] = 0;
}
}
int d = 0;
while (!q.empty()) {
++d;
for (int t = q.size(); t > 0; --t) {
int i = q.front();
q.pop();
vector<int> dirs{i - 1, i + 1};
for (int& j : dirs) {
if (j >= 0 && j < n && ans[j] == -1) {
ans[j] = d;
q.push(j);
}
}
}
}
return ans;
}
};
```

### **Go**

```go
func shortestToChar(s string, c byte) []int {
n := len(s)
ans := make([]int, n)
for i, j := 0, -10000; i < n; i++ {
if s[i] == c {
j = i
}
ans[i] = i - j
}
for i, j := n-1, 10000; i >= 0; i-- {
if s[i] == c {
j = i
}
if j-i < ans[i] {
ans[i] = j - i
}
}
return ans
}
```

```go
func shortestToChar(s string, c byte) []int {
n := len(s)
var q []int
ans := make([]int, n)
for i := range s {
ans[i] = -1
if s[i] == c {
q = append(q, i)
ans[i] = 0
}
}

d := 0
for len(q) > 0 {
d++
for t := len(q); t > 0; t-- {
i := q[0]
q = q[1:]
for _, j := range []int{i - 1, i + 1} {
if j >= 0 && j < n && ans[j] == -1 {
ans[j] = d
q = append(q, j)
}
}
}
}
return ans
}
```

### **...**

```
Expand Down
Loading

0 comments on commit b1bfc3b

Please sign in to comment.