mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 0821
This commit is contained in:
@ -1,10 +1,4 @@
|
||||
# 643. Maximum Average Subarray I
|
||||
|
||||
Added By: halfrost
|
||||
Created Time: Feb 7, 2021 5:07 PM
|
||||
Difficulty: Easy
|
||||
Link: https://leetcode.com/problems/maximum-average-subarray-i/
|
||||
Tags: Array
|
||||
# [643. Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/)
|
||||
|
||||
## 题目
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func shortestToChar(s string, c byte) []int {
|
||||
res := make([]int, len(s))
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == c {
|
||||
res[i] = 0
|
||||
} else {
|
||||
left, right := math.MaxInt32, math.MaxInt32
|
||||
for j := i + 1; j < len(s); j++ {
|
||||
if s[j] == c {
|
||||
right = j - i
|
||||
break
|
||||
}
|
||||
}
|
||||
for k := i - 1; k >= 0; k-- {
|
||||
if s[k] == c {
|
||||
left = i - k
|
||||
break
|
||||
}
|
||||
}
|
||||
res[i] = min(left, right)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question821 struct {
|
||||
para821
|
||||
ans821
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para821 struct {
|
||||
s string
|
||||
c byte
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans821 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem821(t *testing.T) {
|
||||
|
||||
qs := []question821{
|
||||
|
||||
{
|
||||
para821{"loveleetcode", 'e'},
|
||||
ans821{[]int{3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}},
|
||||
},
|
||||
|
||||
{
|
||||
para821{"baaa", 'b'},
|
||||
ans821{[]int{0, 1, 2, 3}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 821------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans821, q.para821
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, shortestToChar(p.s, p.c))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
76
leetcode/0821.Shortest-Distance-to-a-Character/README.md
Normal file
76
leetcode/0821.Shortest-Distance-to-a-Character/README.md
Normal file
@ -0,0 +1,76 @@
|
||||
# [821. Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a string `s` and a character `c` that occurs in `s`, return *an array of integers `answer` where* `answer.length == s.length` *and* `answer[i]` *is the shortest distance from* `s[i]` *to the character* `c` *in* `s`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "loveleetcode", c = "e"
|
||||
Output: [3,2,1,0,1,0,0,1,2,2,1,0]
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "aaab", c = "b"
|
||||
Output: [3,2,1,0]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= s.length <= 104`
|
||||
- `s[i]` and `c` are lowercase English letters.
|
||||
- `c` occurs at least once in `s`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个字符串 S 和一个字符 C。返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。依次扫描字符串 S,针对每一个非字符 C 的字符,分别往左扫一次,往右扫一次,计算出距离目标字符 C 的距离,然后取左右两个距离的最小值存入最终答案数组中。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func shortestToChar(s string, c byte) []int {
|
||||
res := make([]int, len(s))
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == c {
|
||||
res[i] = 0
|
||||
} else {
|
||||
left, right := math.MaxInt32, math.MaxInt32
|
||||
for j := i + 1; j < len(s); j++ {
|
||||
if s[j] == c {
|
||||
right = j - i
|
||||
break
|
||||
}
|
||||
}
|
||||
for k := i - 1; k >= 0; k-- {
|
||||
if s[k] == c {
|
||||
left = i - k
|
||||
break
|
||||
}
|
||||
}
|
||||
res[i] = min(left, right)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user