mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
Merge branch 'master' into janetyu
This commit is contained in:
@ -5,20 +5,23 @@ func lengthOfLongestSubstring(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
|
||||
var bitSet [256]uint8
|
||||
var bitSet [256]bool
|
||||
result, left, right := 0, 0, 0
|
||||
for left < len(s) {
|
||||
if right < len(s) && bitSet[s[right]] == 0 {
|
||||
// 标记对应的 ASCII 码为 1
|
||||
bitSet[s[right]] = 1
|
||||
right++
|
||||
} else {
|
||||
// 标记对应的 ASCII 码为 0
|
||||
bitSet[s[left]] = 0
|
||||
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
|
||||
if bitSet[s[right]] {
|
||||
bitSet[s[left]] = false
|
||||
left++
|
||||
} else {
|
||||
bitSet[s[right]] = true
|
||||
right++
|
||||
}
|
||||
if result < right-left {
|
||||
result = right - left
|
||||
}
|
||||
if left+result >= len(s) || right >= len(s) {
|
||||
break
|
||||
}
|
||||
result = max(result, right-left)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
17
leetcode/0118.Pascals-Triangle/118. Pascal's Triangle.go
Normal file
17
leetcode/0118.Pascals-Triangle/118. Pascal's Triangle.go
Normal file
@ -0,0 +1,17 @@
|
||||
package leetcode
|
||||
|
||||
func generate(numRows int) [][]int {
|
||||
result := [][]int{}
|
||||
for i := 0; i < numRows; i++ {
|
||||
row := []int{}
|
||||
for j := 0; j < i+1; j++ {
|
||||
if j == 0 || j == i {
|
||||
row = append(row, 1)
|
||||
} else if i > 1 {
|
||||
row = append(row, result[i-1][j-1]+result[i-1][j])
|
||||
}
|
||||
}
|
||||
result = append(result, row)
|
||||
}
|
||||
return result
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question118 struct {
|
||||
para118
|
||||
ans118
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para118 struct {
|
||||
numRows int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans118 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem118(t *testing.T) {
|
||||
|
||||
qs := []question118{
|
||||
|
||||
question118{
|
||||
para118{2},
|
||||
ans118{[][]int{{1}, {1, 1}}},
|
||||
},
|
||||
|
||||
question118{
|
||||
para118{5},
|
||||
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}}},
|
||||
},
|
||||
|
||||
question118{
|
||||
para118{10},
|
||||
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}, {1, 9, 36, 84, 126, 126, 84, 36, 9, 1}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 118------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans118, q.para118
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, generate(p.numRows))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
60
leetcode/0118.Pascals-Triangle/README.md
Normal file
60
leetcode/0118.Pascals-Triangle/README.md
Normal file
@ -0,0 +1,60 @@
|
||||
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
|
||||
|
||||

|
||||
|
||||
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
|
||||
|
||||
**Example:**
|
||||
|
||||
```
|
||||
Input: 5
|
||||
Output:
|
||||
[
|
||||
[1],
|
||||
[1,1],
|
||||
[1,2,1],
|
||||
[1,3,3,1],
|
||||
[1,4,6,4,1]
|
||||
]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个 n,要求打印杨辉三角的前 n 行。
|
||||
- 简单题。按照杨辉三角的生成规则循环打印即可。
|
||||
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func generate(numRows int) [][]int {
|
||||
result := [][]int{}
|
||||
for i := 0; i < numRows; i++ {
|
||||
row := []int{}
|
||||
for j := 0; j < i+1; j++ {
|
||||
if j == 0 || j == i {
|
||||
row = append(row, 1)
|
||||
} else if i > 1 {
|
||||
row = append(row, result[i-1][j-1]+result[i-1][j])
|
||||
}
|
||||
}
|
||||
result = append(result, row)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user