FIX GO_LINT

This commit is contained in:
YDZ
2019-07-08 20:22:09 +08:00
parent 777215785f
commit 55fd5c091c
162 changed files with 435 additions and 447 deletions

View File

@ -1,6 +1,6 @@
package leetcode
func reverse_7(x int) int {
func reverse7(x int) int {
tmp := 0
for x != 0 {
tmp = tmp*10 + x%10

View File

@ -51,7 +51,7 @@ func Test_Problem7(t *testing.T) {
for _, q := range qs {
_, p := q.ans7, q.para7
fmt.Printf("【input】:%v 【output】:%v\n", p.one, reverse_7(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p.one, reverse7(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -12,7 +12,7 @@ func threeSum(nums []int) [][]int {
}
uniqNums := []int{}
for key, _ := range counter {
for key := range counter {
uniqNums = append(uniqNums, key)
}
sort.Ints(uniqNums)

View File

@ -30,7 +30,7 @@ func threeSumClosest(nums []int, target int) int {
}
// 解法二 暴力解法 O(n^3)
func threeSumClosest_(nums []int, target int) int {
func threeSumClosest1(nums []int, target int) int {
res, difference := 0, math.MaxInt16
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {

View File

@ -10,7 +10,7 @@ func fourSum(nums []int, target int) [][]int {
}
uniqNums := []int{}
for key, _ := range counter {
for key := range counter {
uniqNums = append(uniqNums, key)
}
sort.Ints(uniqNums)

View File

@ -29,7 +29,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
}
// 解法二
func removeNthFromEnd_(head *ListNode, n int) *ListNode {
func removeNthFromEnd1(head *ListNode, n int) *ListNode {
if head == nil {
return nil
}

View File

@ -17,56 +17,7 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1.Val < l2.Val {
l1.Next = mergeTwoLists(l1.Next, l2)
return l1
} else {
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}
// if l1 == nil && l2 == nil {
// return nil
// }
// head := &ListNode{Val: 0, Next: nil}
// current := head
// var currentl1, currentl2 *ListNode
// for l1 != nil || l2 != nil {
// if l1 != nil {
// currentl1 = &ListNode{Val: l1.Val, Next: nil}
// }
// if l2 != nil {
// currentl2 = &ListNode{Val: l2.Val, Next: nil}
// }
// if l1 == nil && l2 != nil {
// current.Next = currentl2
// current = currentl2
// l2 = l2.Next
// continue
// } else if l2 == nil && l1 != nil {
// current.Next = currentl1
// current = currentl1
// l1 = l1.Next
// continue
// } else if l2 == nil && l1 == nil {
// return head.Next
// } else {
// if currentl1.Val < currentl2.Val {
// current.Next = currentl1
// current = currentl1
// l1 = l1.Next
// continue
// } else if currentl1.Val == currentl2.Val {
// current.Next = currentl2
// currentl2.Next = currentl1
// current = currentl1
// l1 = l1.Next
// l2 = l2.Next
// continue
// } else {
// current.Next = currentl2
// current = currentl2
// l2 = l2.Next
// continue
// }
// }
// }
// return head.Next
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}

View File

@ -31,8 +31,7 @@ func mergeTwoLists1(l1 *ListNode, l2 *ListNode) *ListNode {
if l1.Val < l2.Val {
l1.Next = mergeTwoLists1(l1.Next, l2)
return l1
} else {
l2.Next = mergeTwoLists1(l1, l2.Next)
return l2
}
l2.Next = mergeTwoLists1(l1, l2.Next)
return l2
}

View File

@ -14,13 +14,13 @@ func removeDuplicates(nums []int) int {
}
}
nums[last+1] = nums[finder]
last += 1
last++
}
return last + 1
}
// 解法二
func removeDuplicates_(nums []int) int {
func removeDuplicates1(nums []int) int {
if len(nums) == 0 {
return 0
}
@ -32,14 +32,14 @@ func removeDuplicates_(nums []int) int {
break
}
if nums[i+1] == nums[i] {
removeElement_(nums, i+1, nums[i])
removeElement1(nums, i+1, nums[i])
// fmt.Printf("此时 num = %v length = %v\n", nums, length)
}
}
return i + 1
}
func removeElement_(nums []int, start, val int) int {
func removeElement1(nums []int, start, val int) int {
if len(nums) == 0 {
return 0
}

View File

@ -20,6 +20,6 @@ func strStr(haystack string, needle string) int {
}
// 解法二
func strStr_(haystack string, needle string) int {
func strStr1(haystack string, needle string) int {
return strings.Index(haystack, needle)
}

View File

@ -60,7 +60,7 @@ func isValidSudoku(board [][]byte) bool {
}
// 解法二 添加缓存,时间复杂度 O(n^2)
func isValidSudoku_(board [][]byte) bool {
func isValidSudoku1(board [][]byte) bool {
rowbuf, colbuf, boxbuf := make([][]bool, 9), make([][]bool, 9), make([][]bool, 9)
for i := 0; i < 9; i++ {
rowbuf[i] = make([]bool, 9)

View File

@ -73,7 +73,7 @@ func Test_Problem36(t *testing.T) {
for _, q := range qs {
_, p := q.ans36, q.para36
fmt.Printf("【input】:%v 【output】:%v\n", p, isValidSudoku_(p.s))
fmt.Printf("【input】:%v 【output】:%v\n", p, isValidSudoku1(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -8,11 +8,11 @@ func permuteUnique(nums []int) [][]int {
}
used, p, res := make([]bool, len(nums)), []int{}, [][]int{}
sort.Ints(nums) // 这里是去重的关键逻辑
generatePermutation_47(nums, 0, p, &res, &used)
generatePermutation47(nums, 0, p, &res, &used)
return res
}
func generatePermutation_47(nums []int, index int, p []int, res *[][]int, used *[]bool) {
func generatePermutation47(nums []int, index int, p []int, res *[][]int, used *[]bool) {
if index == len(nums) {
temp := make([]int, len(p))
copy(temp, p)
@ -26,7 +26,7 @@ func generatePermutation_47(nums []int, index int, p []int, res *[][]int, used *
}
(*used)[i] = true
p = append(p, nums[i])
generatePermutation_47(nums, index+1, p, res, used)
generatePermutation47(nums, index+1, p, res, used)
p = p[:len(p)-1]
(*used)[i] = false
}

View File

@ -7,14 +7,14 @@ func totalNQueens(n int) int {
}
// 解法二DFS 回溯法
func totalNQueens_(n int) int {
func totalNQueens1(n int) int {
col, dia1, dia2, row, res := make([]bool, n), make([]bool, 2*n-1), make([]bool, 2*n-1), []int{}, 0
putQueen_(n, 0, &col, &dia1, &dia2, &row, &res)
putQueen52(n, 0, &col, &dia1, &dia2, &row, &res)
return res
}
// 尝试在一个n皇后问题中, 摆放第index行的皇后位置
func putQueen_(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *int) {
func putQueen52(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *int) {
if index == n {
*res++
return
@ -26,7 +26,7 @@ func putQueen_(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *int) {
(*col)[i] = true
(*dia1)[index+i] = true
(*dia2)[index-i+n-1] = true
putQueen_(n, index+1, col, dia1, dia2, row, res)
putQueen52(n, index+1, col, dia1, dia2, row, res)
(*col)[i] = false
(*dia1)[index+i] = false
(*dia2)[index-i+n-1] = false

View File

@ -22,7 +22,7 @@ func maxSubArray(nums []int) int {
}
// 解法二 模拟
func maxSubArray_(nums []int) int {
func maxSubArray1(nums []int) int {
if len(nums) == 1 {
return nums[0]
}

View File

@ -8,12 +8,13 @@ package leetcode
* }
*/
// Interval define
type Interval struct {
Start int
End int
}
func merge_(intervals []Interval) []Interval {
func merge56(intervals []Interval) []Interval {
if len(intervals) == 0 {
return intervals
}
@ -35,17 +36,15 @@ func merge_(intervals []Interval) []Interval {
func max(a int, b int) int {
if a > b {
return a
} else {
return b
}
return b
}
func min(a int, b int) int {
if a > b {
return b
} else {
return a
}
return a
}
func partitionSort(a []Interval, lo, hi int) int {

View File

@ -66,7 +66,7 @@ func Test_Problem56(t *testing.T) {
for _, q := range qs {
_, p := q.ans56, q.para56
fmt.Printf("【input】:%v 【output】:%v\n", p, merge_(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, merge56(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -19,7 +19,7 @@ func minPathSum(grid [][]int) int {
}
// 解法二 最原始的方法,辅助空间 O(n^2)
func minPathSum_(grid [][]int) int {
func minPathSum1(grid [][]int) int {
if len(grid) == 0 {
return 0
}

View File

@ -21,7 +21,7 @@ func mySqrt(x int) int {
}
// 解法二 牛顿迭代法 https://en.wikipedia.org/wiki/Integer_square_root
func mySqrt_(x int) int {
func mySqrt1(x int) int {
r := x
for r*r > x {
r = (r + x/r) / 2

View File

@ -41,7 +41,7 @@ func Test_Problem69(t *testing.T) {
for _, q := range qs {
_, p := q.ans69, q.para69
fmt.Printf("【input】:%v 【output】:%v\n", p, mySqrt_(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, mySqrt1(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -23,13 +23,12 @@ func simplifyPath(path string) string {
}
if len(stack) == 0 {
return "/"
} else {
res = strings.Join(stack, "/")
}
res = strings.Join(stack, "/")
return "/" + res
}
// 解法二 golang 的官方库 API
func simplifyPath_(path string) string {
func simplifyPath1(path string) string {
return filepath.Clean(path)
}

View File

@ -28,7 +28,7 @@ func generateSubsets(nums []int, k, start int, c []int, res *[][]int) {
}
// 解法二
func subsets_(nums []int) [][]int {
func subsets1(nums []int) [][]int {
res := make([][]int, 1)
sort.Ints(nums)
for i := range nums {

View File

@ -13,7 +13,7 @@ func exist(board [][]byte, word string) bool {
visited[i] = make([]bool, len(board[0]))
}
for i, v := range board {
for j, _ := range v {
for j := range v {
if searchWord(board, visited, word, 0, i, j) {
return true
}

View File

@ -1,6 +1,6 @@
package leetcode
func removeDuplicates_80(nums []int) int {
func removeDuplicates80(nums []int) int {
if len(nums) == 0 {
return 0
}
@ -22,7 +22,7 @@ func removeDuplicates_80(nums []int) int {
last += 2
} else {
nums[last+1] = nums[finder]
last += 1
last++
}
if finder == len(nums)-1 {
if nums[finder] != nums[last-1] {

View File

@ -61,7 +61,7 @@ func Test_Problem80(t *testing.T) {
for _, q := range qs {
_, p := q.ans80, q.para80
fmt.Printf("【input】:%v 【output】:%v\n", p.one, removeDuplicates_80(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p.one, removeDuplicates80(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -62,8 +62,7 @@ func deleteDuplicates2(head *ListNode) *ListNode {
head = head.Next
}
return deleteDuplicates(head.Next)
} else {
head.Next = deleteDuplicates(head.Next)
}
head.Next = deleteDuplicates(head.Next)
return head
}

View File

@ -30,6 +30,7 @@ func partition(head *ListNode, x int) *ListNode {
return beforeHead.Next
}
// DoublyListNode define
type DoublyListNode struct {
Val int
Prev *DoublyListNode
@ -37,7 +38,7 @@ type DoublyListNode struct {
}
// 解法二 双链表
func partition_(head *ListNode, x int) *ListNode {
func partition1(head *ListNode, x int) *ListNode {
if head == nil || head.Next == nil {
return head
}

View File

@ -53,7 +53,7 @@ func flipGrayCode(num int) int {
}
// 解法二 直译
func grayCode_(n int) []int {
func grayCode1(n int) []int {
var l uint = 1 << uint(n)
out := make([]int, l)
for i := uint(0); i < l; i++ {

View File

@ -4,7 +4,7 @@ import (
"strconv"
)
func restoreIpAddresses(s string) []string {
func restoreIPAddresses(s string) []string {
if s == "" {
return []string{}
}

View File

@ -46,7 +46,7 @@ func Test_Problem93(t *testing.T) {
for _, q := range qs {
_, p := q.ans93, q.para93
fmt.Printf("【input】:%v 【output】:%v\n", p, restoreIpAddresses(p.s))
fmt.Printf("【input】:%v 【output】:%v\n", p, restoreIPAddresses(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -24,7 +24,7 @@ func isValidbst(root *TreeNode, min, max float64) bool {
}
// 解法二,把 BST 按照左中右的顺序输出到数组中,如果是 BST则数组中的数字是从小到大有序的如果出现逆序就不是 BST
func isValidBST_(root *TreeNode) bool {
func isValidBST1(root *TreeNode) bool {
arr := []int{}
inOrder(root, &arr)
for i := 1; i < len(arr); i++ {

View File

@ -43,7 +43,7 @@ func levelOrder(root *TreeNode) [][]int {
}
// 解法二 DFS
func levelOrder_(root *TreeNode) [][]int {
func levelOrder1(root *TreeNode) [][]int {
levels := [][]int{}
dfsLevel(root, -1, &levels)
return levels

View File

@ -16,6 +16,7 @@ package leetcode
* }
*/
// TreeNode define
type TreeNode struct {
Val int
Left *TreeNode

View File

@ -32,7 +32,7 @@ func findPath(n *TreeNode, sum int, slice [][]int, stack []int) [][]int {
}
// 解法二
func pathSum_(root *TreeNode, sum int) [][]int {
func pathSum1(root *TreeNode, sum int) [][]int {
if root == nil {
return [][]int{}
}

View File

@ -23,7 +23,7 @@ func flatten(root *TreeNode) {
}
// 解法二 递归
func flatten_1(root *TreeNode) {
func flatten1(root *TreeNode) {
if root == nil || (root.Left == nil && root.Right == nil) {
return
}
@ -37,7 +37,9 @@ func flatten_1(root *TreeNode) {
}
root.Right = currRight
}
func flatten_2(root *TreeNode) {
// 解法三 递归
func flatten2(root *TreeNode) {
if root == nil {
return
}

View File

@ -18,7 +18,7 @@ func minimumTotal(triangle [][]int) int {
}
// 解法二 正常 DP空间复杂度 O(n)
func minimumTotal_(triangle [][]int) int {
func minimumTotal1(triangle [][]int) int {
if len(triangle) == 0 {
return 0
}

View File

@ -18,7 +18,7 @@ func maxProfit(prices []int) int {
}
// 解法二 单调栈
func maxProfit_(prices []int) int {
func maxProfit1(prices []int) int {
if len(prices) == 0 {
return 0
}

View File

@ -1,6 +1,6 @@
package leetcode
func maxProfit_122(prices []int) int {
func maxProfit122(prices []int) int {
profit := 0
for i := 0; i < len(prices)-1; i++ {
if prices[i+1] > prices[i] {

View File

@ -56,7 +56,7 @@ func Test_Problem122(t *testing.T) {
for _, q := range qs {
_, p := q.ans122, q.para122
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit_122(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit122(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -47,7 +47,7 @@ func findLadders(beginWord string, endWord string, wordList []string) [][]string
if len(result) > 0 {
return result
}
for k, _ := range levelMap {
for k := range levelMap {
delete(wordMap, k)
}
// clear levelMap

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一
func partition_131(s string) [][]string {
func partition131(s string) [][]string {
if s == "" {
return [][]string{}
}
@ -22,16 +22,16 @@ func findPalindrome(str string, index int, s string, isPal bool, pal []string, r
if index == 0 {
s = string(str[index])
pal = append(pal, s)
findPalindrome(str, index+1, s, isPal && isPalindrome_131(s), pal, res)
findPalindrome(str, index+1, s, isPal && isPalindrome131(s), pal, res)
} else {
temp := pal[len(pal)-1]
s = pal[len(pal)-1] + string(str[index])
pal[len(pal)-1] = s
findPalindrome(str, index+1, s, isPalindrome_131(s), pal, res)
findPalindrome(str, index+1, s, isPalindrome131(s), pal, res)
pal[len(pal)-1] = temp
if isPalindrome_131(temp) {
if isPalindrome131(temp) {
pal = append(pal, string(str[index]))
findPalindrome(str, index+1, temp, isPal && isPalindrome_131(temp), pal, res)
findPalindrome(str, index+1, temp, isPal && isPalindrome131(temp), pal, res)
pal = pal[:len(pal)-1]
}
@ -39,7 +39,7 @@ func findPalindrome(str string, index int, s string, isPal bool, pal []string, r
return
}
func isPalindrome_131(s string) bool {
func isPalindrome131(s string) bool {
slen := len(s)
for i, j := 0, slen-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
@ -50,18 +50,18 @@ func isPalindrome_131(s string) bool {
}
// 解法二
func partition_131_2(s string) [][]string {
func partition131_1(s string) [][]string {
result := [][]string{}
size := len(s)
if size == 0 {
return result
}
current := make([]string, 0, size)
dfs_131(s, 0, current, &result)
dfs131(s, 0, current, &result)
return result
}
func dfs_131(s string, idx int, cur []string, result *[][]string) {
func dfs131(s string, idx int, cur []string, result *[][]string) {
start, end := idx, len(s)
if start == end {
temp := make([]string, len(cur))
@ -71,7 +71,7 @@ func dfs_131(s string, idx int, cur []string, result *[][]string) {
}
for i := start; i < end; i++ {
if isPal(s, start, i) {
dfs_131(s, i+1, append(cur, s[start:i+1]), result)
dfs131(s, i+1, append(cur, s[start:i+1]), result)
}
}
}

View File

@ -51,7 +51,7 @@ func Test_Problem131(t *testing.T) {
for _, q := range qs {
_, p := q.ans131, q.para131
fmt.Printf("【input】:%v 【output】:%v\n", p, partition_131(p.s))
fmt.Printf("【input】:%v 【output】:%v\n", p, partition131(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -24,7 +24,7 @@ func singleNumberIIIII(nums []int) int {
}
// 解法二
func singleNumberIIIII_(nums []int) int {
func singleNumberIIIII1(nums []int) int {
twos, threes, ones := 0xffffffff, 0xffffffff, 0
for i := 0; i < len(nums); i++ {
threes = threes ^ (nums[i] & twos)

View File

@ -11,7 +11,7 @@ func detectCycle(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return nil
}
isCycle, slow := hasCycle_(head)
isCycle, slow := hasCycle142(head)
if !isCycle {
return nil
}
@ -23,7 +23,7 @@ func detectCycle(head *ListNode) *ListNode {
return fast
}
func hasCycle_(head *ListNode) (bool, *ListNode) {
func hasCycle142(head *ListNode) (bool, *ListNode) {
fast := head
slow := head
for slow != nil && fast != nil && fast.Next != nil {

View File

@ -46,7 +46,7 @@ func reorderList(head *ListNode) *ListNode {
}
// 解法二 数组
func reorderList_1(head *ListNode) *ListNode {
func reorderList1(head *ListNode) *ListNode {
array := listToArray(head)
length := len(array)
if length == 0 {

View File

@ -27,7 +27,7 @@ func preorderTraversal(root *TreeNode) []int {
}
// 解法二
func preorderTraversal_(root *TreeNode) []int {
func preorderTraversal1(root *TreeNode) []int {
var result []int
preorder(root, &result)
return result

View File

@ -18,17 +18,17 @@ func sortList(head *ListNode) *ListNode {
return head
}
middleNode := middleNode_(head)
middleNode := middleNode1(head)
cur = middleNode.Next
middleNode.Next = nil
middleNode = cur
left := sortList(head)
right := sortList(middleNode)
return mergeTwoLists_(left, right)
return mergeTwoLists148(left, right)
}
func middleNode_(head *ListNode) *ListNode {
func middleNode1(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
@ -41,7 +41,7 @@ func middleNode_(head *ListNode) *ListNode {
return p1
}
func mergeTwoLists_(l1 *ListNode, l2 *ListNode) *ListNode {
func mergeTwoLists148(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
@ -51,8 +51,7 @@ func mergeTwoLists_(l1 *ListNode, l2 *ListNode) *ListNode {
if l1.Val < l2.Val {
l1.Next = mergeTwoLists(l1.Next, l2)
return l1
} else {
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}

View File

@ -17,28 +17,28 @@ func evalRPN(tokens []string) int {
sum := stack[top-2] + stack[top-1]
stack = stack[:top-2]
stack = append(stack, sum)
top -= 1
top--
}
case "-":
{
sub := stack[top-2] - stack[top-1]
stack = stack[:top-2]
stack = append(stack, sub)
top -= 1
top--
}
case "*":
{
mul := stack[top-2] * stack[top-1]
stack = stack[:top-2]
stack = append(stack, mul)
top -= 1
top--
}
case "/":
{
div := stack[top-2] / stack[top-1]
stack = stack[:top-2]
stack = append(stack, div)
top -= 1
top--
}
default:
{

View File

@ -1,15 +1,19 @@
package leetcode
// MinStack define
type MinStack struct {
elements, min []int
l int
}
/** initialize your data structure here. */
func Constructor_155() MinStack {
// Constructor155 define
func Constructor155() MinStack {
return MinStack{make([]int, 0), make([]int, 0), 0}
}
// Push define
func (this *MinStack) Push(x int) {
this.elements = append(this.elements, x)
if this.l == 0 {

View File

@ -6,7 +6,7 @@ import (
)
func Test_Problem155(t *testing.T) {
obj1 := Constructor_155()
obj1 := Constructor155()
obj1.Push(1)
fmt.Printf("obj1 = %v\n", obj1)
obj1.Push(0)
@ -15,8 +15,8 @@ func Test_Problem155(t *testing.T) {
fmt.Printf("obj1 = %v\n", obj1)
obj1.Pop()
fmt.Printf("obj1 = %v\n", obj1)
param_3 := obj1.Top()
fmt.Printf("param_3 = %v\n", param_3)
param_4 := obj1.GetMin()
fmt.Printf("param_4 = %v\n", param_4)
param3 := obj1.Top()
fmt.Printf("param_3 = %v\n", param3)
param4 := obj1.GetMin()
fmt.Printf("param_4 = %v\n", param4)
}

View File

@ -5,7 +5,7 @@ func maximumGap(nums []int) int {
if len(nums) < 2 {
return 0
}
quickSort__(nums, 0, len(nums)-1)
quickSort164(nums, 0, len(nums)-1)
res := 0
for i := 0; i < len(nums)-1; i++ {
@ -16,7 +16,7 @@ func maximumGap(nums []int) int {
return res
}
func partition__(a []int, lo, hi int) int {
func partition164(a []int, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
@ -28,17 +28,17 @@ func partition__(a []int, lo, hi int) int {
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}
func quickSort__(a []int, lo, hi int) {
func quickSort164(a []int, lo, hi int) {
if lo >= hi {
return
}
p := partition__(a, lo, hi)
quickSort__(a, lo, p-1)
quickSort__(a, p+1, hi)
p := partition164(a, lo, hi)
quickSort164(a, lo, p-1)
quickSort164(a, p+1, hi)
}
// 解法二
func maximumGap_(nums []int) int {
func maximumGap1(nums []int) int {
if nums == nil || len(nums) < 2 {
return 0

View File

@ -55,7 +55,7 @@ func Test_Problem164(t *testing.T) {
for _, q := range qs {
_, p := q.ans164, q.para164
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumGap_(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumGap1(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一 这一题可以利用数组有序的特性
func twoSum_(numbers []int, target int) []int {
func twoSum167(numbers []int, target int) []int {
i, j := 0, len(numbers)-1
for i < j {
if numbers[i]+numbers[j] == target {
@ -16,7 +16,7 @@ func twoSum_(numbers []int, target int) []int {
}
// 解法二 不管数组是否有序,空间复杂度比上一种解法要多 O(n)
func twoSum__(numbers []int, target int) []int {
func twoSum167_1(numbers []int, target int) []int {
m := make(map[int]int)
for i := 0; i < len(numbers); i++ {
another := target - numbers[i]

View File

@ -37,7 +37,7 @@ func Test_Problem167(t *testing.T) {
for _, q := range qs {
_, p := q.ans167, q.para167
fmt.Printf("【input】:%v 【output】:%v\n", p.one, twoSum_(p.one, p.two))
fmt.Printf("【input】:%v 【output】:%v\n", p.one, twoSum167(p.one, p.two))
}
fmt.Printf("\n\n\n")
}

View File

@ -18,7 +18,7 @@ func majorityElement(nums []int) int {
}
// 解法二 时间复杂度 O(n) 空间复杂度 O(n)
func majorityElement_(nums []int) int {
func majorityElement1(nums []int) int {
m := make(map[int]int)
for _, v := range nums {
m[v]++

View File

@ -10,12 +10,15 @@ import "container/heap"
* Right *TreeNode
* }
*/
// BSTIterator define
type BSTIterator struct {
pq PriorityQueueOfInt
count int
}
func Constructor_173(root *TreeNode) BSTIterator {
// Constructor173 define
func Constructor173(root *TreeNode) BSTIterator {
result, pq := []int{}, PriorityQueueOfInt{}
postorder(root, &result)
for _, v := range result {

View File

@ -8,20 +8,20 @@ import (
func Test_Problem173(t *testing.T) {
root := Ints2TreeNode([]int{9, 7, 15, 3, NULL, NULL, 20})
obj := Constructor_173(root)
obj := Constructor173(root)
fmt.Printf("obj = %v\n", obj)
param_1 := obj.Next()
fmt.Printf("param_1 = %v\n", param_1)
param_2 := obj.HasNext()
fmt.Printf("param_2 = %v\n", param_2)
param_1 = obj.Next()
fmt.Printf("param_1 = %v\n", param_1)
param_1 = obj.Next()
fmt.Printf("param_1 = %v\n", param_1)
param_1 = obj.Next()
fmt.Printf("param_1 = %v\n", param_1)
param_1 = obj.Next()
fmt.Printf("param_1 = %v\n", param_1)
param_2 = obj.HasNext()
fmt.Printf("param_2 = %v\n", param_2)
param1 := obj.Next()
fmt.Printf("param_1 = %v\n", param1)
param2 := obj.HasNext()
fmt.Printf("param_2 = %v\n", param2)
param1 = obj.Next()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Next()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Next()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Next()
fmt.Printf("param_1 = %v\n", param1)
param2 = obj.HasNext()
fmt.Printf("param_2 = %v\n", param2)
}

View File

@ -23,7 +23,7 @@ func findRepeatedDnaSequences(s string) []string {
}
// 解法二
func findRepeatedDnaSequences_(s string) []string {
func findRepeatedDnaSequences1(s string) []string {
if len(s) < 10 {
return []string{}
}
@ -33,7 +33,7 @@ func findRepeatedDnaSequences_(s string) []string {
if cache[curr] == 1 {
ans = append(ans, curr)
}
cache[curr] += 1
cache[curr]++
}
return ans
}

View File

@ -1,7 +1,7 @@
package leetcode
func reverseBits(num uint32) uint32 {
var res uint32 = 0
var res uint32
for i := 0; i < 32; i++ {
res = res<<1 | num&1
num >>= 1

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一 DP
func rob_(nums []int) int {
func rob198(nums []int) int {
n := len(nums)
if n == 0 {
return 0
@ -19,7 +19,7 @@ func rob_(nums []int) int {
}
// 解法二 DP 优化辅助空间,把迭代的值保存在 2 个变量中
func rob__(nums []int) int {
func rob198_1(nums []int) int {
n := len(nums)
if n == 0 {
return 0

View File

@ -45,7 +45,7 @@ func Test_Problem198(t *testing.T) {
for _, q := range qs {
_, p := q.ans198, q.para198
fmt.Printf("【input】:%v 【output】:%v\n", p, rob_(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, rob198(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一
func rangeBitwiseAnd_(m int, n int) int {
func rangeBitwiseAnd1(m int, n int) int {
if m == 0 {
return 0
}

View File

@ -15,12 +15,11 @@ func isHappy(n int) bool {
if _, ok := record[res]; !ok {
if res == 1 {
return true
} else {
record[res] = res
num = res
res = 0
continue
}
record[res] = res
num = res
res = 0
continue
} else {
return false
}

View File

@ -6,7 +6,7 @@ type Trie struct {
}
/** Initialize your data structure here. */
func Constructor_208() Trie {
func Constructor208() Trie {
return Trie{isWord: false, children: make(map[rune]*Trie)}
}

View File

@ -6,18 +6,18 @@ import (
)
func Test_Problem208(t *testing.T) {
obj := Constructor_208()
obj := Constructor208()
fmt.Printf("obj = %v\n", obj)
obj.Insert("apple")
fmt.Printf("obj = %v\n", obj)
param_1 := obj.Search("apple")
fmt.Printf("param_1 = %v obj = %v\n", param_1, obj)
param_2 := obj.Search("app")
fmt.Printf("param_2 = %v obj = %v\n", param_2, obj)
param_3 := obj.StartsWith("app")
fmt.Printf("param_3 = %v obj = %v\n", param_3, obj)
param1 := obj.Search("apple")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param2 := obj.Search("app")
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
param3 := obj.StartsWith("app")
fmt.Printf("param_3 = %v obj = %v\n", param3, obj)
obj.Insert("app")
fmt.Printf("obj = %v\n", obj)
param_4 := obj.Search("app")
fmt.Printf("param_4 = %v obj = %v\n", param_4, obj)
param4 := obj.Search("app")
fmt.Printf("param_4 = %v obj = %v\n", param4, obj)
}

View File

@ -6,7 +6,7 @@ type WordDictionary struct {
}
/** Initialize your data structure here. */
func Constructor_211() WordDictionary {
func Constructor211() WordDictionary {
return WordDictionary{children: make(map[rune]*WordDictionary)}
}

View File

@ -6,7 +6,7 @@ import (
)
func Test_Problem211(t *testing.T) {
obj := Constructor_211()
obj := Constructor211()
fmt.Printf("obj = %v\n", obj)
obj.AddWord("bad")
fmt.Printf("obj = %v\n", obj)
@ -15,12 +15,12 @@ func Test_Problem211(t *testing.T) {
obj.AddWord("mad")
fmt.Printf("obj = %v\n", obj)
param_1 := obj.Search("pad")
fmt.Printf("param_1 = %v obj = %v\n", param_1, obj)
param_2 := obj.Search("bad")
fmt.Printf("param_2 = %v obj = %v\n", param_2, obj)
param_3 := obj.Search(".ad")
fmt.Printf("param_3 = %v obj = %v\n", param_3, obj)
param_4 := obj.Search("b..")
fmt.Printf("param_4 = %v obj = %v\n", param_4, obj)
param1 := obj.Search("pad")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param2 := obj.Search("bad")
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
param3 := obj.Search(".ad")
fmt.Printf("param_3 = %v obj = %v\n", param3, obj)
param4 := obj.Search("b..")
fmt.Printf("param_4 = %v obj = %v\n", param4, obj)
}

View File

@ -1,6 +1,6 @@
package leetcode
func rob_213(nums []int) int {
func rob213(nums []int) int {
n := len(nums)
if n == 0 {
return 0
@ -12,10 +12,10 @@ func rob_213(nums []int) int {
return max(nums[0], nums[1])
}
// 由于首尾是相邻的,所以需要对比 [0n-1]、[1n] 这两个区间的最大值
return max(rob_213_(nums, 0, n-2), rob_213_(nums, 1, n-1))
return max(rob213_1(nums, 0, n-2), rob213_1(nums, 1, n-1))
}
func rob_213_(nums []int, start, end int) int {
func rob213_1(nums []int, start, end int) int {
preMax := nums[start]
curMax := max(preMax, nums[start+1])
for i := start + 2; i <= end; i++ {

View File

@ -45,7 +45,7 @@ func Test_Problem213(t *testing.T) {
for _, q := range qs {
_, p := q.ans213, q.para213
fmt.Printf("【input】:%v 【output】:%v\n", p, rob_213(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, rob213(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -3,7 +3,7 @@ package leetcode
import "sort"
// 解法一 排序,排序的方法反而速度是最快的
func findKthLargest_(nums []int, k int) int {
func findKthLargest1(nums []int, k int) int {
sort.Ints(nums)
return nums[len(nums)-k]
}
@ -17,13 +17,10 @@ func findKthLargest(nums []int, k int) int {
}
func selection(arr []int, l, r, k int) int {
if l == r {
return arr[l]
}
p := partition__(arr, l, r)
p := partition164(arr, l, r)
if k == p {
return arr[p]
} else if k < p {

View File

@ -49,7 +49,7 @@ func Test_Problem215(t *testing.T) {
question215{
para215{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6, 7, 7, 8, 2, 3, 1, 1, 1, 10, 11, 5, 6, 2, 4, 7, 8, 5, 6}, 20},
ans215{10},
ans215{2},
},
}

View File

@ -41,7 +41,7 @@ func calculate(s string) int {
}
// 解法二
func calculate_(s string) int {
func calculate1(s string) int {
stack := []byte{}
for i := 0; i < len(s); i++ {
if s[i] == ' ' {

View File

@ -6,7 +6,7 @@ type MyStack struct {
}
/** Initialize your data structure here. */
func Constructor_225() MyStack {
func Constructor225() MyStack {
return MyStack{[]int{}, []int{}}
}

View File

@ -6,18 +6,18 @@ import (
)
func Test_Problem225(t *testing.T) {
obj := Constructor_225()
obj := Constructor225()
fmt.Printf("obj = %v\n", obj)
param_5 := obj.Empty()
fmt.Printf("param_5 = %v\n", param_5)
param5 := obj.Empty()
fmt.Printf("param_5 = %v\n", param5)
obj.Push(2)
fmt.Printf("obj = %v\n", obj)
obj.Push(10)
fmt.Printf("obj = %v\n", obj)
param_2 := obj.Pop()
fmt.Printf("param_2 = %v\n", param_2)
param_3 := obj.Top()
fmt.Printf("param_3 = %v\n", param_3)
param_4 := obj.Empty()
fmt.Printf("param_4 = %v\n", param_4)
param2 := obj.Pop()
fmt.Printf("param_2 = %v\n", param2)
param3 := obj.Top()
fmt.Printf("param_3 = %v\n", param3)
param4 := obj.Empty()
fmt.Printf("param_4 = %v\n", param4)
}

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一 时间复杂度 O(n) 空间复杂度 O(1)
func majorityElement_229(nums []int) []int {
func majorityElement229(nums []int) []int {
// since we are checking if a num appears more than 1/3 of the time
// it is only possible to have at most 2 nums (>1/3 + >1/3 = >2/3)
count1, count2, candidate1, candidate2 := 0, 0, 0, 1
@ -46,7 +46,7 @@ func majorityElement_229(nums []int) []int {
}
// 解法二 时间复杂度 O(n) 空间复杂度 O(n)
func majorityElement_229_(nums []int) []int {
func majorityElement229_1(nums []int) []int {
result, m := make([]int, 0), make(map[int]int)
for _, val := range nums {
if v, ok := m[val]; ok {

View File

@ -41,7 +41,7 @@ func Test_Problem229(t *testing.T) {
for _, q := range qs {
_, p := q.ans229, q.para229
fmt.Printf("【input】:%v 【output】:%v\n", p, majorityElement_229(p.s))
fmt.Printf("【input】:%v 【output】:%v\n", p, majorityElement229(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -10,18 +10,18 @@ package leetcode
*/
func kthSmallest(root *TreeNode, k int) int {
res, count := 0, 0
inorder_(root, k, &count, &res)
inorder230(root, k, &count, &res)
return res
}
func inorder_(node *TreeNode, k int, count *int, ans *int) {
func inorder230(node *TreeNode, k int, count *int, ans *int) {
if node != nil {
inorder_(node.Left, k, count, ans)
inorder230(node.Left, k, count, ans)
*count++
if *count == k {
*ans = node.Val
return
}
inorder_(node.Right, k, count, ans)
inorder230(node.Right, k, count, ans)
}
}

View File

@ -6,19 +6,19 @@ func isPowerOfTwo(num int) bool {
}
// 解法二 数论
func isPowerOfTwo_1(num int) bool {
func isPowerOfTwo1(num int) bool {
return num > 0 && (1073741824%num == 0)
}
// 解法三 打表法
func isPowerOfTwo_2(num int) bool {
func isPowerOfTwo2(num int) bool {
allPowerOfTwoMap := map[int]int{1: 1, 2: 2, 4: 4, 8: 8, 16: 16, 32: 32, 64: 64, 128: 128, 256: 256, 512: 512, 1024: 1024, 2048: 2048, 4096: 4096, 8192: 8192, 16384: 16384, 32768: 32768, 65536: 65536, 131072: 131072, 262144: 262144, 524288: 524288, 1048576: 1048576, 2097152: 2097152, 4194304: 4194304, 8388608: 8388608, 16777216: 16777216, 33554432: 33554432, 67108864: 67108864, 134217728: 134217728, 268435456: 268435456, 536870912: 536870912, 1073741824: 1073741824}
_, ok := allPowerOfTwoMap[num]
return ok
}
// 解法四 循环
func isPowerOfTwo_3(num int) bool {
func isPowerOfTwo3(num int) bool {
for num >= 2 {
if num%2 == 0 {
num = num / 2

View File

@ -6,7 +6,7 @@ type MyQueue struct {
}
/** Initialize your data structure here. */
func Constructor_232() MyQueue {
func Constructor232() MyQueue {
tmp1, tmp2 := []int{}, []int{}
return MyQueue{Stack: &tmp1, Queue: &tmp2}
}

View File

@ -6,16 +6,16 @@ import (
)
func Test_Problem232(t *testing.T) {
obj := Constructor_232()
obj := Constructor232()
fmt.Printf("obj = %v\n", obj)
obj.Push(2)
fmt.Printf("obj = %v\n", obj)
obj.Push(10)
fmt.Printf("obj = %v\n", obj)
param_2 := obj.Pop()
fmt.Printf("param_2 = %v\n", param_2)
param_3 := obj.Peek()
fmt.Printf("param_3 = %v\n", param_3)
param_4 := obj.Empty()
fmt.Printf("param_4 = %v\n", param_4)
param2 := obj.Pop()
fmt.Printf("param_2 = %v\n", param2)
param3 := obj.Peek()
fmt.Printf("param_3 = %v\n", param3)
param4 := obj.Empty()
fmt.Printf("param_4 = %v\n", param4)
}

View File

@ -9,7 +9,7 @@ package leetcode
*/
// 此题和 143 题 Reorder List 思路基本一致
func isPalindrome_(head *ListNode) bool {
func isPalindrome234(head *ListNode) bool {
if head == nil || head.Next == nil {
return true
}
@ -56,6 +56,7 @@ func isPalindrome_(head *ListNode) bool {
return res
}
// L2ss define
func L2ss(head *ListNode) []int {
res := []int{}

View File

@ -81,7 +81,7 @@ func Test_Problem234(t *testing.T) {
for _, q := range qs {
_, p := q.ans234, q.para234
fmt.Printf("【input】:%v 【output】:%v\n", p, isPalindrome_(S2l(p.one)))
fmt.Printf("【input】:%v 【output】:%v\n", p, isPalindrome234(S2l(p.one)))
}
fmt.Printf("\n\n\n")
}

View File

@ -8,12 +8,12 @@ package leetcode
* Right *ListNode
* }
*/
func lowestCommonAncestor_(root, p, q *TreeNode) *TreeNode {
func lowestCommonAncestor236(root, p, q *TreeNode) *TreeNode {
if root == nil || root == q || root == p {
return root
}
left := lowestCommonAncestor_(root.Left, p, q)
right := lowestCommonAncestor_(root.Right, p, q)
left := lowestCommonAncestor236(root.Left, p, q)
right := lowestCommonAncestor236(root.Right, p, q)
if left != nil {
if right != nil {
return root

View File

@ -62,7 +62,7 @@ func Test_Problem236(t *testing.T) {
rootOne := Ints2TreeNode(p.one)
rootTwo := Ints2TreeNode(p.two)
rootThr := Ints2TreeNode(p.thr)
fmt.Printf("【output】:%v \n", lowestCommonAncestor_(rootOne, rootTwo, rootThr))
fmt.Printf("【output】:%v \n", lowestCommonAncestor236(rootOne, rootTwo, rootThr))
}
fmt.Printf("\n\n\n")
}

View File

@ -1,7 +1,7 @@
package leetcode
// 解法一 暴力解法 O(nk):
func maxSlidingWindow_(a []int, k int) []int {
// 解法一 暴力解法 O(nk)
func maxSlidingWindow1(a []int, k int) []int {
res := make([]int, 0, k)
n := len(a)
if n == 0 {

View File

@ -23,7 +23,7 @@ func isAnagram(s string, t string) bool {
}
// 解法二
func isAnagram_(s string, t string) bool {
func isAnagram1(s string, t string) bool {
if s == "" && t == "" {
return true
}

View File

@ -22,8 +22,8 @@ func hIndex(citations []int) int {
}
// 解法二
func hIndex_(citations []int) int {
quickSort__(citations, 0, len(citations)-1)
func hIndex1(citations []int) int {
quickSort164(citations, 0, len(citations)-1)
hIndex := 0
for i := len(citations) - 1; i >= 0; i-- {
if citations[i] >= len(citations)-i {

View File

@ -19,7 +19,7 @@ func findDuplicate(nums []int) int {
}
// 解法二
func findDuplicate_(nums []int) int {
func findDuplicate1(nums []int) int {
if len(nums) == 0 {
return 0
}

View File

@ -19,7 +19,7 @@ func lengthOfLIS(nums []int) int {
}
// 解法二 O(n log n) DP
func lengthOfLIS_(nums []int) int {
func lengthOfLIS1(nums []int) int {
dp := []int{}
for _, num := range nums {
i := sort.SearchInts(dp, num)

View File

@ -35,7 +35,6 @@ func recursiveCheck(num string, x1 int, x2 int, left int) bool {
}
if strings.HasPrefix(num[left:], strconv.Itoa(x1+x2)) {
return recursiveCheck(num, x2, x1+x2, left+len(strconv.Itoa(x1+x2)))
} else {
return false
}
return false
}

View File

@ -5,12 +5,12 @@ import (
)
// 解法一 DP
func maxProfit_309(prices []int) int {
func maxProfit309(prices []int) int {
if len(prices) <= 1 {
return 0
}
buy, sell := make([]int, len(prices)), make([]int, len(prices))
for i, _ := range buy {
for i := range buy {
buy[i] = math.MinInt64
}
buy[0] = -prices[0]
@ -24,7 +24,7 @@ func maxProfit_309(prices []int) int {
}
// 解法二 优化辅助空间的 DP
func maxProfit_309_(prices []int) int {
func maxProfit309_1(prices []int) int {
if len(prices) <= 1 {
return 0
}

View File

@ -61,7 +61,7 @@ func Test_Problem309(t *testing.T) {
for _, q := range qs {
_, p := q.ans309, q.para309
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit_309(p.one))
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit309(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -9,7 +9,7 @@ func wiggleSort(nums []int) {
if len(nums) < 2 {
return
}
median := findKthLargest_324(nums, (len(nums)+1)/2)
median := findKthLargest324(nums, (len(nums)+1)/2)
n, i, left, right := len(nums), 0, 0, len(nums)-1
for i <= right {
@ -30,29 +30,29 @@ func indexMap(index, n int) int {
return (1 + 2*index) % (n | 1)
}
func findKthLargest_324(nums []int, k int) int {
func findKthLargest324(nums []int, k int) int {
if len(nums) == 0 {
return 0
}
return selection_324(nums, 0, len(nums)-1, len(nums)-k)
return selection324(nums, 0, len(nums)-1, len(nums)-k)
}
func selection_324(arr []int, l, r, k int) int {
func selection324(arr []int, l, r, k int) int {
if l == r {
return arr[l]
}
p := partition__324(arr, l, r)
p := partition324(arr, l, r)
if k == p {
return arr[p]
} else if k < p {
return selection_324(arr, l, p-1, k)
return selection324(arr, l, p-1, k)
} else {
return selection_324(arr, p+1, r, k)
return selection324(arr, p+1, r, k)
}
}
func partition__324(a []int, lo, hi int) int {
func partition324(a []int, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
@ -66,7 +66,7 @@ func partition__324(a []int, lo, hi int) int {
}
// 解法二
func wiggleSort_(nums []int) {
func wiggleSort1(nums []int) {
if len(nums) < 2 {
return
}

View File

@ -7,7 +7,7 @@ func isPowerOfThree(n int) bool {
}
// 解法二 打表法
func isPowerOfThree_1(n int) bool {
func isPowerOfThree1(n int) bool {
// 1162261467 is 3^19, 3^20 is bigger than int
allPowerOfThreeMap := map[int]int{1: 1, 3: 3, 9: 9, 27: 27, 81: 81, 243: 243, 729: 729, 2187: 2187, 6561: 6561, 19683: 19683, 59049: 59049, 177147: 177147, 531441: 531441, 1594323: 1594323, 4782969: 4782969, 14348907: 14348907, 43046721: 43046721, 129140163: 129140163, 387420489: 387420489, 1162261467: 1162261467}
_, ok := allPowerOfThreeMap[n]
@ -15,7 +15,7 @@ func isPowerOfThree_1(n int) bool {
}
// 解法三 循环
func isPowerOfThree_2(num int) bool {
func isPowerOfThree2(num int) bool {
for num >= 3 {
if num%3 == 0 {
num = num / 3

View File

@ -6,7 +6,7 @@ func isPowerOfFour(num int) bool {
}
// 解法二 循环
func isPowerOfFour_(num int) bool {
func isPowerOfFour1(num int) bool {
for num >= 4 {
if num%4 == 0 {
num = num / 4

View File

@ -1,7 +1,7 @@
package leetcode
func integerBreak(n int) int {
var dp []int = make([]int, n+1)
dp := make([]int, n+1)
dp[0], dp[1] = 1, 1
for i := 1; i <= n; i++ {
for j := 1; j < i; j++ {

View File

@ -22,7 +22,6 @@ func reverseVowels(s string) string {
func isVowels(s byte) bool {
if s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' || s == 'A' || s == 'E' || s == 'I' || s == 'O' || s == 'U' {
return true
} else {
return false
}
return false
}

View File

@ -19,6 +19,7 @@ func topKFrequent(nums []int, k int) []int {
return result
}
// Item define
type Item struct {
key int
count int
@ -40,11 +41,13 @@ func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
// Push define
func (pq *PriorityQueue) Push(x interface{}) {
item := x.(*Item)
*pq = append(*pq, item)
}
// Pop define
func (pq *PriorityQueue) Pop() interface{} {
n := len(*pq)
item := (*pq)[n-1]

View File

@ -1,7 +1,7 @@
package leetcode
// 暴力打表法
func countNumbersWithUniqueDigits_(n int) int {
func countNumbersWithUniqueDigits1(n int) int {
res := []int{1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691}
if n >= 10 {
return res[10]

View File

@ -22,7 +22,7 @@ func isSubsequence(s string, t string) bool {
}
// 解法二 O(n)
func isSubsequence_(s string, t string) bool {
func isSubsequence1(s string, t string) bool {
for len(s) > 0 && len(t) > 0 {
if s[0] == t[0] {
s = s[1:]

Some files were not shown because too many files have changed in this diff Show More