├── .gitignore ├── LICENSE ├── README.md ├── algorithms ├── 001-twoSum │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 0019-Remove Nth Node From End of List │ ├── Bingjian-Zhu.md │ └── index.md ├── 002-addTwoNumbers │ ├── Bingjian-Zhu.md │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 003-Longest Substring Without Repeating Characters │ ├── Bingjian-Zhu.md │ └── index.md ├── 005-Longest Palindromic Substring │ ├── Bingjian-Zhu.md │ └── index.md ├── 006-ZigZag Conversion │ ├── Bingjian-Zhu.md │ └── index.md ├── 007-Reverse Integer │ ├── Bingjian-Zhu.md │ └── index.md ├── 008-String to Integer (atoi) │ ├── Bingjian-Zhu.md │ └── index.md ├── 009-Palindrome Number │ ├── Bingjian-Zhu.md │ └── index.md ├── 011-Container With Most Water │ ├── Bingjian-Zhu.md │ └── index.md ├── 012-Integer to Roman │ ├── Bingjian-Zhu.md │ └── index.md ├── 013-Roman to Integer │ ├── Bingjian-Zhu.md │ └── index.md ├── 014-Longest Common Prefix │ ├── Bingjian-Zhu.md │ └── index.md ├── 015-3Sum │ ├── Bingjian-Zhu.md │ └── index.md ├── 016-3Sum Closest │ ├── Bingjian-Zhu.md │ └── index.md ├── 017-letter-combinations-of-a-phone-number │ ├── Bingjian-Zhu.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 018-4Sum │ ├── Bingjian-Zhu.md │ └── index.md ├── 020-Valid Parentheses │ ├── Bingjian-Zhu.md │ └── index.md ├── 021-Merge Two Sorted Lists │ ├── Bingjian-Zhu.md │ └── index.md ├── 022-Generate Parentheses │ ├── Bingjian-Zhu.md │ └── index.md ├── 023-Merge k Sorted Lists │ ├── Bingjian-Zhu.md │ └── index.md ├── 024-Swap Nodes in Pairs │ ├── Bingjian-Zhu.md │ └── index.md ├── 025-Reverse Nodes in k-Group │ ├── Bingjian-Zhu.md │ └── index.md ├── 026-remove_duplicates_from_sorted_array │ ├── Bingjian-Zhu.md │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 027-remove_element │ ├── Bingjian-Zhu.md │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 028-Implement strStr() │ ├── Bingjian-Zhu.md │ └── index.md ├── 031-Next Permutation │ ├── Bingjian-Zhu.md │ └── index.md ├── 033-Search in Rotated Sorted Array │ ├── Bingjian-Zhu.md │ └── index.md ├── 034-find-first-and-last-position-of-element-in-sorted-array │ ├── Bingjian-Zhu.md │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 035-search_insert_position │ ├── Bingjian-Zhu.md │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 041-First Missing Positive │ ├── Bingjian-Zhu.md │ └── index.md ├── 043-Multiply Strings │ ├── Bingjian-Zhu.md │ └── index.md ├── 045-Jump Game II │ ├── Bingjian-Zhu.md │ └── index.md ├── 046-permutations │ ├── Bingjian-Zhu.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 047-permutations-ii │ ├── byxjxj.md │ └── index.md ├── 049-Group Anagrams │ ├── Bingjian-Zhu.md │ └── index.md ├── 053-maximum_subarray │ ├── LIYINGZHE.md │ ├── byxjxj.md │ ├── index.md │ └── lhwang95.md ├── 055-Jump Game │ ├── Bingjian-Zhu.md │ └── index.md ├── 062-unique-paths │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 064-minimum-path-sum │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 066-plusOne │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 069-sqrtx │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 070-climbing-stairs │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 075-sort-colors │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── lightfish-zhang.go │ └── whao246.md ├── 088-merge_sorted_array │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 091-decode-ways │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ └── index.md ├── 093-restore-ip-addresses │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 093-word-search │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 095-unique-binary-search-trees-ii │ ├── index.md │ └── lightfish-zhang.md ├── 118-pascals_triangle │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 119-pascals_triangle_ii │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── lhwang95.md │ └── mojiajuzi.md ├── 121-best-time-to-buy-and-sell-stock │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ └── lhwang95.md ├── 122-best-time-to-buy-and-sell-stock-ii │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lhwang95.md ├── 127-word-ladder │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 130-surrounded-regions │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 141-linked-list-cycle │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 153-find-minimum-in-rotated-sorted-array │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 167-two_sum_input_array_is_sorted │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ └── lhwang95.md ├── 169-majority_element │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ └── lhwang95.md ├── 189-rotate_array │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 198-house-robber │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 200-number-of-islands │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 213-house-robber-ii │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 215-kth-largest-element-in-an-array │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 217-contains_duplicate │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── mojiajuzi.md │ └── whao246.md ├── 219-contains-duplicate-ii │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── mojiajuzi.md │ └── whao246.md ├── 241-different-ways-to-add-parentheses │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 257-binary-tree-paths │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 268-missing-number │ ├── 268. Missing Number.md │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── mojiajuzi.md │ └── whao246.md ├── 278-first-bad-version │ └── index.md ├── 279-perfect-squares │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 283-move-zeroes │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── mojiajuzi.md │ ├── whao246.md │ └── wlk.md ├── 300-longest-increasing-subsequence │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 303-range-sum-query-immutable │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 343-integer-break │ ├── byxjxj.md │ └── index.md ├── 345-reverse-vowels-of-a-string │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 347-top-k-frequent-elements │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 406-queue-reconstruction-by-height │ ├── byxjxj.md │ └── index.md ├── 413-arithmetic-slices │ ├── byxjxj.md │ └── index.md ├── 414-third-maximum-number │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 417-pacific-atlantic-water-flow │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 435-non-overlapping-intervals │ ├── byxjxj.md │ └── index.md ├── 448-find-all-numbers-disappeared-in-an-array │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 451-sort-characters-by-frequency │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 452-minimum-number-of-arrows-to-burst-balloons │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ └── index.md ├── 455-assign-cookies │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lightfish-zhang.md │ └── whao246.md ├── 485-max-consecutive-ones │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── mojiajuzi.md │ └── whao246.md ├── 509-fibonacci-number │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 524-longest-word-in-dictionary-through-deleting │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 532-k-diff-pairs-in-an-array │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 540-single-element-in-a-sorted-array │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 547-friend-circles │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 561-array-partition-i │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 566-reshape-the-matrix │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 581-shortest-unsorted-continuous-subarray │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 605-can-place-flowers │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 628-maximum-product-of-three-numbers │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 633-sum-of-square-numbers │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 643-maximum-average-subarray-i │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 646-maximum-length-of-pair-chain │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 661-image-smoother │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 665-non-decreasing-array │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── lightfish-zhang.md │ └── whao246.md ├── 674-longest-continuous-increasing-subsequence │ ├── LIYINGZHEN.md │ ├── RudeFish.md │ ├── byxjxj.md │ ├── lhwang95.md │ └── whao246.md ├── 680-valid-palindrome-ii │ ├── LIYINGZHEN.md │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 695-max-area-of-island │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 697-degree-of-an-array │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 717-1-bit-and-2-bit-characters │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ ├── lightfish-zhang.md │ └── whao246.md ├── 744-find-smallest-letter-greater-than-target │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 763-partition-labels │ ├── byxjxj.md │ ├── index.md │ └── lightfish-zhang.md ├── 764-find-pivot-index │ ├── byxjxj.md │ ├── index.md │ ├── lhwang95.md │ └── whao246.md ├── 997-Find-the-Town-Judge │ └── xz1220.md └── 专题训练 │ ├── 二分查找 │ └── index.md │ ├── 分治 │ └── index.md │ ├── 双指针 │ ├── LIYINGZHEN.md │ └── index.md │ ├── 排序 │ └── index.md │ └── 贪心 │ └── index.md ├── index.md ├── maintainer.md └── wiki.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | .idea 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leetcode 2 | LeetCode Problems' Solutions In Go For Learning 3 | 4 | ## 参考 5 | 6 | - https://github.com/haoel/leetcode 7 | - https://github.com/qiyuangong/leetcode 8 | 9 | 10 | ## 项目结构 11 | 12 | - 每一个题目建立一个文件夹,文件名名称以题目名称命名 13 | 14 | - 每一个文件下面包含一个`index.md`文件用来说明题目以及解题思路的说明与总结 15 | 16 | - 个人的解题思路以及代码以文件的形式提交到对应的文件夹中,文件以`github用户名.md`作为命名 17 | 18 | - 项目根目录下面会有一个列表,该列表对应题目列表并指向对应的文件夹 19 | -------------------------------------------------------------------------------- /algorithms/001-twoSum/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [1. Two Sum](https://leetcode.com/problems/two-sum/) 4 | 5 | ### 解題思路 6 | 7 | **利用 map** 8 | 9 | 1. key 是列表中的元素 10 | 2. value 是元素的下標 11 | 12 | counterPart 是對應的元素,相加會等於 target 13 | 14 | ### 代碼 15 | 16 | ```go 17 | func twoSum(nums []int, target int) []int { 18 | // m store value and index 19 | visited := make(map[int]int) 20 | 21 | for currentIndex, v := range nums { 22 | // 找對應的元素 23 | counterPart := target - v 24 | 25 | counterPartIndex, ok := visited[counterPart] 26 | if ok { 27 | // 在 map 找到直接回傳 28 | return []int{counterPartIndex, currentIndex} 29 | } 30 | // 找不到就把自己加入到 map 裡面 31 | visited[v] = currentIndex 32 | } 33 | 34 | return []int{-1, -1} 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/001-twoSum/index.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | 3 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 4 | 5 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 6 | 7 | 8 | ``` 9 | Given nums = [2, 7, 11, 15], target = 9, 10 | 11 | Because nums[0] + nums[1] = 2 + 7 = 9, 12 | return [0, 1]. 13 | ``` 14 | 15 | [原题地址](https://leetcode.com/problems/two-sum/) 16 | 17 | ## 解题过程 18 | -------------------------------------------------------------------------------- /algorithms/001-twoSum/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func twoSum(nums []int, target int) []int { 3 | 4 | var res []int 5 | 6 | finish:=false 7 | 8 | for i,v := range nums { 9 | diff:=target-v 10 | if finish { 11 | break 12 | } 13 | 14 | for j,k := range nums { 15 | if i==j { 16 | continue 17 | } 18 | if diff==k { 19 | finish=true 20 | res=append(res,i) 21 | res=append(res,j) 22 | break 23 | } 24 | } 25 | } 26 | return res 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/0019-Remove Nth Node From End of List/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 时间复杂度:O(n) 2 | ```golang 3 | func removeNthFromEnd(head *ListNode, n int) *ListNode { 4 | var temp, delNodeParent *ListNode 5 | temp = head 6 | delNodeParent = head //需要删除节点的父节点 7 | for i := 0; i < n; i++ { 8 | temp = temp.Next 9 | } 10 | if temp == nil { //判断是否删除头节点 11 | head = head.Next 12 | return head 13 | } 14 | for temp.Next != nil { 15 | temp = temp.Next 16 | delNodeParent = delNodeParent.Next 17 | } 18 | delNodeParent.Next = delNodeParent.Next.Next 19 | return head 20 | } 21 | ``` -------------------------------------------------------------------------------- /algorithms/0019-Remove Nth Node From End of List/index.md: -------------------------------------------------------------------------------- 1 | Given a linked list, remove the n-th node from the end of list and return its head. 2 | 3 | Example: 4 | 5 | Given linked list: 1->2->3->4->5, and n = 2. 6 | 7 | After removing the second node from the end, the linked list becomes 1->2->3->5. 8 | Note: 9 | 10 | Given n will always be valid. 11 | 12 | Follow up: 13 | 14 | Could you do this in one pass? 15 | 16 | 17 | [原文地址](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list) -------------------------------------------------------------------------------- /algorithms/002-addTwoNumbers/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) 4 | 5 | ### 解題思路 6 | 7 | 這題想通就不難了,遍歷兩個鏈表相加即可。 8 | 進位的部分要多練習。 9 | 10 | ### 代碼 11 | 12 | ```go 13 | /** 14 | * Definition for singly-linked list. 15 | * type ListNode struct { 16 | * Val int 17 | * Next *ListNode 18 | * } 19 | */ 20 | func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 21 | dummy := ListNode{} 22 | head := &dummy 23 | carry := 0 24 | 25 | for l1 != nil || l2 != nil || carry != 0 { 26 | sum := 0 27 | 28 | if l1 != nil { 29 | sum += l1.Val 30 | l1 = l1.Next 31 | } 32 | if l2 != nil { 33 | sum += l2.Val 34 | l2 = l2.Next 35 | } 36 | 37 | sum += carry 38 | // 取出進位值 39 | carry = sum / 10 40 | 41 | head.Next = &ListNode{Val: sum % 10} 42 | head = head.Next 43 | } 44 | 45 | return dummy.Next 46 | } 47 | ``` 48 | -------------------------------------------------------------------------------- /algorithms/002-addTwoNumbers/byxjxj.md: -------------------------------------------------------------------------------- 1 | 同步遍历两个链表,对位相加并记录进位,当下一结点都为nil且无进位时,返回结果,耗时12ms。 2 | ```golang 3 | /** 4 | * Definition for singly-linked list. 5 | * type ListNode struct { 6 | * Val int 7 | * Next *ListNode 8 | * } 9 | */ 10 | 11 | func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 12 | 13 | res := &ListNode{} 14 | cur := res 15 | c := 0 16 | 17 | for l1 != nil || l2 != nil || c != 0 { 18 | 19 | if l1 != nil { 20 | cur.Val += l1.Val 21 | l1 = l1.Next 22 | } 23 | if l2 != nil { 24 | cur.Val += l2.Val 25 | l2 = l2.Next 26 | } 27 | 28 | cur.Val += c 29 | c = cur.Val / 10 30 | cur.Val %= 10 31 | 32 | if l1 == nil && l2 == nil && c == 0 { 33 | return res 34 | } 35 | 36 | cur.Next = &ListNode{} 37 | cur = cur.Next 38 | } 39 | 40 | return nil 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /algorithms/002-addTwoNumbers/index.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | 3 | You are given two non-empty linked lists representing two non-negative integers. 4 | 5 | The digits are stored in reverse order and each of their nodes contain a single digit. 6 | 7 | Add the two numbers and return it as a linked list. 8 | 9 | You may assume the two numbers do not contain any leading zero, except the number 0 itself. 10 | 11 | 12 | ``` 13 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 14 | Output: 7 -> 0 -> 8 15 | Explanation: 342 + 465 = 807. 16 | ``` 17 | 18 | [原题地址](https://leetcode.com/problems/add-two-numbers/) 19 | -------------------------------------------------------------------------------- /algorithms/003-Longest Substring Without Repeating Characters/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 滑动窗口,例如题中的abcabcbb,列表中有abc后,发现再放a就重复了,然后把左边的a去掉。 2 | 用map的原因是它查找元素是否存在的效率为O(1) 3 | ```golang 4 | func lengthOfLongestSubstring(s string) int { 5 | res := 0 6 | lenS := len(s) 7 | if lenS == 0 { 8 | return res 9 | } 10 | strMap := make(map[byte]struct{}, 100) 11 | left := 0 12 | right := 0 13 | for left < lenS && right < lenS { 14 | if _, ok := strMap[s[right]]; !ok { 15 | strMap[s[right]] = struct{}{} 16 | right++ 17 | res = maxInt(res, len(strMap)) 18 | } else { 19 | delete(strMap, s[left]) 20 | left++ 21 | } 22 | } 23 | return res 24 | } 25 | 26 | func maxInt(x, y int) int { 27 | if x < y { 28 | return y 29 | } 30 | return x 31 | } 32 | ``` -------------------------------------------------------------------------------- /algorithms/003-Longest Substring Without Repeating Characters/index.md: -------------------------------------------------------------------------------- 1 | Given a string, find the length of the longest substring without repeating characters. 2 | 3 | Example 1: 4 | ``` 5 | Input: "abcabcbb" 6 | Output: 3 7 | Explanation: The answer is "abc", with the length of 3. 8 | Example 2: 9 | ``` 10 | ``` 11 | Input: "bbbbb" 12 | Output: 1 13 | Explanation: The answer is "b", with the length of 1. 14 | Example 3: 15 | ``` 16 | Input: "pwwkew" 17 | Output: 3 18 | Explanation: The answer is "wke", with the length of 3. 19 | Note that the answer must be a substring, "pwke" is a subsequence and not a substring. 20 | ``` 21 | [原文地址](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) -------------------------------------------------------------------------------- /algorithms/005-Longest Palindromic Substring/index.md: -------------------------------------------------------------------------------- 1 | Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 2 | 3 | Example 1: 4 | ``` 5 | Input: "babad" 6 | Output: "bab" 7 | Note: "aba" is also a valid answer. 8 | ``` 9 | Example 2: 10 | ``` 11 | Input: "cbbd" 12 | Output: "bb" 13 | ``` 14 | [原题地址](https://leetcode-cn.com/problems/longest-palindromic-substring/) -------------------------------------------------------------------------------- /algorithms/006-ZigZag Conversion/index.md: -------------------------------------------------------------------------------- 1 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 2 | 3 | P A H N 4 | A P L S I I G 5 | Y I R 6 | And then read line by line: "PAHNAPLSIIGYIR" 7 | 8 | Write the code that will take a string and make this conversion given a number of rows: 9 | 10 | string convert(string s, int numRows); 11 | Example 1: 12 | ``` 13 | Input: s = "PAYPALISHIRING", numRows = 3 14 | Output: "PAHNAPLSIIGYIR" 15 | ``` 16 | Example 2: 17 | ``` 18 | Input: s = "PAYPALISHIRING", numRows = 4 19 | Output: "PINALSIGYAHRPI" 20 | ``` 21 | Explanation: 22 | 23 | P I N 24 | A L S I G 25 | Y A H R 26 | P I 27 | 28 | [原题地址](https://leetcode-cn.com/problems/zigzag-conversion/) -------------------------------------------------------------------------------- /algorithms/007-Reverse Integer/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 1.如为负数就先记录,并把负数取绝对值 2 | 2.求模取余,每次取数字的最后一位 3 | 4 | ```golang 5 | func reverse(x int) int { 6 | isNegative := false 7 | if x < 0 { 8 | isNegative = true 9 | x = x * -1 10 | } 11 | res := 0.0 12 | minVal := -math.Pow(2, 31) 13 | maxVal := math.Pow(2, 31) - 1 14 | for x > 0 { 15 | k := x % 10 16 | res = res*10.0 + float64(k) 17 | if res < minVal || res > maxVal { 18 | return 0 19 | } 20 | x = x / 10 21 | } 22 | if isNegative { 23 | return int(res * -1) 24 | } 25 | return int(res) 26 | } 27 | ``` -------------------------------------------------------------------------------- /algorithms/007-Reverse Integer/index.md: -------------------------------------------------------------------------------- 1 | Given a 32-bit signed integer, reverse digits of an integer. 2 | 3 | Example 1: 4 | ``` 5 | Input: 123 6 | Output: 321 7 | ``` 8 | Example 2: 9 | ``` 10 | Input: -123 11 | Output: -321 12 | ``` 13 | Example 3: 14 | ``` 15 | Input: 120 16 | Output: 21 17 | ``` 18 | Note: 19 | Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 20 | 21 | [原题地址](https://leetcode-cn.com/problems/reverse-integer/) -------------------------------------------------------------------------------- /algorithms/009-Palindrome Number/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 不转换成字符串的做法 2 | ```golang 3 | func isPalindrome(x int) bool { 4 | if x < 0 { 5 | return false 6 | } else if x < 10 { 7 | return true 8 | } else { 9 | num := 0 10 | tmp := x 11 | for x > 0 { 12 | num = num*10 + x%10 13 | x = x / 10 14 | } 15 | if tmp == num { 16 | return true 17 | } else { 18 | return false 19 | } 20 | } 21 | } 22 | ``` -------------------------------------------------------------------------------- /algorithms/009-Palindrome Number/index.md: -------------------------------------------------------------------------------- 1 | Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. 2 | 3 | Example 1: 4 | ``` 5 | Input: 121 6 | Output: true 7 | ``` 8 | Example 2: 9 | ``` 10 | Input: -121 11 | Output: false 12 | ``` 13 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 14 | Example 3: 15 | ``` 16 | Input: 10 17 | Output: false 18 | ``` 19 | Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 20 | Follow up: 21 | 22 | Coud you solve it without converting the integer to a string? 23 | 24 | [原文地址](https://leetcode-cn.com/problems/palindrome-number) -------------------------------------------------------------------------------- /algorithms/011-Container With Most Water/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 暴力法: 2 | ```golang 3 | func maxArea(height []int) int { 4 | max := 0 5 | lenH := len(height) 6 | for i := 0; i < lenH; i++ { 7 | for j := 1; j < lenH; j++ { 8 | 9 | tmp := smallInt(height[j], height[i]) * (j - i) 10 | if tmp > max { 11 | max = tmp 12 | } 13 | } 14 | } 15 | return max 16 | } 17 | func smallInt(x, y int) int { 18 | if x < y { 19 | return x 20 | } 21 | return y 22 | } 23 | ``` 24 | 25 | 双指针方法: 26 | ```golang 27 | func maxArea(height []int) int { 28 | max := 0 29 | i := 0 30 | j := len(height) - 1 31 | for i < j { 32 | tmp := smallInt(height[i], height[j]) * (j - i) 33 | if tmp > max { 34 | max = tmp 35 | } 36 | if height[i] < height[j] { 37 | i++ 38 | } else { 39 | j-- 40 | } 41 | } 42 | return max 43 | } 44 | func smallInt(x, y int) int { 45 | if x < y { 46 | return x 47 | } 48 | return y 49 | } 50 | ``` -------------------------------------------------------------------------------- /algorithms/011-Container With Most Water/index.md: -------------------------------------------------------------------------------- 1 | Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 2 | 3 | Note: You may not slant the container and n is at least 2. 4 | 5 | ![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg) 6 | The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. 7 | 8 | 9 | 10 | Example: 11 | ``` 12 | Input: [1,8,6,2,5,4,8,3,7] 13 | Output: 49 14 | ``` 15 | 16 | [原文地址](https://leetcode-cn.com/problems/container-with-most-water) 17 | -------------------------------------------------------------------------------- /algorithms/014-Longest Common Prefix/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func longestCommonPrefix(strs []string) string { 3 | lenStr := len(strs) 4 | if lenStr == 0 { 5 | return "" 6 | } 7 | var res = []byte{} 8 | i := 0 9 | isExit := false 10 | var tmp byte 11 | for !isExit { 12 | if i >= len(strs[0]) { 13 | return string(res) 14 | } 15 | tmp = strs[0][i] 16 | for index, val := range strs { 17 | if i >= len(val) || tmp != val[i] { 18 | isExit = true 19 | break 20 | } 21 | if index >= lenStr-1 { 22 | i++ 23 | res = append(res, tmp) 24 | } 25 | } 26 | } 27 | return string(res) 28 | } 29 | ``` -------------------------------------------------------------------------------- /algorithms/014-Longest Common Prefix/index.md: -------------------------------------------------------------------------------- 1 | Write a function to find the longest common prefix string amongst an array of strings. 2 | 3 | If there is no common prefix, return an empty string "". 4 | 5 | Example 1: 6 | 7 | Input: ["flower","flow","flight"] 8 | Output: "fl" 9 | Example 2: 10 | 11 | Input: ["dog","racecar","car"] 12 | Output: "" 13 | Explanation: There is no common prefix among the input strings. 14 | Note: 15 | 16 | All given inputs are in lowercase letters a-z. 17 | 18 | 19 | [原文地址](https://leetcode-cn.com/problems/longest-common-prefix) 20 | -------------------------------------------------------------------------------- /algorithms/015-3Sum/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 排序+双指针 2 | ```golang 3 | func threeSum(nums []int) [][]int { 4 | var res = [][]int{} 5 | lenNum := len(nums) 6 | if lenNum < 3 { 7 | return res 8 | } 9 | sort.Ints(nums) 10 | for i := 0; i < lenNum; i++ { 11 | if nums[i] > 0 { 12 | break 13 | } 14 | if i > 0 && nums[i] == nums[i-1] { 15 | continue // 去重 16 | } 17 | left := i + 1 18 | right := lenNum - 1 19 | for left < right { 20 | sum := nums[i] + nums[left] + nums[right] 21 | if sum == 0 { 22 | res = append(res, []int{nums[i], nums[left], nums[right]}) 23 | for left < right && nums[left] == nums[left+1] { 24 | left++ // 去重 25 | } 26 | for left < right && nums[right] == nums[right-1] { 27 | right-- // 去重 28 | } 29 | left++ 30 | right-- 31 | } else if sum < 0 { 32 | left++ 33 | } else if sum > 0 { 34 | right-- 35 | } 36 | } 37 | } 38 | return res 39 | } 40 | ``` -------------------------------------------------------------------------------- /algorithms/015-3Sum/index.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 2 | 3 | Note: 4 | 5 | The solution set must not contain duplicate triplets. 6 | 7 | Example: 8 | 9 | Given array nums = [-1, 0, 1, 2, -1, -4], 10 | 11 | A solution set is: 12 | [ 13 | [-1, 0, 1], 14 | [-1, -1, 2] 15 | ] 16 | 17 | [原文地址](https://leetcode-cn.com/problems/3sum) 18 | -------------------------------------------------------------------------------- /algorithms/016-3Sum Closest/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 排序+双指针 2 | ```golang 3 | func threeSumClosest(nums []int, target int) int { 4 | lenNum := len(nums) 5 | sort.Ints(nums) 6 | min := abs(nums[0] + nums[1] + nums[2] - target) 7 | minSum := nums[0] + nums[1] + nums[2] 8 | for i := 0; i < lenNum; i++ { 9 | left := i + 1 10 | right := lenNum - 1 11 | for left < right { 12 | sum := nums[i] + nums[left] + nums[right] 13 | tmp := abs(sum - target) 14 | if tmp < min { 15 | minSum, min = sum, tmp 16 | } 17 | if tmp == 0 { 18 | return minSum 19 | } else if sum < target { 20 | left++ 21 | } else { 22 | right-- 23 | } 24 | } 25 | } 26 | return minSum 27 | } 28 | 29 | func abs(x int) int { 30 | if x < 0 { 31 | return -x 32 | } 33 | return x 34 | } 35 | ``` -------------------------------------------------------------------------------- /algorithms/016-3Sum Closest/index.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 2 | 3 | Example: 4 | 5 | Given array nums = [-1, 2, 1, -4], and target = 1. 6 | 7 | The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 8 | 9 | [原文链接](https://leetcode-cn.com/problems/3sum-closest) -------------------------------------------------------------------------------- /algorithms/017-letter-combinations-of-a-phone-number/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 非递归方法,使用迭代,每次加一个字母 2 | 3 | ```golang 4 | func letterCombinations(digits string) []string { 5 | var ( 6 | lookup = map[byte][]string{ 7 | '0': []string{"0"}, 8 | '1': []string{"1"}, 9 | '2': []string{"a", "b", "c"}, 10 | '3': []string{"d", "e", "f"}, 11 | '4': []string{"g", "h", "i"}, 12 | '5': []string{"j", "k", "l"}, 13 | '6': []string{"m", "n", "o"}, 14 | '7': []string{"p", "q", "r", "s"}, 15 | '8': []string{"t", "u", "v"}, 16 | '9': []string{"w", "x", "y", "z"}, 17 | } 18 | res = []string{""} 19 | ) 20 | if digits == "" { 21 | return []string{} 22 | } 23 | for i := 0; i < len(digits); i++ { 24 | nextRes := []string{} 25 | for j := 0; j < len(lookup[digits[i]]); j++ { 26 | for _, tmp := range res { 27 | nextRes = append(nextRes, tmp+lookup[digits[i]][j]) 28 | } 29 | } 30 | res = nextRes 31 | } 32 | return res 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/017-letter-combinations-of-a-phone-number/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func letterCombinations(digits string) []string { 3 | 4 | if len(digits) == 0 { 5 | return []string{} 6 | } 7 | 8 | charMap := map[byte]string{'2': "abc", '3': "def", '4': "ghi", '5': "jkl", '6': "mno", '7': "pqrs", '8': "tuv", '9': "wxyz"} 9 | res := []string{} 10 | 11 | var dfs func(int, string) 12 | dfs = func(index int, com string) { 13 | if index == len(digits) { 14 | res = append(res, com) 15 | return 16 | } 17 | 18 | for _, c := range charMap[digits[index]] { 19 | dfs(index+1, com+string(c)) 20 | } 21 | } 22 | 23 | dfs(0, "") 24 | 25 | return res 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/017-letter-combinations-of-a-phone-number/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/017-letter-combinations-of-a-phone-number/index.md -------------------------------------------------------------------------------- /algorithms/017-letter-combinations-of-a-phone-number/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 17.letter combinations of a phone number 2 | 3 | ```golang 4 | func letterCombinations(digits string) []string { 5 | res := []string{} 6 | if len(digits) == 0 { 7 | return res 8 | } 9 | m := map[byte][]string{ 10 | '0': []string{"0"}, 11 | '1': []string{"1"}, 12 | '2': []string{"a", "b", "c"}, 13 | '3': []string{"d", "e", "f"}, 14 | '4': []string{"g", "h", "i"}, 15 | '5': []string{"j", "k", "l"}, 16 | '6': []string{"m", "n", "o"}, 17 | '7': []string{"p", "q", "r", "s"}, 18 | '8': []string{"t", "u", "v"}, 19 | '9': []string{"w", "x", "y", "z"}, 20 | } 21 | res = m[digits[0]] 22 | for i := 1; i < len(digits); i++ { 23 | reslen := len(res) 24 | for j := 0; j < reslen; j++ { 25 | for _, ch := range m[digits[i]] { 26 | res = append(res, res[j]+ch) 27 | } 28 | } 29 | res = res[reslen:] 30 | } 31 | return res 32 | } 33 | ``` -------------------------------------------------------------------------------- /algorithms/018-4Sum/index.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 2 | 3 | Note: 4 | 5 | The solution set must not contain duplicate quadruplets. 6 | 7 | Example: 8 | 9 | Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. 10 | 11 | A solution set is: 12 | [ 13 | [-1, 0, 0, 1], 14 | [-2, -1, 1, 2], 15 | [-2, 0, 0, 2] 16 | ] 17 | 18 | [原文地址](https://leetcode-cn.com/problems/4sum) -------------------------------------------------------------------------------- /algorithms/020-Valid Parentheses/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 利用栈的特性 2 | ```golang 3 | func isValid(s string) bool { 4 | lenS := len(s) 5 | stack := make([]byte, lenS) 6 | top := 0 //头指针 7 | for i := 0; i < lenS; i++ { 8 | switch s[i] { 9 | case '(': 10 | stack[top] = '(' 11 | top++ 12 | case '[': 13 | stack[top] = '[' 14 | top++ 15 | case '{': 16 | stack[top] = '{' 17 | top++ 18 | case ')': 19 | if top > 0 && stack[top-1] == '(' { 20 | top-- 21 | } else { 22 | return false 23 | } 24 | case ']': 25 | if top > 0 && stack[top-1] == '[' { 26 | top-- 27 | } else { 28 | return false 29 | } 30 | case '}': 31 | if top > 0 && stack[top-1] == '{' { 32 | top-- 33 | } else { 34 | return false 35 | } 36 | } 37 | } 38 | return top == 0 39 | } 40 | ``` -------------------------------------------------------------------------------- /algorithms/020-Valid Parentheses/index.md: -------------------------------------------------------------------------------- 1 | Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 2 | 3 | An input string is valid if: 4 | 5 | Open brackets must be closed by the same type of brackets. 6 | Open brackets must be closed in the correct order. 7 | Note that an empty string is also considered valid. 8 | 9 | Example 1: 10 | 11 | Input: "()" 12 | Output: true 13 | Example 2: 14 | 15 | Input: "()[]{}" 16 | Output: true 17 | Example 3: 18 | 19 | Input: "(]" 20 | Output: false 21 | Example 4: 22 | 23 | Input: "([)]" 24 | Output: false 25 | Example 5: 26 | 27 | Input: "{[]}" 28 | Output: true 29 | 30 | 31 | [原文地址](https://leetcode-cn.com/problems/valid-parentheses) 32 | -------------------------------------------------------------------------------- /algorithms/021-Merge Two Sorted Lists/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { 3 | if l1 == nil { 4 | return l2 5 | } 6 | if l2 == nil { 7 | return l1 8 | } 9 | res := &ListNode{} 10 | if l1.Val < l2.Val { 11 | res.Val = l1.Val 12 | l1 = l1.Next 13 | } else { 14 | res.Val = l2.Val 15 | l2 = l2.Next 16 | } 17 | temp := res 18 | for l1 != nil || l2 != nil { 19 | if l1 == nil { 20 | temp.Next = l2 21 | return res 22 | } 23 | if l2 == nil { 24 | temp.Next = l1 25 | return res 26 | } 27 | if l1.Val < l2.Val { 28 | temp.Next = l1 29 | l1 = l1.Next 30 | temp = temp.Next 31 | } else { 32 | temp.Next = l2 33 | l2 = l2.Next 34 | temp = temp.Next 35 | } 36 | } 37 | return res 38 | } 39 | ``` -------------------------------------------------------------------------------- /algorithms/021-Merge Two Sorted Lists/index.md: -------------------------------------------------------------------------------- 1 | Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 2 | 3 | Example: 4 | 5 | Input: 1->2->4, 1->3->4 6 | Output: 1->1->2->3->4->4 7 | 8 | 9 | [原文地址](https://leetcode-cn.com/problems/merge-two-sorted-lists) -------------------------------------------------------------------------------- /algorithms/022-Generate Parentheses/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 回溯法 2 | ```golang 3 | func generateParenthesis(n int) []string { 4 | res := []string{} 5 | backTrack(&res, "", 0, 0, n) 6 | return res 7 | } 8 | 9 | func backTrack(res *[]string, s string, open int, close int, max int) { 10 | if len(s) == max*2 { 11 | *res = append(*res, s) 12 | return 13 | } 14 | if open < max { 15 | backTrack(res, s+"(", open+1, close, max) 16 | } 17 | if close < open { 18 | backTrack(res, s+")", open, close+1, max) 19 | } 20 | } 21 | ``` -------------------------------------------------------------------------------- /algorithms/022-Generate Parentheses/index.md: -------------------------------------------------------------------------------- 1 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 2 | 3 | For example, given n = 3, a solution set is: 4 | 5 | [ 6 | "((()))", 7 | "(()())", 8 | "(())()", 9 | "()(())", 10 | "()()()" 11 | ] 12 | 13 | [原文地址](https://leetcode-cn.com/problems/generate-parentheses) 14 | -------------------------------------------------------------------------------- /algorithms/023-Merge k Sorted Lists/index.md: -------------------------------------------------------------------------------- 1 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 2 | 3 | Example: 4 | 5 | Input: 6 | [ 7 |   1->4->5, 8 |   1->3->4, 9 |   2->6 10 | ] 11 | Output: 1->1->2->3->4->4->5->6 12 | 13 | 14 | [原文地址](https://leetcode-cn.com/problems/merge-k-sorted-lists) 15 | -------------------------------------------------------------------------------- /algorithms/024-Swap Nodes in Pairs/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 链表操作 2 | ```golang 3 | /** 4 | * Definition for singly-linked list. 5 | * type ListNode struct { 6 | * Val int 7 | * Next *ListNode 8 | * } 9 | */ 10 | func swapPairs(head *ListNode) *ListNode { 11 | res := head 12 | first := true 13 | tmp := &ListNode{} 14 | for head != nil { 15 | if head.Next != nil { 16 | temp := head.Next 17 | head.Next = temp.Next 18 | temp.Next = head 19 | if first { 20 | res = temp 21 | first = false 22 | } else { 23 | tmp.Next = temp 24 | } 25 | tmp = head 26 | } 27 | head = head.Next 28 | } 29 | return res 30 | } 31 | ``` -------------------------------------------------------------------------------- /algorithms/024-Swap Nodes in Pairs/index.md: -------------------------------------------------------------------------------- 1 | Given a linked list, swap every two adjacent nodes and return its head. 2 | 3 | You may not modify the values in the list's nodes, only nodes itself may be changed. 4 | 5 |   6 | 7 | Example: 8 | 9 | Given 1->2->3->4, you should return the list as 2->1->4->3. 10 | 11 | 12 | [原文链接](https://leetcode-cn.com/problems/swap-nodes-in-pairs) 13 | -------------------------------------------------------------------------------- /algorithms/025-Reverse Nodes in k-Group/index.md: -------------------------------------------------------------------------------- 1 | Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 2 | 3 | k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. 4 | 5 | Example: 6 | 7 | Given this linked list: 1->2->3->4->5 8 | 9 | For k = 2, you should return: 2->1->4->3->5 10 | 11 | For k = 3, you should return: 3->2->1->4->5 12 | 13 | Note: 14 | 15 | Only constant extra memory is allowed. 16 | You may not alter the values in the list's nodes, only nodes itself may be changed. 17 | 18 | 19 | [原文地址](https://leetcode-cn.com/problems/reverse-nodes-in-k-group) 20 | -------------------------------------------------------------------------------- /algorithms/026-remove_duplicates_from_sorted_array/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func removeDuplicates(nums []int) int { 3 | lenNum := len(nums) 4 | if lenNum <= 1 { 5 | return lenNum 6 | } 7 | temp := 0 8 | res := 1 9 | for i := 1; i < lenNum; i++ { 10 | if nums[temp] != nums[i] { 11 | temp = i 12 | nums[res] = nums[i] 13 | res++ 14 | } 15 | } 16 | return res 17 | } 18 | ``` -------------------------------------------------------------------------------- /algorithms/026-remove_duplicates_from_sorted_array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [26. Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) 4 | 5 | ### 解題思路 6 | 7 | 這題需要兩個下標做追蹤,一個是用來取代的下標 (replaceIndex),一個是負責往下遍歷 (currentIndex)。 8 | 9 | currentIndex 會跟前一個值比,只要不同把值賦予給 replaceIndex。 10 | 11 | 最後 replaceIndex 就是沒有重複值 array 的長度。 12 | 13 | ### 代碼 14 | 15 | ```go 16 | func removeDuplicates(nums []int) int { 17 | currentIndex, replaceIndex := 1, 1 18 | 19 | for currentIndex < len(nums) { 20 | if nums[currentIndex] != nums[currentIndex-1] { 21 | nums[replaceIndex] = nums[currentIndex] 22 | replaceIndex++ 23 | } 24 | currentIndex++ 25 | } 26 | 27 | return replaceIndex 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /algorithms/026-remove_duplicates_from_sorted_array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func removeDuplicates(nums []int) int { 3 | 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | res := 1 9 | cur := nums[0] 10 | for _, v := range nums { 11 | if v != cur { 12 | nums[res] = v 13 | cur = v 14 | res++ 15 | } 16 | } 17 | 18 | return res 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/026-remove_duplicates_from_sorted_array/lhwang95.md: -------------------------------------------------------------------------------- 1 | 44ms,7.7Mb,破坏数组内数据了,应该有不破坏数据的解法 2 | ``` 3 | func removeDuplicates(nums []int) int { 4 | res, cur := 0, 0 5 | for ; cur < len(nums); cur++ { 6 | if nums[res] != nums[cur] { 7 | res++ 8 | nums[res] = nums[cur] 9 | } 10 | } 11 | return res+1 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /algorithms/026-remove_duplicates_from_sorted_array/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | package main 3 | 4 | import "fmt" 5 | 6 | func main() { 7 | n := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4} 8 | l := removeDuplicates(n) 9 | fmt.Println(l) 10 | fmt.Println(n) 11 | } 12 | 13 | func removeDuplicates(nums []int) int { 14 | l := 0 15 | if len(nums) == 0 { 16 | return l 17 | } 18 | s := nums[0:1] 19 | for _, v := range nums { 20 | if v != s[l] { 21 | s = append(s, v) 22 | l++ 23 | } 24 | } 25 | l++ 26 | return l 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/027-remove_element/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func removeElement(nums []int, val int) int { 3 | res := 0 4 | for i := 0; i < len(nums); i++ { 5 | if nums[i] != val { 6 | nums[res] = nums[i] 7 | res++ 8 | } 9 | } 10 | return res 11 | } 12 | ``` -------------------------------------------------------------------------------- /algorithms/027-remove_element/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [27. Remove Element](https://leetcode.com/problems/remove-element/) 4 | 5 | ### 解題思路 6 | 7 | 這題跟 26. Remove Duplicates from Sorted Array 很像。 8 | 基本上就是遍歷數組,並且不斷把不同的值往前挪動。 9 | 10 | ### 代碼 11 | 12 | ```go 13 | func removeElement(nums []int, val int) int { 14 | replaceIndex := 0 15 | 16 | for i, v := range nums { 17 | if v != val { 18 | nums[replaceIndex] = nums[i] 19 | replaceIndex++ 20 | } 21 | } 22 | 23 | return replaceIndex 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/027-remove_element/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func removeElement(nums []int, val int) int{ 3 | 4 | res := 0 5 | 6 | for _, v := range nums { 7 | if v != val { 8 | nums[res] = v 9 | res++ 10 | } 11 | } 12 | 13 | return res 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/027-remove_element/lhwang95.md: -------------------------------------------------------------------------------- 1 | 0ms,2.4Mb 2 | 题目要求不能增加空间,用append之类的截取在底层上不确定会不会申请新的空间 3 | 结合使用案例,直接操作列表,将前面等于val的和后面不等的互换,最后返回从起始到最后一个不是val那部分的长度 4 | ``` 5 | func removeElement(nums []int, val int) int { 6 | head, tail := 0, len(nums)-1 7 | label: 8 | for tail >= 0 { 9 | for nums[tail] == val { // 从后往前遍历,找到第一个不等于val的索引 10 | tail-- 11 | if (tail < 0) || (head >= tail) { // 超出索引时跳出循环 12 | break label 13 | } 14 | } 15 | if (nums[head] == val) && (head < tail) { //从前往后遍历,找到第一个等于val的索引,交换 16 | temp := val; 17 | nums[head] = nums[tail] 18 | nums[tail] = temp; 19 | } 20 | head++ 21 | if head >= len(nums) { // 超出索引时跳出循环 22 | break 23 | } 24 | } 25 | if (tail >= 0 ) && (nums[tail] == val) { //预防 head==tail跳出时,该处等于val 26 | tail-- 27 | } 28 | return tail+1 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /algorithms/027-remove_element/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | package main 3 | 4 | import "fmt" 5 | 6 | func main() { 7 | n := []int{0, 1, 2, 2, 3, 0, 4, 2} 8 | fmt.Println(n) 9 | l := removeElement(n, 2) 10 | fmt.Println(l) 11 | fmt.Println(n) 12 | } 13 | ``` 14 | 15 | 解题思路一 16 | 17 | ```go 18 | func removeElement(nums []int, val int) int { 19 | 20 | s := nums[0:0] 21 | for _, v := range nums { 22 | if v != val { 23 | s = append(s, v) 24 | } 25 | } 26 | return len(s) 27 | } 28 | ``` 29 | 30 | 31 | 32 | 解题思路二 33 | 34 | ```go 35 | func removeElement(nums []int, val int) int { 36 | l := 0 37 | for _, v := range nums { 38 | if v != val { 39 | nums[l] = v 40 | l++ 41 | } 42 | } 43 | return l 44 | } 45 | ``` -------------------------------------------------------------------------------- /algorithms/028-Implement strStr()/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func strStr(haystack string, needle string) int { 3 | lenHaystack := len(haystack) 4 | lenNeedle := len(needle) 5 | if needle == "" { 6 | return 0 7 | } 8 | if lenHaystack < lenNeedle { 9 | return -1 10 | } 11 | for i := 0; i < lenHaystack; i++ { 12 | if haystack[i] == needle[0] { 13 | temp := i 14 | if lenNeedle == 1 { 15 | return i 16 | } else { 17 | for j := 1; j < lenNeedle; j++ { 18 | temp++ 19 | if temp > lenHaystack-1 { 20 | return -1 21 | } 22 | if haystack[temp] != needle[j] { 23 | break 24 | } 25 | if j >= lenNeedle-1 { 26 | return i 27 | } 28 | } 29 | } 30 | } 31 | } 32 | return -1 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/028-Implement strStr()/index.md: -------------------------------------------------------------------------------- 1 | Implement strStr(). 2 | 3 | Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 4 | 5 | Example 1: 6 | 7 | Input: haystack = "hello", needle = "ll" 8 | Output: 2 9 | Example 2: 10 | 11 | Input: haystack = "aaaaa", needle = "bba" 12 | Output: -1 13 | Clarification: 14 | 15 | What should we return when needle is an empty string? This is a great question to ask during an interview. 16 | 17 | For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). 18 | 19 | 20 | [原文链接](https://leetcode-cn.com/problems/implement-strstr) 21 | -------------------------------------------------------------------------------- /algorithms/031-Next Permutation/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func nextPermutation(nums []int) { 3 | i := len(nums) - 2 4 | for i >= 0 && nums[i+1] <= nums[i] { 5 | i-- 6 | } 7 | if i >= 0 { 8 | j := len(nums) - 1 9 | for j >= 0 && nums[j] <= nums[i] { 10 | j-- 11 | } 12 | nums[i], nums[j] = nums[j], nums[i] 13 | } 14 | reverse(nums, i+1) 15 | } 16 | 17 | func reverse(nums []int, start int) { 18 | i, j := start, len(nums)-1 19 | for i < j { 20 | nums[i], nums[j] = nums[j], nums[i] 21 | i++ 22 | j-- 23 | } 24 | } 25 | ``` -------------------------------------------------------------------------------- /algorithms/031-Next Permutation/index.md: -------------------------------------------------------------------------------- 1 | If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). 2 | 3 | The replacement must be in-place and use only constant extra memory. 4 | 5 | Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 6 | 7 | 1,2,3 → 1,3,2 8 | 3,2,1 → 1,2,3 9 | 1,1,5 → 1,5,1 10 | 11 | 12 | [原文链接](https://leetcode-cn.com/problems/next-permutation) -------------------------------------------------------------------------------- /algorithms/033-Search in Rotated Sorted Array/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func search(nums []int, target int) int { 3 | low := 0 4 | hight := len(nums) - 1 5 | 6 | for low < hight { 7 | mid := (low + hight) / 2 8 | // 当[0,mid]有序时,向后规约条件 9 | if nums[0] <= nums[mid] && (target > nums[mid] || target < nums[0]) { 10 | low = mid + 1 11 | // 当[0,mid]发生旋转时,向后规约条件 12 | } else if target > nums[mid] && target < nums[0] { 13 | low = mid + 1 14 | } else { 15 | hight = mid 16 | } 17 | } 18 | if low == hight && nums[low] == target { 19 | return low 20 | } 21 | return -1 22 | } 23 | ``` -------------------------------------------------------------------------------- /algorithms/033-Search in Rotated Sorted Array/index.md: -------------------------------------------------------------------------------- 1 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 2 | 3 | (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). 4 | 5 | You are given a target value to search. If found in the array return its index, otherwise return -1. 6 | 7 | You may assume no duplicate exists in the array. 8 | 9 | Your algorithm's runtime complexity must be in the order of O(log n). 10 | 11 | Example 1: 12 | 13 | Input: nums = [4,5,6,7,0,1,2], target = 0 14 | Output: 4 15 | Example 2: 16 | 17 | Input: nums = [4,5,6,7,0,1,2], target = 3 18 | Output: -1 19 | 20 | 21 | [原文链接](https://leetcode-cn.com/problems/search-in-rotated-sorted-array) -------------------------------------------------------------------------------- /algorithms/034-find-first-and-last-position-of-element-in-sorted-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/034-find-first-and-last-position-of-element-in-sorted-array/index.md -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 二分查找 2 | ```golang 3 | func searchInsert(nums []int, target int) int { 4 | lenNum := len(nums) 5 | if (lenNum == 1 && nums[0] == target) || lenNum == 0 { 6 | return 0 7 | } 8 | mid, low, hight := 0, 0, lenNum-1 9 | for low < hight { 10 | mid = (low + hight) / 2 11 | if target < nums[mid] { 12 | hight = mid 13 | } else if target > nums[mid] { 14 | low = mid + 1 15 | } 16 | if target == nums[mid] { 17 | return mid 18 | } 19 | if target == nums[hight] { 20 | return hight 21 | } 22 | } 23 | if target < nums[low] { 24 | if low != 0 { 25 | return low 26 | } 27 | return 0 28 | } else if target > nums[hight] { 29 | return hight + 1 30 | } else { 31 | return low + 1 32 | } 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/) 4 | 5 | ### 解題思路 6 | 7 | 1. 相同的話回傳當前下標 8 | 2. 小於當前值就插入到當前的位置 9 | 3. 都不對的話就塞到最後一個 10 | 11 | ### 代碼 12 | 13 | ```go 14 | func searchInsert(nums []int, target int) int { 15 | for i, v := range nums { 16 | if (v == target) { 17 | return i 18 | } 19 | if (target < v) { 20 | return i 21 | } 22 | } 23 | 24 | return len(nums) 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func searchInsert(nums []int, target int) int { 3 | 4 | for k, v := range nums { 5 | 6 | if v >= target { 7 | return k 8 | } 9 | } 10 | 11 | return len(nums) 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/index.md: -------------------------------------------------------------------------------- 1 | # 题目 2 | 3 | Given a sorted array and a target value, 4 | 5 | return the index if the target is found. 6 | 7 | If not, return the index where it would be if it were inserted in order. 8 | 9 | You may assume no duplicates in the array. 10 | 11 | ## 例子 12 | 13 | ``` 14 | Input: [1,3,5,6], 5 15 | Output: 2 16 | ``` 17 | 18 | ``` 19 | Input: [1,3,5,6], 2 20 | Output: 1 21 | ``` 22 | 23 | ``` 24 | Input: [1,3,5,6], 7 25 | Output: 4 26 | ``` 27 | 28 | ``` 29 | Input: [1,3,5,6], 0 30 | Output: 0 31 | ``` 32 | 33 | 34 | 35 | 36 | 37 | 38 | [原题地址](https://leetcode.com/problems/search-insert-position/) -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/lhwang95.md: -------------------------------------------------------------------------------- 1 | 4ms,3.1Mb 2 | ``` 3 | func searchInsert(nums []int, target int) int { 4 | res, length := 0, len(nums) 5 | if length == 0 || target < nums[0] { // 如果切片为空或者所有元素都大于target,返回0 6 | return res 7 | } 8 | for ; res < length; res++ { 9 | if nums[res] >= target { 10 | return res 11 | } 12 | } 13 | return res 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/035-search_insert_position/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | 使用二分法查找相对应的元素 2 | 3 | ```go 4 | 5 | package main 6 | 7 | import "fmt" 8 | 9 | func main() { 10 | s := []int{1, 23} 11 | r := searchInsert(s, 12) 12 | fmt.Println(r) 13 | } 14 | 15 | func searchInsert(nums []int, target int) int { 16 | min, max := 0, len(nums)-1 17 | for min <= max { 18 | if min == max && target > nums[min] { 19 | return min + 1 20 | } 21 | 22 | mid := (min + max) / 2 23 | if nums[mid] == target { 24 | return mid 25 | } 26 | 27 | if nums[mid] > target { 28 | max = mid - 1 29 | } else { 30 | min = mid + 1 31 | } 32 | } 33 | return min 34 | } 35 | ``` -------------------------------------------------------------------------------- /algorithms/041-First Missing Positive/Bingjian-Zhu.md: -------------------------------------------------------------------------------- 1 | 桶排序 2 | ```golang 3 | func firstMissingPositive(nums []int) int { 4 | lenNum := len(nums) 5 | 6 | for i := 0; i < lenNum; i++ { 7 | for nums[i] > 0 && nums[i] <= lenNum && nums[nums[i]-1] != nums[i] { 8 | nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] 9 | } 10 | } 11 | for i := 0; i < lenNum; i++ { 12 | if nums[i] != i+1 { 13 | return i + 1 14 | } 15 | } 16 | return lenNum + 1 17 | } 18 | ``` -------------------------------------------------------------------------------- /algorithms/041-First Missing Positive/index.md: -------------------------------------------------------------------------------- 1 | Given an unsorted integer array, find the smallest missing positive integer. 2 | 3 | Example 1: 4 | 5 | Input: [1,2,0] 6 | Output: 3 7 | Example 2: 8 | 9 | Input: [3,4,-1,1] 10 | Output: 2 11 | Example 3: 12 | 13 | Input: [7,8,9,11,12] 14 | Output: 1 15 | 16 | 17 | [原文链接](https://leetcode-cn.com/problems/first-missing-positive) -------------------------------------------------------------------------------- /algorithms/043-Multiply Strings/index.md: -------------------------------------------------------------------------------- 1 | Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. 2 | 3 | Example 1: 4 | 5 | Input: num1 = "2", num2 = "3" 6 | Output: "6" 7 | Example 2: 8 | 9 | Input: num1 = "123", num2 = "456" 10 | Output: "56088" 11 | Note: 12 | 13 | The length of both num1 and num2 is < 110. 14 | Both num1 and num2 contain only digits 0-9. 15 | Both num1 and num2 do not contain any leading zero, except the number 0 itself. 16 | You must not use any built-in BigInteger library or convert the inputs to integer directly. 17 | 18 | 19 | [原文链接](https://leetcode-cn.com/problems/multiply-strings) 20 | -------------------------------------------------------------------------------- /algorithms/045-Jump Game II/index.md: -------------------------------------------------------------------------------- 1 | Given an array of non-negative integers, you are initially positioned at the first index of the array. 2 | 3 | Each element in the array represents your maximum jump length at that position. 4 | 5 | Your goal is to reach the last index in the minimum number of jumps. 6 | 7 | Example: 8 | 9 | Input: [2,3,1,1,4] 10 | Output: 2 11 | Explanation: The minimum number of jumps to reach the last index is 2. 12 | Jump 1 step from index 0 to 1, then 3 steps to the last index. 13 | Note: 14 | 15 | You can assume that you can always reach the last index 16 | 17 | 18 | [原文链接](https://leetcode-cn.com/problems/jump-game-ii) 19 | -------------------------------------------------------------------------------- /algorithms/046-permutations/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func permute(nums []int) [][]int { 3 | 4 | if len(nums) == 0 { 5 | return [][]int{} 6 | } 7 | 8 | visited := make([]bool, len(nums)) 9 | res := [][]int{} 10 | 11 | backTrack(nums, []int{}, visited, &res) 12 | 13 | return res 14 | } 15 | 16 | func backTrack(nums, sub []int, visited []bool, res *[][]int) { 17 | if len(sub) == len(nums) { 18 | tmp := make([]int, len(sub)) 19 | copy(tmp, sub) 20 | *res = append(*res, tmp) 21 | return 22 | } 23 | 24 | for i, num := range nums { 25 | if visited[i] { 26 | continue 27 | } 28 | visited[i] = true 29 | sub = append(sub, num) 30 | backTrack(nums, sub, visited, res) 31 | visited[i] = false 32 | sub = sub[:len(sub)-1] 33 | } 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /algorithms/046-permutations/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/046-permutations/index.md -------------------------------------------------------------------------------- /algorithms/046-permutations/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 46 permutations 2 | 3 | ## 解法 4 | 5 | - 逐个插入数字来构造所有permutations 6 | - 在使用 golang slice 时,尽量预分配适合的cap,减少内存分配频率 7 | 8 | ```golang 9 | func permute(nums []int) [][]int { 10 | var ret [][]int 11 | if len(nums) == 0 { 12 | return ret 13 | } 14 | ret = make([][]int, 1, len(nums)) 15 | ret[0] = make([]int, 1, len(nums)) 16 | ret[0][0] = nums[0] 17 | for i := 1; i < len(nums); i++ { 18 | rows_num := len(ret) 19 | for j := 0; j < rows_num; j++ { 20 | // insert, 0~n-1 21 | for k := 0; k < len(ret[j]); k++ { 22 | item := make([]int, len(ret[j])+1, len(nums)) 23 | copy(item[:k], ret[j][:k]) 24 | copy(item[k+1:], ret[j][k:]) 25 | item[k] = nums[i] 26 | ret = append(ret, item) 27 | } 28 | // push back, n,重复利用原来的数组 29 | ret[j] = append(ret[j], nums[i]) 30 | } 31 | } 32 | return ret 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/047-permutations-ii/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func permuteUnique(nums []int) [][]int { 3 | 4 | if len(nums) == 0 { 5 | return [][]int{} 6 | } 7 | 8 | sort.Ints(nums) 9 | 10 | visited := make([]bool, len(nums)) 11 | res := [][]int{} 12 | 13 | backTrack(visited, []int{}, nums, &res) 14 | 15 | return res 16 | } 17 | 18 | func backTrack(visited []bool, sub, nums []int, res *[][]int) { 19 | if len(sub) == len(nums) { 20 | *res = append(*res, append([]int{}, sub...)) 21 | return 22 | } 23 | 24 | for i, num := range nums { 25 | if visited[i] || (i != 0 && nums[i] == nums[i-1] && !visited[i-1]) { 26 | continue 27 | } 28 | 29 | visited[i] = true 30 | sub = append(sub, num) 31 | backTrack(visited, sub, nums, res) 32 | visited[i] = false 33 | sub = sub[:len(sub)-1] 34 | } 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/047-permutations-ii/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/047-permutations-ii/index.md -------------------------------------------------------------------------------- /algorithms/049-Group Anagrams/index.md: -------------------------------------------------------------------------------- 1 | Given an array of strings, group anagrams together. 2 | 3 | Example: 4 | 5 | Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 6 | Output: 7 | [ 8 | ["ate","eat","tea"], 9 | ["nat","tan"], 10 | ["bat"] 11 | ] 12 | Note: 13 | 14 | All inputs will be in lowercase. 15 | The order of your output does not matter. 16 | 17 | [原文链接](https://leetcode-cn.com/problems/group-anagrams) 18 | -------------------------------------------------------------------------------- /algorithms/053-maximum_subarray/LIYINGZHE.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) 4 | 5 | ### 解題思路 6 | 7 | 如果現在的值 (cur) 是負數,就直接拋棄,選用最新的值。 8 | 如果現在的值 (cur) 是正數,就加上最新的值。 9 | 最後都與 max 比較。 10 | 11 | ### 代碼 12 | 13 | ```go 14 | func maxSubArray(nums []int) int { 15 | cur, max := nums[0], nums[0] 16 | 17 | for _, v := range(nums[1:]) { 18 | if cur < 0 { 19 | cur = v 20 | } else { 21 | cur += v 22 | } 23 | if cur > max { 24 | max = cur 25 | } 26 | } 27 | return max 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /algorithms/053-maximum_subarray/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func maxSubArray(nums []int) int { 3 | 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | res, tmp := nums[0], 0 9 | 10 | for _, v := range nums { 11 | if tmp < 0 { 12 | tmp = 0 13 | } 14 | tmp += v 15 | 16 | if res < tmp { 17 | res = tmp 18 | } 19 | } 20 | 21 | return res 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/053-maximum_subarray/index.md: -------------------------------------------------------------------------------- 1 | # 题目 2 | 3 | Given an integer array `nums`, 4 | 5 | find the contiguous subarray (containing at least one number) 6 | 7 | which has the largest sum and return its sum. 8 | 9 | ## 例子 10 | 11 | ``` 12 | Input: [-2,1,-3,4,-1,2,1,-5,4], 13 | Output: 6 14 | Explanation: [4,-1,2,1] has the largest sum = 6. 15 | ``` 16 | 17 | ## 说明 18 | 19 | If you have figured out the O(n) solution, 20 | 21 | try coding another solution using the divide and conquer approach, 22 | 23 | which is more subtle. 24 | 25 | 26 | [原题链接](https://leetcode.com/problems/maximum-subarray/) -------------------------------------------------------------------------------- /algorithms/053-maximum_subarray/lhwang95.md: -------------------------------------------------------------------------------- 1 | 4ms, 3.5mb 2 | 动态规划法,以i为结尾的子串最大值为 3 | f(i)=nums(i), if i = 0 or f(i-1)<=0 4 | f(i)=nums(i)+f(i-1), if i != 0 and f(i-1)>0 5 | 然后求以各个索引为结尾子串的最大值切片的最大值就是子串最大值 6 | 7 | ``` 8 | func maxSubArray(nums []int) int { 9 | res := ^int(^uint(0)>>1) 10 | length := len(nums) 11 | sum := make([]int, length) 12 | for i := 0; i < length; i++ { 13 | if i == 0 || sum[i-1] <= 0 { 14 | sum[i] = nums[i] 15 | } 16 | if i != 0 && sum[i-1] > 0 { 17 | sum[i] = nums[i] + sum[i-1] 18 | } 19 | } 20 | for _, v := range sum { 21 | if v > res { 22 | res = v 23 | } 24 | } 25 | return res 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/055-Jump Game/index.md: -------------------------------------------------------------------------------- 1 | Given an array of non-negative integers, you are initially positioned at the first index of the array. 2 | 3 | Each element in the array represents your maximum jump length at that position. 4 | 5 | Determine if you are able to reach the last index. 6 | 7 | Example 1: 8 | 9 | Input: [2,3,1,1,4] 10 | Output: true 11 | Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. 12 | Example 2: 13 | 14 | Input: [3,2,1,0,4] 15 | Output: false 16 | Explanation: You will always arrive at index 3 no matter what. Its maximum 17 |   jump length is 0, which makes it impossible to reach the last index. 18 | 19 | [原文链接](https://leetcode-cn.com/problems/jump-game) -------------------------------------------------------------------------------- /algorithms/062-unique-paths/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func uniquePaths(m int, n int) int { 3 | 4 | if m <= 0 || n <= 0 { 5 | return 0 6 | } 7 | 8 | res := make([][]int, m+1) 9 | for i := range res { 10 | res[i] = make([]int, n+1) 11 | } 12 | res[0][1] = 1 13 | for i := 1; i < m+1; i++ { 14 | for j := 1; j < n+1; j++ { 15 | res[i][j] = res[i-1][j] + res[i][j-1] 16 | } 17 | } 18 | 19 | return res[m][n] 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/062-unique-paths/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/062-unique-paths/index.md -------------------------------------------------------------------------------- /algorithms/062-unique-paths/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 唯一路径 2 | 3 | ## 解法 4 | 5 | 动态规划的思路,状态转移方程 `dp[m][n] = dp[m-1][n] + dp[m][n-1]`,初始化值 `dp[m][n] = 0 (m==0 or n==0)` 6 | 7 | ```golang 8 | func uniquePaths(m int, n int) int { 9 | if m == 0 || n == 0 { 10 | return 0 11 | } 12 | if m == 1 || n == 1 { 13 | return 1 14 | } 15 | dp := make([][]int, m) 16 | for i := 0; i < m; i++ { 17 | dp[i] = make([]int, n) 18 | } 19 | for i := 0; i < m; i++ { 20 | for j := 0; j < n; j++ { 21 | if i == 0 || j == 0 { 22 | dp[i][j] = 1 23 | } else { 24 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 25 | } 26 | } 27 | } 28 | return dp[m-1][n-1] 29 | } 30 | ``` -------------------------------------------------------------------------------- /algorithms/064-minimum-path-sum/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func minPathSum(grid [][]int) int { 3 | 4 | if len(grid) == 0 || len(grid[0]) == 0 { 5 | return 0 6 | } 7 | 8 | maxInt := 1 << 63 - 1 9 | minSum := make([][]int, len(grid)+1) 10 | for i := range minSum { 11 | minSum[i] = make([]int, len(grid[0])+1) 12 | if i == 0 { 13 | for j := range minSum[i] { 14 | minSum[i][j] = maxInt 15 | } 16 | } 17 | minSum[i][0] = maxInt 18 | } 19 | minSum[0][1] = 0 20 | 21 | for i := range grid { 22 | for j := range grid[i] { 23 | minSum[i+1][j+1] = min(minSum[i+1][j], minSum[i][j+1]) + grid[i][j] 24 | } 25 | } 26 | 27 | return minSum[len(grid)][len(grid[0])] 28 | } 29 | 30 | func min(a, b int) int { 31 | if a > b { 32 | return b 33 | } 34 | 35 | return a 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /algorithms/064-minimum-path-sum/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/064-minimum-path-sum/index.md -------------------------------------------------------------------------------- /algorithms/066-plusOne/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [66. Plus One](https://leetcode.com/problems/plus-one/) 4 | 5 | ### 解題思路 6 | 7 | 這題跟 2. Add Two Numbers 有點像。 8 | 由右邊算到左邊,記得處理 carry 還有值的情況。 9 | 10 | ### 代碼 11 | 12 | ```go 13 | func plusOne(digits []int) []int { 14 | length := len(digits) - 1 15 | right := length 16 | carry := 0 17 | 18 | for right >= 0 || carry != 0 { 19 | sum := 0 20 | 21 | // 頭進位 1 22 | if (right < 0 && carry != 0) { 23 | return append([]int{1}, digits...) 24 | } 25 | 26 | if (right == length) { 27 | sum = digits[right] + 1 28 | } else { 29 | sum = digits[right] + carry 30 | } 31 | 32 | digits[right] = sum % 10 33 | carry = sum / 10 34 | right-- 35 | } 36 | 37 | return digits 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /algorithms/066-plusOne/RudeFish.md: -------------------------------------------------------------------------------- 1 | ``` 2 | func plusOne(digits []int) []int { 3 | // 如果为空直接返回 4 | if digits == nil || len(digits) == 0 { 5 | return nil 6 | } 7 | for i := len(digits) - 1; i >= 0; i-- { 8 | // 最后一个<9直接返回 9 | if digits[i] < 9 { 10 | digits[i] = digits[i] + 1 11 | return digits 12 | } 13 | 14 | for digits[i] == 9 || digits[i] == 10 { 15 | // 加到index为0,在前面加一位 16 | if (i == 0 && digits[i] == 10) || (i == 0 && digits[i] == 9) { 17 | digits[i] = 0 18 | digits = append([]int{1}, digits[0:]...) 19 | return digits 20 | } 21 | // 当前数为9或10向前进一 22 | if digits[i] == 10 || digits[i] == 9 { 23 | digits[i] = 0 24 | digits[i - 1] = digits[i - 1] + 1 25 | //如果前面一个数<=9直接返回 26 | if digits[i - 1] <= 9 { 27 | return digits 28 | } 29 | } 30 | } 31 | } 32 | return digits 33 | } 34 | -------------------------------------------------------------------------------- /algorithms/066-plusOne/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func plusOne(digits []int) []int { 3 | 4 | carry := 1 5 | res := make([]int, len(digits)+1) 6 | 7 | for i := len(digits)-1; i >= 0; i-- { 8 | tmp := digits[i] + carry 9 | carry = tmp / 10 10 | res[i+1] = tmp % 10 11 | } 12 | 13 | if carry == 1 { 14 | res[0] = 1 15 | } else { 16 | res = res[1:] 17 | } 18 | 19 | return res 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/066-plusOne/index.md: -------------------------------------------------------------------------------- 1 | # 题目 2 | Given a non-empty array of digits representing a non-negative integer, plus one to the integer. 3 | 4 | The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. 5 | 6 | You may assume the integer does not contain any leading zero, except the number 0 itself. 7 | 8 | ## 例子 9 | 10 | ``` 11 | Input: [1,2,3] 12 | Output: [1,2,4] 13 | Explanation: The array represents the integer 123. 14 | ``` 15 | 16 | ``` 17 | Input: [4,3,2,1] 18 | Output: [4,3,2,2] 19 | Explanation: The array represents the integer 4321. 20 | ``` 21 | 22 | [原地地址](https://leetcode.com/problems/plus-one/) -------------------------------------------------------------------------------- /algorithms/066-plusOne/lhwang95.md: -------------------------------------------------------------------------------- 1 | 注意陷阱,加一可能会进位 2 | 3 | ``` 4 | func plusOne(digits []int) []int { 5 | cin := 1 // 设置进位标志 6 | length := len(digits) 7 | res := make([]int, length+1) 8 | for i := length-1; i >= 0; i-- { 9 | res[i+1] = digits[i] + cin 10 | if res[i+1] >= 10 { 11 | res[i+1] -= 10 12 | cin = 1 13 | } else { 14 | cin = 0 15 | } 16 | } 17 | if cin == 1 { 18 | res[0] = 1 19 | } else { 20 | res = res[1:] 21 | } 22 | return res 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /algorithms/066-plusOne/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | 2 | 直接遍历切片,如果满足要求则进位,否则直接跳出执行, 3 | 4 | ```go 5 | func plusOne(digits []int) []int { 6 | l := len(digits) - 1 7 | 8 | for i := l; i >= 0; i-- { 9 | if digits[i] == 9 { 10 | digits[i] = 0 11 | } else { 12 | digits[i]++ 13 | break 14 | } 15 | } 16 | 17 | if digits[0] > 0 { 18 | return digits 19 | } 20 | 21 | return append([]int{1}, digits...) 22 | } 23 | ``` 24 | 25 | 扩展: 添加切片元素到特定的 -------------------------------------------------------------------------------- /algorithms/069-sqrtx/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [69. Sqrt(x)](https://leetcode.com/problems/sqrtx/) 4 | 5 | ### 解題思路 6 | 7 | 簡單的二分法 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func mySqrt(x int) int { 13 | l, r := 0, x 14 | 15 | for l <= r { 16 | m := l + (r-l) / 2 17 | if (m*m < x) { 18 | l = m+1 19 | } else if (m*m > x) { 20 | r = m-1 21 | } else { 22 | return m 23 | } 24 | } 25 | 26 | return r 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/069-sqrtx/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func mySqrt(x int) int { 3 | 4 | left, right := 1, x 5 | 6 | for left <= right { 7 | mid := left + (right-left) >> 1 8 | if mid > x / mid { 9 | right = mid - 1 10 | } else { 11 | left = mid + 1 12 | } 13 | } 14 | 15 | return right 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/069-sqrtx/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/069-sqrtx/index.md -------------------------------------------------------------------------------- /algorithms/069-sqrtx/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # x 的平方根 2 | 3 | ## 解法 4 | 5 | 用二分法来猜测平均数的平方是否接近x,因为输出只有整数部分,所以最终结果偏向左边界 6 | 7 | ```golang 8 | func mySqrt(x int) int { 9 | if x == 0 || x == 1 { 10 | return x 11 | } 12 | l, r, t := 1, x, 0 13 | m := (l + r) / 2 14 | for l != m { 15 | t = m * m 16 | if t == x { 17 | return m 18 | } 19 | if t < x { 20 | l = m 21 | } else { 22 | r = m 23 | } 24 | m = (l + r) / 2 25 | } 26 | return m 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/070-climbing-stairs/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ## Question 2 | 3 | https://leetcode.com/problems/0070-climbing-stairs/ 4 | 5 | ## Solutions 6 | 7 | ```go 8 | // Time: O(n), Space: O(n) 9 | func climbStairs(n int) int { 10 | d := make([]int, n+1) 11 | d[0] = 1 12 | d[1] = 1 13 | 14 | for i := 2; i <= n; i++ { 15 | d[i] = d[i-1] + d[i-2] 16 | } 17 | 18 | return d[n] 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/070-climbing-stairs/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func climbStairs(n int) int { 3 | 4 | if n <= 2 { 5 | return n 6 | } 7 | 8 | pre, res := 1, 2 9 | for i := 3; i <= n; i++ { 10 | pre, res = res, pre + res 11 | } 12 | 13 | return res 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/070-climbing-stairs/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/070-climbing-stairs/index.md -------------------------------------------------------------------------------- /algorithms/070-climbing-stairs/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 70.爬楼梯 2 | 3 | 实际上是斐波那契数列 4 | 5 | ```golang 6 | func climbStairs(n int) int { 7 | if n < 3 { 8 | return n 9 | } 10 | x, y := 1, 2 11 | for i := 2; i < n; i++ { 12 | x, y = y, x+y 13 | } 14 | return y 15 | } 16 | ``` -------------------------------------------------------------------------------- /algorithms/075-sort-colors/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func sortColors(nums []int) { 3 | 4 | l, r, cur := 0, len(nums)-1, 0 5 | 6 | for cur <= r { 7 | switch nums[cur] { 8 | case 0: 9 | nums[l], nums[cur] = nums[cur], nums[l] 10 | l++ 11 | cur++ 12 | 13 | case 2: 14 | nums[r], nums[cur] = nums[cur], nums[r] 15 | r-- 16 | 17 | default: 18 | cur++ 19 | } 20 | 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/075-sort-colors/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/075-sort-colors/index.md -------------------------------------------------------------------------------- /algorithms/075-sort-colors/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 0 ms, faster than 100.00% of Go online submissions for Sort Colors. 2 | 3 | Memory Usage: 2.3 MB, less than 65.52% of Go online submissions for Sort Colors. 4 | ``` 5 | func sortColors(nums []int) { 6 | color := 0 7 | for i:=0; i= 0 { 20 | if (array1Target >= 0 && nums1[array1Target] > nums2[array2Target]) { 21 | nums1[replaceIndex] = nums1[array1Target] 22 | array1Target-- 23 | } else { 24 | nums1[replaceIndex] = nums2[array2Target] 25 | array2Target-- 26 | } 27 | replaceIndex-- 28 | } 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /algorithms/088-merge_sorted_array/RudeFish.md: -------------------------------------------------------------------------------- 1 | ``` 2 | func merge(nums1 []int, m int, nums2 []int, n int) { 3 | nums1 = append(nums1[:m] , nums2[:]...) 4 | sort.Ints(nums1) 5 | } 6 | -------------------------------------------------------------------------------- /algorithms/088-merge_sorted_array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func merge(nums1 []int, m int, nums2 []int, n int) { 3 | 4 | nums1 = append(nums1[:m], nums2...) 5 | sort.Ints(nums1) 6 | } 7 | ``` 8 | -------------------------------------------------------------------------------- /algorithms/088-merge_sorted_array/index.md: -------------------------------------------------------------------------------- 1 | # 题目 2 | 3 | Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 4 | 5 | 6 | ## 注意 7 | 8 | - The number of elements initialized in nums1 and nums2 are m and n respectively. 9 | 10 | - You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. 11 | 12 | ## 例子 13 | 14 | ``` 15 | Input: 16 | nums1 = [1,2,3,0,0,0], m = 3 17 | nums2 = [2,5,6], n = 3 18 | 19 | Output: [1,2,2,3,5,6] 20 | ``` 21 | 22 | [原题](https://leetcode.com/problems/merge-sorted-array/) 23 | 24 | 25 | -------------------------------------------------------------------------------- /algorithms/088-merge_sorted_array/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func merge(nums1 []int, m int, nums2 []int, n int) { 3 | nums1 = append(nums1[0:m], nums2[0:n]...) 4 | sort.Ints(nums1) 5 | } 6 | ``` -------------------------------------------------------------------------------- /algorithms/091-decode-ways/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [91. Decode Ways](https://leetcode.com/problems/decode-ways/) 4 | 5 | ### 解題思路 6 | 7 | Dynamic Programming 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func isValidTwoDigit(a, b byte) bool { 13 | return (a == '1' && b <= '9') || (a == '2' && b <= '6') 14 | } 15 | 16 | func numDecodings(s string) int { 17 | n := len(s) 18 | d := make([]int, n+1) 19 | 20 | d[0] = 1 21 | if s[0] != '0' { 22 | d[1] = 1 23 | } else { 24 | d[1] = 0 25 | } 26 | 27 | for i := 2; i <= n; i++ { 28 | if s[i-1] != '0' { 29 | d[i] += d[i-1] 30 | } 31 | if isValidTwoDigit(s[i-2], s[i-1]) { 32 | d[i] += d[i-2] 33 | } 34 | } 35 | 36 | return d[n] 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /algorithms/091-decode-ways/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func numDecodings(s string) int { 3 | 4 | if len(s) == 0 || s[0] == '0' { 5 | return 0 6 | } 7 | 8 | pre, res := 1, 1 9 | for i := 1; i < len(s); i++ { 10 | tmp := 0 11 | if s[i] != '0' { 12 | tmp += res 13 | } 14 | if s[i-1] == '1' || (s[i-1] == '2' && s[i] <= '6') { 15 | tmp += pre 16 | } 17 | pre, res = res, tmp 18 | } 19 | 20 | return res 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/091-decode-ways/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/091-decode-ways/index.md -------------------------------------------------------------------------------- /algorithms/093-restore-ip-addresses/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/093-restore-ip-addresses/index.md -------------------------------------------------------------------------------- /algorithms/093-word-search/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/093-word-search/index.md -------------------------------------------------------------------------------- /algorithms/095-unique-binary-search-trees-ii/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/095-unique-binary-search-trees-ii/index.md -------------------------------------------------------------------------------- /algorithms/118-pascals_triangle/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) 4 | 5 | ### 解題思路 6 | 7 | 1. 最左與最右邊都是 1 8 | 2. 剩下的值是左上 ([row-1][column-1]) 跟右上 ([row-1][column]) 的和 9 | 10 | ### 代碼 11 | 12 | ```go 13 | func generate(numRows int) [][]int { 14 | // 處理極端情況 15 | if numRows < 1 { 16 | return [][]int{} 17 | } 18 | 19 | result := make([][]int, numRows) 20 | // 第一行一定是 [1] 21 | result[0] = []int{1} 22 | 23 | for row := 1; row < numRows; row++ { 24 | // 建立空間 25 | result[row] = make([]int, row+1) 26 | 27 | for column := 0; column <= row; column++ { 28 | // 最左,最右邊都是 1 29 | if column == 0 || column == row { 30 | result[row][column] = 1 31 | } else { 32 | // 剩下的值是 左上跟右上的和 33 | result[row][column] = result[row-1][column-1] + result[row-1][column] 34 | } 35 | } 36 | } 37 | 38 | return result 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /algorithms/118-pascals_triangle/RudeFish.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func generate(numRows int) [][]int { 3 | if numRows == 0 { 4 | return nil 5 | } 6 | if numRows == 1 { 7 | fmt.Println([][]int{{1}}) 8 | return [][]int{{1}} 9 | } 10 | rut := [][]int{} 11 | for i := 0; i < numRows; i++ { 12 | a := []int{} 13 | for j := 0; j <= i; j++ { 14 | if j == 0 || j == i { 15 | //a[j] = 1 16 | a = append(a, 1) 17 | }else { 18 | //a[j] = rut[i][j-1] + rut[i][j] 19 | a = append(a, rut[i-1][j-1] + rut[i-1][j]) 20 | } 21 | } 22 | rut = append(rut, [][]int{a}...) 23 | } 24 | fmt.Println(rut) 25 | return rut 26 | } 27 | -------------------------------------------------------------------------------- /algorithms/118-pascals_triangle/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func generate(numRows int) [][]int { 3 | 4 | res := make([][]int, numRows) 5 | 6 | for i := range res { 7 | res[i] = make([]int, i+1) 8 | 9 | for j := range res[i] { 10 | if j == 0 || j == i { 11 | res[i][j] = 1 12 | } else { 13 | res[i][j] = res[i-1][j-1] + res[i-1][j] 14 | } 15 | } 16 | } 17 | 18 | return res 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/118-pascals_triangle/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 0 ms, faster than 100.00% of Go online submissions for Pascal's Triangle. 2 | 3 | Memory Usage: 2.3 MB, less than 50.97% of Go online submissions for Pascal's Triangle 4 | 5 | ``` 6 | func generate(numRows int) [][]int { 7 | res := make([][]int, 0) // 声明空返回值 8 | for i := 1; i <= numRows; i++ { 9 | temp := make([]int, i) // 声明每一行 10 | temp[0], temp[i-1] = 1, 1 // 每行首尾都是1 11 | if i >= 3 { 12 | for j := 0; j < len(res[i-2]) - 1; j++ { 13 | temp[j+1] = res[i-2][j] + res[i-2][j+1] // 每行第二个值起都等于前一行相邻加和 14 | } 15 | } 16 | res = append(res, temp) 17 | } 18 | return res 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/118-pascals_triangle/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func generate(numRows int) [][]int { 3 | 4 | s := make([][]int, numRows) 5 | for i := 1; i <= numRows; i++ { 6 | if i == 1 { 7 | s[i-1] = []int{1} 8 | continue 9 | } 10 | 11 | if i == 2 { 12 | s[i-1] = []int{1, 1} 13 | continue 14 | } 15 | s[i-1] = make([]int, i) 16 | copy(s[i-1], s[i-2]) 17 | for j := 0; j < (i - 2); j++ { 18 | s[i-1][j+1] = s[i-2][j] + s[i-2][j+1] 19 | } 20 | s[i-1][i-1] = 1 21 | } 22 | return s 23 | } 24 | ``` -------------------------------------------------------------------------------- /algorithms/119-pascals_triangle_ii/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [119. Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) 4 | 5 | ### 解題思路 6 | 7 | 跟 [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) 解題思路一樣。 8 | 注意下標越界問題即可。 9 | 10 | ### 代碼 11 | 12 | ```go 13 | func getRow(rowIndex int) []int { 14 | result := make([][]int, rowIndex+1) 15 | result[0] = []int{1} 16 | 17 | for row := 1; row <= rowIndex; row++ { 18 | result[row] = make([]int, row+1) 19 | for column := 0; column <= row; column++ { 20 | if column == 0 || column == row { 21 | result[row][column] = 1 22 | } else { 23 | result[row][column] = result[row-1][column-1] + result[row-1][column] 24 | } 25 | } 26 | } 27 | 28 | return result[rowIndex] 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /algorithms/119-pascals_triangle_ii/RudeFish.md: -------------------------------------------------------------------------------- 1 | ```go 2 | rut := [][]int{} 3 | for i := 0; i < rowIndex+1; i++ { 4 | a := []int{} 5 | for j := 0; j <= i; j++ { 6 | if j == 0 || j == i { 7 | //a[j] = 1 8 | a = append(a, 1) 9 | }else { 10 | //a[j] = rut[i][j-1] + rut[i][j] 11 | a = append(a, rut[i-1][j-1] + rut[i-1][j]) 12 | } 13 | } 14 | rut = append(rut, [][]int{a}...) 15 | } 16 | //fmt.Println(rut) 17 | return rut[rowIndex] 18 | -------------------------------------------------------------------------------- /algorithms/119-pascals_triangle_ii/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func getRow(rowIndex int) []int { 3 | 4 | res := make([]int, rowIndex+1) 5 | 6 | for i := 0; i <= rowIndex; i++ { 7 | for j := i; j >= 0; j-- { 8 | if j == 0 || j == i { 9 | res[j] = 1 10 | } else { 11 | res[j] = res[j-1] + res[j] 12 | } 13 | } 14 | } 15 | 16 | return res 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/119-pascals_triangle_ii/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 0 ms, faster than 100.00% of Go online submissions for Pascal's Triangle II. 2 | 3 | Memory Usage: 2.1 MB, less than 30.60% of Go online submissions for Pascal's Triangle II 4 | 5 | 想用数学公式算,但是溢出了 6 | 7 | ``` 8 | func getRow(rowIndex int) []int { 9 | res := make([][]int, 0) 10 | for i := 1; i <= rowIndex+1; i++ { 11 | temp := make([]int, i) 12 | temp[0], temp[i-1] = 1, 1 13 | if i >= 3 { 14 | for j := 0; j < len(res[i-2]) - 1; j++ { 15 | temp[j+1] = res[i-2][j] + res[i-2][j+1] 16 | } 17 | } 18 | res = append(res, temp) 19 | } 20 | return res[rowIndex] 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/119-pascals_triangle_ii/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func getRow(rowIndex int) []int { 3 | rowIndex++ 4 | s := make([][]int, rowIndex) 5 | for i := 1; i <= rowIndex; i++ { 6 | if i == 1 { 7 | s[i-1] = []int{1} 8 | continue 9 | } 10 | 11 | if i == 2 { 12 | s[i-1] = []int{1, 1} 13 | continue 14 | } 15 | s[i-1] = make([]int, i) 16 | copy(s[i-1], s[i-2]) 17 | for j := 0; j < (i - 2); j++ { 18 | s[i-1][j+1] = s[i-2][j] + s[i-2][j+1] 19 | } 20 | s[i-1][i-1] = 1 21 | } 22 | return s[rowIndex-1] 23 | 24 | } 25 | ``` -------------------------------------------------------------------------------- /algorithms/121-best-time-to-buy-and-sell-stock/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) 4 | 5 | ### 解題思路 6 | 7 | 出現低價就買 8 | 高價就賣 9 | 比較最大值即可 10 | 11 | ### 代碼 12 | 13 | ```go 14 | func maxProfit(prices []int) int { 15 | if len(prices) == 0 { 16 | return 0 17 | } 18 | 19 | max := 0 20 | buyPrice := prices[0] 21 | 22 | for _, price := range prices[1:] { 23 | if price < buyPrice { 24 | buyPrice = price 25 | } else { 26 | earn := price - buyPrice 27 | if earn > max { 28 | max = earn 29 | } 30 | } 31 | } 32 | 33 | return max 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /algorithms/121-best-time-to-buy-and-sell-stock/RudeFish.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func maxProfit(prices []int) int { 3 | tmp := 0 4 | for i := 0; i < len(prices)-1; i++ { 5 | if prices[i] > prices[0]{ 6 | continue 7 | } 8 | for j := i+1; j < len(prices); j++ { 9 | //fmt.Println(prices[j], prices[i] , "\n") 10 | if prices[j] < prices[i] { 11 | continue 12 | } 13 | tmp2 := prices[j]- prices[i] 14 | //fmt.Println(tmp2, "\n") 15 | if tmp2 > tmp { 16 | tmp = tmp2 17 | } 18 | } 19 | } 20 | return tmp 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/121-best-time-to-buy-and-sell-stock/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func maxProfit(prices []int) int { 3 | 4 | tmp, res := 0, 0 5 | 6 | for i := 1; i < len(prices); i++ { 7 | tmp += prices[i] - prices[i-1] 8 | if tmp < 0 { 9 | tmp = 0 10 | } 11 | if res < tmp { 12 | res = tmp 13 | } 14 | } 15 | 16 | return res 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/121-best-time-to-buy-and-sell-stock/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/121-best-time-to-buy-and-sell-stock/index.md -------------------------------------------------------------------------------- /algorithms/121-best-time-to-buy-and-sell-stock/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 4 ms, faster than 98.86% of Go online submissions for Best Time to Buy and Sell Stock. 2 | 3 | Memory Usage: 3.1 MB, less than 65.47% of Go online submissions for Best Time to Buy and Sell Stock. 4 | 5 | ``` 6 | func maxProfit(prices []int) int { 7 | b := int(^uint(0)>>1) // 买入初值设为最大 8 | s := ^int(^uint(0)>>1) // 卖出初值设为最小 9 | for _, v := range prices { 10 | if b > v { 11 | b = v // 找到最小购入值 12 | } 13 | if s < v - b { 14 | s = v - b // 找到卖出的最大收益 15 | } 16 | } 17 | if s > 0 { 18 | return s // 如果收益大于0,则购买了 19 | } else { 20 | return 0 // 其他情况没有购买,收益为0 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/122-best-time-to-buy-and-sell-stock-ii/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) 4 | 5 | ### 解題思路 6 | 7 | 這題很容易想得很複雜,原本以為要用動態規劃。 8 | 9 | 看了解答,比想象中的簡單,只要今天比昨天高就賣。 10 | 11 | 連續的在波峰波谷賣出比差值最大的波谷還賺的多。 12 | 13 | ![WechatIMG48](https://user-images.githubusercontent.com/11765228/59348805-aa8d6400-8d4a-11e9-9bf0-dc1d4fdeaf37.jpeg) 14 | 15 | 16 | ### 代碼 17 | 18 | ```go 19 | func maxProfit(prices []int) int { 20 | if len(prices) == 0 { 21 | return 0 22 | } 23 | 24 | profit := 0 25 | yesterdayPrice := prices[0] 26 | 27 | for _, todayPrice := range prices[1:] { 28 | if todayPrice > yesterdayPrice { 29 | earn := todayPrice - yesterdayPrice 30 | profit += earn 31 | } 32 | yesterdayPrice = todayPrice 33 | } 34 | 35 | return profit 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /algorithms/122-best-time-to-buy-and-sell-stock-ii/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func maxProfit(prices []int) int { 3 | 4 | res := 0 5 | 6 | for i := 1; i < len(prices); i++ { 7 | tmp := prices[i] - prices[i-1] 8 | if tmp > 0 { 9 | res += tmp 10 | } 11 | } 12 | 13 | return res 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/122-best-time-to-buy-and-sell-stock-ii/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/122-best-time-to-buy-and-sell-stock-ii/index.md -------------------------------------------------------------------------------- /algorithms/127-word-ladder/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/127-word-ladder/index.md -------------------------------------------------------------------------------- /algorithms/130-surrounded-regions/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/130-surrounded-regions/index.md -------------------------------------------------------------------------------- /algorithms/141-linked-list-cycle/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/) 4 | 5 | ### 解題思路 6 | 7 | 利用快慢指針,快指針走兩步、慢指針走一步 8 | 9 | ### 代碼 10 | 11 | ```go 12 | /** 13 | * Definition for singly-linked list. 14 | * type ListNode struct { 15 | * Val int 16 | * Next *ListNode 17 | * } 18 | */ 19 | func hasCycle(head *ListNode) bool { 20 | fast, slow := head, head 21 | 22 | for fast != nil { 23 | if fast.Next == nil { 24 | return false 25 | } 26 | fast = fast.Next.Next 27 | slow = slow.Next 28 | if fast == slow { 29 | return true 30 | } 31 | } 32 | return false 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /algorithms/141-linked-list-cycle/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | /** 3 | * Definition for singly-linked list. 4 | * type ListNode struct { 5 | * Val int 6 | * Next *ListNode 7 | * } 8 | */ 9 | func hasCycle(head *ListNode) bool { 10 | 11 | if head != nil { 12 | fast, slow := head, head 13 | 14 | for fast.Next != nil && fast.Next.Next != nil { 15 | fast = fast.Next.Next 16 | slow = slow.Next 17 | if slow == fast { 18 | return true 19 | } 20 | } 21 | } 22 | 23 | return false 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/141-linked-list-cycle/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/141-linked-list-cycle/index.md -------------------------------------------------------------------------------- /algorithms/141-linked-list-cycle/whao246.md: -------------------------------------------------------------------------------- 1 | ### 141. 环形链表 2 | 3 | ### 代码 4 | ``` 5 | /** 6 | * Definition for singly-linked list. 7 | * type ListNode struct { 8 | * Val int 9 | * Next *ListNode 10 | * } 11 | */ 12 | func hasCycle(head *ListNode) bool { 13 | if head==nil||head.Next==nil{ 14 | return false 15 | } 16 | fast:=head.Next 17 | slow:=head 18 | for fast!=slow{ 19 | if fast==nil||fast.Next==nil{ 20 | return false 21 | } 22 | fast=fast.Next.Next 23 | slow=slow.Next 24 | 25 | } 26 | return true 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/153-find-minimum-in-rotated-sorted-array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) 4 | 5 | ### 解題思路 6 | 7 | Sorted 二分法求解 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func findMin(nums []int) int { 13 | low, high := 0, len(nums)-1 14 | 15 | for low < high { 16 | mid := low + (high-low)/2 17 | 18 | if nums[mid] > nums[high] { 19 | low = mid+1 20 | } else { 21 | high = mid 22 | } 23 | } 24 | 25 | return nums[low] 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/153-find-minimum-in-rotated-sorted-array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findMin(nums []int) int { 3 | 4 | left, right := 0, len(nums) - 1 5 | 6 | for left < right { 7 | mid := left + (right - left) >> 1 8 | if nums[mid] > nums[right] { 9 | left = mid + 1 10 | } else { 11 | right = mid 12 | } 13 | } 14 | 15 | return nums[left] 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/153-find-minimum-in-rotated-sorted-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/153-find-minimum-in-rotated-sorted-array/index.md -------------------------------------------------------------------------------- /algorithms/153-find-minimum-in-rotated-sorted-array/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 寻找旋转排序数组中的最小值 2 | 3 | ## 解法 4 | 5 | 数组反转后左侧的数比右侧的大,利用这个特点,使用二分法查找 6 | 7 | ```golang 8 | func findMin(nums []int) int { 9 | l, m, r := 0, 0, len(nums)-1 10 | for l < r { 11 | if nums[l] <= nums[r] { 12 | return nums[l] 13 | } 14 | m = (r + l) / 2 15 | if nums[l] <= nums[m] { 16 | l = m + 1 17 | } else { 18 | r = m 19 | } 20 | 21 | } 22 | return nums[l] 23 | } 24 | ``` -------------------------------------------------------------------------------- /algorithms/167-two_sum_input_array_is_sorted/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [167. Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) 4 | 5 | ### 解題思路 6 | 7 | 二元搜索法 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func twoSum(numbers []int, target int) []int { 13 | for i, n := range numbers { 14 | b := binarySearch(numbers, target-n, i+1, len(numbers)-1) 15 | if b != -1 { 16 | return []int{i + 1, b + 1} 17 | } 18 | } 19 | return []int{} 20 | } 21 | 22 | func binarySearch(nums []int, target int, left int, right int) int { 23 | // 注意這裡。 >= 就會錯 24 | if left > right { 25 | return -1 26 | } 27 | mid := (left + right) / 2 28 | if nums[mid] == target { 29 | return mid 30 | } 31 | if nums[mid] > target { 32 | return binarySearch(nums, target, left, mid-1) 33 | } 34 | return binarySearch(nums, target, mid+1, right) 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/167-two_sum_input_array_is_sorted/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [167. 两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/submissions/) 3 | 4 | ## 思路 5 | > 因为给定数组为升序,可以从首位相加,算出来的值过大去掉最大值,算出来的值过小去掉最小值 6 | 7 | ## 代码 8 | ```go 9 | func twoSum3(numbers []int, target int) []int { 10 | for i, j := 0, len(numbers)-1; i < j; { 11 | if numbers[i] + numbers[j] > target { 12 | j-- 13 | } 14 | if numbers[i] + numbers[j] < target { 15 | i++ 16 | } 17 | if numbers[i] + numbers[j] == target { 18 | return []int{i+1, j+1} 19 | } 20 | } 21 | return nil 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/167-two_sum_input_array_is_sorted/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func twoSum(numbers []int, target int) []int { 3 | 4 | i, j := 0, len(numbers)-1 5 | 6 | for i < j { 7 | sum := numbers[i] + numbers[j] 8 | if sum > target { 9 | j-- 10 | } else if sum < target { 11 | i++ 12 | } else { 13 | return []int{i+1, j+1} 14 | } 15 | } 16 | 17 | return nil 18 | 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/167-two_sum_input_array_is_sorted/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/167-two_sum_input_array_is_sorted/index.md -------------------------------------------------------------------------------- /algorithms/167-two_sum_input_array_is_sorted/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 4 ms, faster than 98.79% of Go online submissions for Two Sum II - Input array is sorted. 2 | 3 | Memory Usage: 3 MB, less than 70.90% of Go online submissions for Two Sum II - Input array is sorted. 4 | 5 | 6 | ``` 7 | func twoSum(numbers []int, target int) []int { 8 | data := make(map[int]int) 9 | 10 | for i, v := range numbers { 11 | rest := target - v 12 | 13 | restIndex, ok := data[rest] 14 | if ok { 15 | if i > restIndex { 16 | return []int{restIndex+1, i+1} 17 | } else { 18 | return []int{i+1, restIndex+1} 19 | } 20 | } 21 | data[v] = i 22 | } 23 | 24 | return nil 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /algorithms/169-majority_element/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [169. Majority Element](https://leetcode.com/problems/majority-element/) 4 | 5 | ### 解題思路 6 | 7 | HashMap 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func majorityElement(nums []int) int { 13 | // 處理特殊情況 14 | if len(nums) == 1 { 15 | return nums[0] 16 | } 17 | 18 | // hashMap 記錄出現次數 19 | hashMap := make(map[int]int) 20 | half := len(nums) / 2 21 | 22 | for _, v := range nums { 23 | count, ok := hashMap[v] 24 | if ok { 25 | count++ 26 | hashMap[v] = count 27 | // 超過一半立刻返回 28 | if count > half { 29 | return v 30 | } 31 | } else { 32 | hashMap[v] = 1 33 | } 34 | } 35 | 36 | return 0 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /algorithms/169-majority_element/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func majorityElement(nums []int) int { 3 | 4 | count := make(map[int]int) 5 | res := 0 6 | 7 | for _, v := range nums { 8 | count[v]++ 9 | if count[v] >= len(nums)/2+1 { 10 | res = v 11 | break 12 | } 13 | } 14 | 15 | return res 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/169-majority_element/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/169-majority_element/index.md -------------------------------------------------------------------------------- /algorithms/169-majority_element/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 12 ms, faster than 99.83% of Go online submissions for Majority Element. 2 | 3 | Memory Usage: 5.9 MB, less than 75.24% of Go online submissions for Majority Element. 4 | 5 | 速度有点慢 6 | ``` 7 | func majorityElement(nums []int) int { 8 | res, times := nums[0], 1 9 | for _, v := range nums { 10 | if res != v { 11 | times-- 12 | } else { 13 | times++ 14 | } 15 | if times == 0 { 16 | res = v 17 | times = 1 18 | } 19 | } 20 | if times >= 1 { 21 | return res 22 | } 23 | return 0 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/189-rotate_array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [189. Rotate Array](https://leetcode.com/problems/rotate-array/) 4 | 5 | ### 解題思路 6 | 7 | 1. 先全部翻轉 8 | 2. 翻轉下標頭到 k-1 9 | 3. 翻轉下標 k 到尾巴 10 | 11 | *例子* 12 | 13 | [1,2,3,4,5,6,7] and k = 3 14 | 15 | 1. [7,6,5,4,3,2,1] 16 | 2. [5,6,7,4,3,2,1] 17 | 3. [5,6,7,1,2,3,4] 18 | 19 | ### 代碼 20 | 21 | ```go 22 | func rotate(nums []int, k int) { 23 | n := len(nums) 24 | 25 | k = k % n 26 | 27 | reverse(nums, 0, n-1) 28 | reverse(nums, 0, k-1) 29 | reverse(nums, k, n-1) 30 | } 31 | 32 | func reverse(nums []int, i int, j int) { 33 | for i < j { 34 | nums[i], nums[j] = nums[j], nums[i] 35 | i++ 36 | j-- 37 | } 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /algorithms/189-rotate_array/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/submissions/) 3 | 4 | ## 思路 5 | 如果直接用apend将nums[i:], nums[:i]相加后重新赋值给nums打印出来是正确的,但是被赋值的nums是一个新的地址,原来的nums并没有改变 6 | 使用copy直接将正确翻转后的值直接赋给nums即可 7 | 8 | ## 代码 9 | ```go 10 | func rotate(nums []int, k int) { 11 | i := len(nums) - k % len(nums) 12 | copy(nums, append(nums[i:], nums[:i]...)) 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/189-rotate_array/byxjxj.md: -------------------------------------------------------------------------------- 1 | func rotate(nums []int, k int) { 2 | 3 | res := make([]int, len(nums)) 4 | 5 | for i, _ := range nums { 6 | res[(i+k)%len(nums)] = nums[i] 7 | } 8 | 9 | copy(nums, res) 10 | } 11 | -------------------------------------------------------------------------------- /algorithms/189-rotate_array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/189-rotate_array/index.md -------------------------------------------------------------------------------- /algorithms/189-rotate_array/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 52 ms 2 | 3 | Memory Usage: 7.6 MB 4 | 5 | 想不到好的解法 6 | 7 | ``` 8 | func rotate(nums []int, k int) { 9 | length := len(nums) 10 | k = k % length 11 | temp := make([]int, length) 12 | copy(temp, nums) 13 | copy(nums[k:], temp[:length-k]) 14 | copy(nums[:k], temp[length-k:]) 15 | } 16 | ``` 17 | -------------------------------------------------------------------------------- /algorithms/198-house-robber/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func rob(nums []int) int { 3 | 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | pre, res := 0, 0 9 | for i := 0; i < len(nums); i++ { 10 | pre, res = res, max(res, pre+nums[i]) 11 | } 12 | 13 | return res 14 | } 15 | 16 | func max(a, b int) int { 17 | if a > b { 18 | return a 19 | } 20 | 21 | return b 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/198-house-robber/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/198-house-robber/index.md -------------------------------------------------------------------------------- /algorithms/198-house-robber/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 198.打家劫舍 2 | 3 | ## 解法 4 | 5 | 动态规划的思路,找到状态转移方程 `f(n) = max(f(n-2)+nums[n], f(n-1))` ,初始值为 `f(0)=0, f(1)=nums[1]` 6 | 7 | ```golang 8 | func rob(nums []int) int { 9 | x, y := 0, 0 10 | for _, n := range nums { 11 | x, y = y, x+n 12 | if x > y { 13 | y = x 14 | } 15 | } 16 | return y 17 | } 18 | ``` -------------------------------------------------------------------------------- /algorithms/200-number-of-islands/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func numIslands(grid [][]byte) int { 3 | 4 | if len(grid) == 0 || len(grid[0]) == 0 { 5 | return 0 6 | } 7 | 8 | dirx := []int{0, 0, -1, 1} 9 | diry := []int{-1, 1, 0, 0} 10 | 11 | var dfs func(int, int) 12 | dfs = func(i, j int) { 13 | if i >= len(grid) || i < 0 || j >= len(grid[0]) || j < 0 || grid[i][j] == '0' { 14 | return 15 | } 16 | 17 | grid[i][j] = '0' 18 | 19 | for k := 0; k < 4; k++ { 20 | dfs(i+dirx[k], j+diry[k]) 21 | } 22 | } 23 | 24 | res := 0 25 | for i := range grid { 26 | for j := range grid[i] { 27 | if grid[i][j] == '1' { 28 | dfs(i, j) 29 | res++ 30 | } 31 | } 32 | } 33 | 34 | return res 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/200-number-of-islands/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/200-number-of-islands/index.md -------------------------------------------------------------------------------- /algorithms/200-number-of-islands/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 200.number of is islands 2 | 3 | ## 解法 4 | 5 | 遍历矩阵,找到 '1',岛屿数加一,然后深度优先搜索将相邻的 '1' 修改为 '0',继续遍历 6 | 7 | ```golang 8 | func numIslands(grid [][]byte) int { 9 | h := len(grid) 10 | if h == 0 { 11 | return 0 12 | } 13 | res := 0 14 | w := len(grid[0]) 15 | for i := 0; i < h; i++ { 16 | for j := 0; j < w; j++ { 17 | if grid[i][j] == '1' { 18 | res++ 19 | clear(grid, j, i, w, h) 20 | } 21 | } 22 | } 23 | return res 24 | } 25 | 26 | func clear(grid [][]byte, x, y, w, h int) { 27 | if x < 0 || x >= w || y < 0 || y >= h || grid[y][x] == '0' { 28 | return 29 | } 30 | grid[y][x] = '0' 31 | clear(grid, x+1, y, w, h) 32 | clear(grid, x-1, y, w, h) 33 | clear(grid, x, y+1, w, h) 34 | clear(grid, x, y-1, w, h) 35 | } 36 | ``` -------------------------------------------------------------------------------- /algorithms/213-house-robber-ii/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func rob(nums []int) int { 3 | 4 | switch len(nums) { 5 | case 0: 6 | return 0 7 | case 1: 8 | return nums[0] 9 | } 10 | 11 | pre1, res1 := 0, 0 12 | pre2, res2 := 0, 0 13 | for i := 0; i < len(nums) - 1; i++ { 14 | pre1, res1 = res1, max(res1, pre1+nums[i]) 15 | pre2, res2 = res2, max(res2, pre2+nums[i+1]) 16 | } 17 | 18 | return max(res1, res2) 19 | } 20 | 21 | func max(a, b int) int { 22 | if a > b { 23 | return a 24 | } 25 | 26 | return b 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/213-house-robber-ii/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/213-house-robber-ii/index.md -------------------------------------------------------------------------------- /algorithms/213-house-robber-ii/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 213.打家劫舍 2 | 3 | ## 解法 4 | 5 | 198.house-robber题不同的是,第一个房子与最后一个房子连起来,所以求两种情况的最大值再比较,“不抢最后一个房子的”与“不抢第一个房子”,即 nums[:len(nums)-1] 与 nums[1:] 6 | 7 | ```golang 8 | func rob(nums []int) int { 9 | l := len(nums) 10 | if l == 0 { 11 | return 0 12 | } 13 | if l == 1 { 14 | return nums[0] 15 | } 16 | res1 := _rob(nums[:l-1]) 17 | res2 := _rob(nums[1:]) 18 | if res1 < res2 { 19 | return res2 20 | } 21 | return res1 22 | } 23 | 24 | func _rob(nums []int) int { 25 | x, y := 0, nums[0] 26 | for _, n := range nums[1:] { 27 | x, y = y, x+n 28 | if y < x { 29 | y = x 30 | } 31 | } 32 | return y 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/215-kth-largest-element-in-an-array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) 4 | 5 | ### 解題思路 6 | 7 | 排列之後取第幾大值即可 8 | 9 | ### 代碼 10 | 11 | ```go 12 | import "sort" 13 | 14 | func findKthLargest(nums []int, k int) int { 15 | sort.Ints(nums) 16 | return nums[len(nums)-k] 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/215-kth-largest-element-in-an-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/215-kth-largest-element-in-an-array/index.md -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) 4 | 5 | ### 解題思路 6 | 7 | hashMap 遇到存在直接返回 true 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func containsDuplicate(nums []int) bool { 13 | hashMap := make(map[int]int) 14 | 15 | for _, v := range nums { 16 | _, ok := hashMap[v] 17 | if ok { 18 | return true 19 | } 20 | hashMap[v] = v 21 | } 22 | 23 | return false 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [217. 存在重复元素](https://leetcode-cn.com/problems/contains-duplicate/submissions/) 3 | 4 | ## 思路 5 | 先排序,再比较相邻两个有没有一样的 6 | 7 | ## 代码 8 | ```go 9 | func containsDuplicate(nums []int) bool { 10 | sort.Ints(nums) 11 | for i := 1; i < len(nums); i++ { 12 | if nums[i] == nums[i -1] { 13 | return true 14 | } 15 | } 16 | return false 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/byxjxj.md: -------------------------------------------------------------------------------- 1 | func containsDuplicate(nums []int) bool { 2 | 3 | count := make(map[int]int, len(nums)) 4 | 5 | for _, v := range nums { 6 | count[v]++ 7 | if count[v] > 1 { 8 | return true 9 | } 10 | } 11 | 12 | return false 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/217-contains_duplicate/index.md -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | ```go 2 | func containsDuplicate(nums []int) bool { 3 | re := false 4 | 5 | if nums == nil { 6 | return re 7 | } 8 | 9 | if len(nums) <= 1 { 10 | return re 11 | } 12 | 13 | sort.Ints(nums) 14 | target := nums[0] 15 | for i := 1; i < len(nums); i++ { 16 | if nums[i] == target { 17 | re = true 18 | break 19 | } else { 20 | target = nums[i] 21 | } 22 | } 23 | return re 24 | } 25 | ``` 26 | 27 | 借助map中的hashtab特性 28 | 29 | ```go 30 | func containsDuplicate(nums []int) bool { 31 | re := false 32 | 33 | m:= make(map[int]int) 34 | 35 | for k,v := range nums { 36 | if _, ok := m[v]; ok { 37 | re = true 38 | break 39 | }else{ 40 | m[v]=k 41 | } 42 | } 43 | return re 44 | } 45 | ``` -------------------------------------------------------------------------------- /algorithms/217-contains_duplicate/whao246.md: -------------------------------------------------------------------------------- 1 | ## 217. Contains Duplicate 2 | 3 | 给定一个整数数组,判断是否存在重复元素。 4 | 5 | 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 6 | 7 | 示例 1: 8 | 9 | 输入: [1,2,3,1] 10 | 输出: true 11 | 示例 2: 12 | 13 | 输入: [1,2,3,4] 14 | 输出: false 15 | 示例 3: 16 | 17 | 输入: [1,1,1,3,3,4,3,2,4,2] 18 | 输出: true 19 | 20 | ## 解析 21 | 22 | 遍历数组, 如果哈希表中存在元素则返回true, 否则, 把元素加入到哈希表中 23 | 24 | ## 代码 25 | 26 | ```go 27 | func containsDuplicate(nums []int) bool { 28 | // if len(nums)==0{ 29 | // return true 30 | // } 31 | res:=make(map[int]int) 32 | for i:=0;i (3 ^ 0 ^ 0) ^ (1 ^ 1) ^ (2 ^ 3) => 2 22 | ``` 23 | 24 | ### 代碼 25 | 26 | ```go 27 | func missingNumber(nums []int) int { 28 | missing := len(nums) 29 | for i, v := range nums { 30 | missing = missing ^ i ^ v 31 | } 32 | return missing 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /algorithms/268-missing-number/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [268. 缺失数字](https://leetcode-cn.com/problems/missing-number/submissions/) 3 | 4 | ## 思路一 5 | 各种if else堆叠:joy: 6 | ## 代码 7 | ```go 8 | func missingNumber(nums []int) int { 9 | if len(nums) == 1 && nums[0] == 1 { 10 | return 0 11 | } 12 | sort.Ints(nums) 13 | 14 | for i:=1; i <= len(nums); { 15 | //fmt.Println(len(nums)) 16 | //fmt.Println(1, len(nums)) 17 | if i == len(nums) { 18 | //fmt.Println(i, len(nums)) 19 | return nums[i-1] + 1 20 | }else if nums[i] != nums[i-1] + 1 { 21 | return nums[i-1] + 1 22 | } else if nums[1] == 2 { 23 | return 0 24 | } 25 | i++ 26 | } 27 | return 0 28 | } 29 | ``` 30 | 31 | ## 思路二 32 | 用从0加到n的数,减去nums里面现有的数就是缺失的数字 33 | ## 代码 34 | ```go 35 | func missingNumber(nums []int) int { 36 | sum1, sum2 := 0, len(nums) 37 | for i := 0; i < len(nums); i++ { 38 | sum2 = sum2 + i 39 | sum1 = sum1 + nums[i] 40 | } 41 | return sum2 - sum1 42 | } 43 | -------------------------------------------------------------------------------- /algorithms/268-missing-number/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func missingNumber(nums []int) int { 3 | 4 | res := 0 5 | 6 | for i, v := range nums { 7 | res ^= i ^ v 8 | } 9 | 10 | return res^len(nums) 11 | 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /algorithms/268-missing-number/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/268-missing-number/index.md -------------------------------------------------------------------------------- /algorithms/268-missing-number/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | 2 | Runtime: 16 ms, faster than 96.05% of Go online submissions for Missing Number. 3 | Memory Usage: 5.9 MB, less than 100.00% of Go online submissions for Missing Number. 4 | 5 | ```go 6 | func missingNumber(nums []int) int { 7 | for k, v := range nums { 8 | if k == 0 { 9 | nums[0] = k - v 10 | } else { 11 | nums[0] += k - v 12 | } 13 | } 14 | return len(nums) + nums[0] 15 | } 16 | ``` 17 | 18 | 19 | -------------------------------------------------------------------------------- /algorithms/268-missing-number/whao246.md: -------------------------------------------------------------------------------- 1 | ## [268. 缺失数字](https://leetcode-cn.com/problems/missing-number/) 2 | 3 | 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 4 | 5 | 示例 1: 6 | 7 | 输入: [3,0,1] 8 | 输出: 2 9 | 示例 2: 10 | 11 | 输入: [9,6,4,2,3,5,7,0,1] 12 | 输出: 8 13 | 14 | ## 思路 15 | 16 | 高斯定理:cry: 17 | 18 | 没想到 19 | 20 | 21 | 22 | ## 代码 23 | 24 | ```go 25 | func missingNumber(nums []int) int { 26 | res:=0 27 | n:=len(nums) 28 | for i:=0;i b { 29 | return b 30 | } 31 | return a 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /algorithms/279-perfect-squares/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func numSquares(n int) int { 3 | 4 | dp := make([]int, n+1) 5 | 6 | for i := 0; i <= n; i++ { 7 | dp[i] = i 8 | for j := 1; i-j*j >= 0; j++ { 9 | dp[i] = min(dp[i], dp[i-j*j]+1) 10 | } 11 | } 12 | 13 | return dp[n] 14 | } 15 | 16 | func min(a, b int) int { 17 | if a > b { 18 | return b 19 | } 20 | 21 | return a 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/279-perfect-squares/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/279-perfect-squares/index.md -------------------------------------------------------------------------------- /algorithms/279-perfect-squares/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 完美平方数 2 | 3 | ```golang 4 | /* 5 | * @lc app=leetcode.cn id=279 lang=golang 6 | * 动态规划的思路,状态转移方程:dp[n] = min(dp[n-1*1]+1, dp[n-2*2]+1, ..., dp[n-k*k]+1), ( 0< k*k <=n ) 7 | */ 8 | func numSquares(n int) int { 9 | if n <= 0 { 10 | return 0 11 | } 12 | dp := make([]int, n+1) // 多申请了一份整形,使代码更容易理解, dp[n] 就是 n 的完全平方数的求解 13 | for i := 1; i <= n; i++ { 14 | dp[i] = i // 初始值 dp[n] 的最大值的解,也是最容易求的解 15 | for j := 0; j*j <= i; j++ { 16 | dp[i] = minInt(dp[i-j*j]+1, dp[i]) 17 | } 18 | } 19 | return dp[n] 20 | } 21 | 22 | func minInt(x, y int) int { 23 | if x < y { 24 | return x 25 | } 26 | return y 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/) 4 | 5 | ### 解題思路 6 | 7 | 1. replaceIndex 記錄 0 值的位置,遍歷數組將非 0 值往 replaceIndex 寫入 8 | 2. 最後再將 0 值補齊即可 9 | 10 | ### 代碼 11 | 12 | ```go 13 | func moveZeroes(nums []int) { 14 | replaceIndex := 0 15 | 16 | for _, v := range nums { 17 | if v != 0 { 18 | nums[replaceIndex] = v 19 | replaceIndex++ 20 | } 21 | } 22 | 23 | for i := replaceIndex; i < len(nums); i++ { 24 | nums[i] = 0 25 | } 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [283. 移动零](https://leetcode-cn.com/problems/move-zeroes/submissions/) 3 | ## 思路 4 | 先把不等于0的往前移,然后把后面的都等于0 5 | ## 代码 6 | ```go 7 | func moveZeroes(nums []int) { 8 | n := 0 9 | // 不等于0的向前移 10 | for _, v := range nums { 11 | if v != 0 { 12 | nums[n] = v 13 | n++ 14 | } 15 | } 16 | // 0往后移 17 | for j := n; j < len(nums); { 18 | nums[j] = 0 19 | j++ 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func moveZeroes(nums []int) { 3 | 4 | j := 0 5 | for i, v := range nums { 6 | if v != 0 { 7 | nums[i], nums[j] = nums[j], nums[i] 8 | j++ 9 | } 10 | } 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/283-move-zeroes/index.md -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 60 ms, faster than 99.03% of Go online submissions for Move Zeroes. 2 | 3 | Memory Usage: 7.9 MB, less than 26.04% of Go online submissions for Move Zeroes. 4 | 5 | ``` 6 | func moveZeroes(nums []int) { 7 | setNum, curNum, length := 0, 0, len(nums) 8 | if length == 0 { 9 | return 10 | } 11 | for curNum < length { 12 | if nums[curNum] != 0 { 13 | nums[setNum] = nums[curNum] 14 | setNum++ 15 | } 16 | curNum++ 17 | } 18 | for setNum < length { 19 | nums[setNum] = 0 20 | setNum++ 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | 2 | Runtime: 60 ms, faster than 99.03% of Go online submissions for Move Zeroes. 3 | Memory Usage: 7.9 MB, less than 26.09% of Go online submissions for Move Zeroes. 4 | 5 | 思路: 6 | 7 | 1. 定义一个变量标识第一个值为0的下标元素位置 8 | 9 | 1. 遍历数组,将为0与不为0的元素交换 10 | 11 | 12 | ```go 13 | func moveZeroes(nums []int) { 14 | curre := 0 // nums[curre]==0 15 | for i := 1; i < len(nums); i++ { 16 | if nums[i] != 0 { 17 | if nums[curre] == 0 { 18 | nums[curre], nums[i] = nums[i], nums[curre] 19 | curre++ 20 | } 21 | } else { 22 | if nums[curre] != 0 { 23 | curre = i 24 | } 25 | } 26 | } 27 | } 28 | ``` -------------------------------------------------------------------------------- /algorithms/283-move-zeroes/whao246.md: -------------------------------------------------------------------------------- 1 | ### 283. Move Zeroes 2 | 3 | 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 4 | 5 | 示例: 6 | 7 | 输入: [0,1,0,3,12] 8 | 输出: [1,3,12,0,0] 9 | 说明: 10 | 11 | 必须在原数组上操作,不能拷贝额外的数组。 12 | 尽量减少操作次数。 13 | 14 | ### 解析 15 | 16 | 当index和i都指向非0时,交换同一个元素,index和i都加1 17 | 18 | 当index和i指向0时, i加1 19 | 20 | 当index指向0,i指向非0, 交换两个元素, index和i都加1 21 | 22 | ### 代码 23 | 24 | ```go 25 | func moveZeroes(nums []int) { 26 | index:=0 27 | for i:=0;i nums[j] { 25 | cur = d[j] + 1 26 | } 27 | d[i] = Max(d[i], cur) 28 | } 29 | max = Max(max, d[i]) 30 | } 31 | 32 | return max 33 | } 34 | 35 | func Max(a, b int) int { 36 | if a > b { 37 | return a 38 | } 39 | return b 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /algorithms/300-longest-increasing-subsequence/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func lengthOfLIS(nums []int) int { 3 | 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | 8 | res := make([]int, 0, len(nums)) 9 | for i := range nums { 10 | if i == 0 || nums[i] > res[len(res)-1] { 11 | res = append(res, nums[i]) 12 | } else { 13 | l, r := 0, len(res)-1 14 | for l < r { 15 | mid := l + (r-l) >> 1 16 | if nums[i] > res[mid] { 17 | l = mid + 1 18 | } else { 19 | r = mid 20 | } 21 | } 22 | res[r] = nums[i] 23 | } 24 | } 25 | 26 | return len(res) 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/300-longest-increasing-subsequence/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/300-longest-increasing-subsequence/index.md -------------------------------------------------------------------------------- /algorithms/303-range-sum-query-immutable/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | type NumArray struct { 3 | sum []int 4 | } 5 | 6 | 7 | func Constructor(nums []int) NumArray { 8 | arr := NumArray{ 9 | sum : make([]int, len(nums)+1), 10 | } 11 | for i := range nums { 12 | arr.sum[i+1] = arr.sum[i] + nums[i] 13 | } 14 | 15 | return arr 16 | } 17 | 18 | 19 | func (this *NumArray) SumRange(i int, j int) int { 20 | return this.sum[j+1] - this.sum[i] 21 | } 22 | 23 | 24 | /** 25 | * Your NumArray object will be instantiated and called as such: 26 | * obj := Constructor(nums); 27 | * param_1 := obj.SumRange(i,j); 28 | */ 29 | ``` 30 | -------------------------------------------------------------------------------- /algorithms/303-range-sum-query-immutable/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/303-range-sum-query-immutable/index.md -------------------------------------------------------------------------------- /algorithms/303-range-sum-query-immutable/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 303 区域和检索 - 数组不可变 2 | 3 | ## 解法 4 | 5 | 动态规划,推导方程 `f(i,j) = f(0,j)-f(0,i-1)` 6 | 7 | ```golang 8 | type NumArray struct { 9 | Dp []int 10 | } 11 | 12 | func Constructor(nums []int) NumArray { 13 | dp := make([]int, len(nums)+1) 14 | dp[0] = 0 15 | for i, n := range nums { 16 | dp[i+1] = dp[i] + n 17 | } 18 | return NumArray{Dp: dp} 19 | } 20 | 21 | func (this *NumArray) SumRange(i int, j int) int { 22 | return this.Dp[j+1] - this.Dp[i] 23 | } 24 | ``` -------------------------------------------------------------------------------- /algorithms/343-integer-break/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func integerBreak(n int) int { 3 | 4 | res := make([]int, n+1) 5 | res[0], res[1] = 0, 1 6 | for i := 2; i <= n; i++ { 7 | maxProduct := -1 << 31 8 | for j := 1; j <= i - 1; j++ { 9 | maxProduct = max(maxProduct, j*(i-j), j*res[i-j]) 10 | } 11 | res[i] = maxProduct 12 | } 13 | 14 | return res[n] 15 | } 16 | 17 | func max(a, b, c int) int { 18 | var res int 19 | if a > b { 20 | res = a 21 | } else { 22 | res = b 23 | } 24 | 25 | if res < c { 26 | res = c 27 | } 28 | 29 | return res 30 | } 31 | ``` 32 | -------------------------------------------------------------------------------- /algorithms/343-integer-break/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/343-integer-break/index.md -------------------------------------------------------------------------------- /algorithms/345-reverse-vowels-of-a-string/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) 4 | 5 | ### 解題思路 6 | 7 | 雙指針遍歷,分別找到左右兩邊的母音然後替換即可 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func reverseVowels(s string) string { 13 | vowels := "aeiouAEIOU" 14 | left, right := 0, len(s)-1 15 | 16 | ss := []rune(s) 17 | 18 | for left < right { 19 | if !strings.ContainsRune(vowels, ss[left]) { 20 | left++ 21 | } else if !strings.ContainsRune(vowels, ss[right]) { 22 | right-- 23 | } else { 24 | ss[left], ss[right] = ss[right], ss[left] 25 | left++ 26 | right-- 27 | } 28 | } 29 | 30 | return string(ss) 31 | } 32 | ``` 33 | -------------------------------------------------------------------------------- /algorithms/345-reverse-vowels-of-a-string/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func reverseVowels(s string) string { 3 | 4 | vowel := map[byte]bool { 5 | 'a': true, 'A': true, 6 | 'e': true, 'E': true, 7 | 'i': true, 'I': true, 8 | 'o': true, 'O': true, 9 | 'u': true, 'U': true, 10 | } 11 | 12 | i, j := 0, len(s)-1 13 | tmp := []byte(s) 14 | 15 | for i < j { 16 | for i < len(s) && !vowel[tmp[i]] { 17 | i++ 18 | } 19 | for j >= 0 && !vowel[tmp[j]] { 20 | j-- 21 | } 22 | if i >= j { 23 | break 24 | } 25 | 26 | tmp[i], tmp[j] = tmp[j], tmp[i] 27 | i++ 28 | j-- 29 | } 30 | 31 | return string(tmp) 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /algorithms/345-reverse-vowels-of-a-string/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/345-reverse-vowels-of-a-string/index.md -------------------------------------------------------------------------------- /algorithms/345-reverse-vowels-of-a-string/whao246.md: -------------------------------------------------------------------------------- 1 | ### 345. 反转字符串中的元音字母 2 | 3 | ### 代码 4 | ``` 5 | func reverseVowels(s string) string { 6 | m:="aoeiuAOEIU" 7 | sByte := []rune(s) 8 | i:=0 9 | j:=len(s)-1 10 | for i= min { 21 | res = append(res, k) 22 | } 23 | } 24 | 25 | return res 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/347-top-k-frequent-elements/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/347-top-k-frequent-elements/index.md -------------------------------------------------------------------------------- /algorithms/347-top-k-frequent-elements/whao246.md: -------------------------------------------------------------------------------- 1 | ### 347. 前K个高频元素 2 | ### 代码 3 | 4 | ``` 5 | func frequencySort(s string) string{ 6 | m:=make(map[byte]int) 7 | out:=[]int{} 8 | res:=[]byte{} 9 | for i:=0;i= p[1] + 1; j-- { 12 | res[j] = res[j-1] 13 | } 14 | res[p[1]] = p 15 | 16 | count++ 17 | } 18 | 19 | return res 20 | } 21 | 22 | type arr [][]int 23 | 24 | func (a arr) Len() int { 25 | return len(a) 26 | } 27 | 28 | func (a arr) Less(i, j int) bool { 29 | if a[i][0] != a[j][0] { 30 | return a[i][0] > a[j][0] 31 | } 32 | return a[i][1] < a[j][1] 33 | } 34 | 35 | func (a arr) Swap(i, j int) { 36 | a[i], a[j] = a[j], a[i] 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /algorithms/406-queue-reconstruction-by-height/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/406-queue-reconstruction-by-height/index.md -------------------------------------------------------------------------------- /algorithms/413-arithmetic-slices/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func numberOfArithmeticSlices(A []int) int { 3 | 4 | if len(A) < 3 { 5 | return 0 6 | } 7 | 8 | count, res := 0, 0 9 | for i := 2; i < len(A); i++ { 10 | if A[i] - A[i-1] == A[i-1] - A[i-2] { 11 | count++ 12 | res += count 13 | } else { 14 | count = 0 15 | } 16 | } 17 | 18 | return res 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/413-arithmetic-slices/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/413-arithmetic-slices/index.md -------------------------------------------------------------------------------- /algorithms/414-third-maximum-number/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [414. Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) 4 | 5 | ### 解題思路 6 | 7 | 用三個數記錄大小遍歷即可 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func thirdMax(nums []int) int { 13 | const INT_MAX = int(^uint(0) >> 1) 14 | const INT_MIN = ^INT_MAX 15 | 16 | s1, s2, s3 := INT_MIN, INT_MIN, INT_MIN 17 | 18 | for _, num := range nums { 19 | if num > s1 { 20 | s1, s2, s3 = num, s1, s2 21 | } else if num < s1 && num > s2 { 22 | s2, s3 = num, s2 23 | } else if num < s2 && num > s3 { 24 | s3 = num 25 | } 26 | } 27 | 28 | if s3 != INT_MIN { 29 | // return the third maximum number 30 | return s3 31 | } else { 32 | // return the maximum 33 | return s1 34 | } 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/414-third-maximum-number/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func thirdMax(nums []int) int { 3 | 4 | tmp := make([]int, 3) 5 | for i := range tmp { 6 | tmp[i] = -2<<31 7 | } 8 | 9 | for _, v := range nums { 10 | if v > tmp[0] { 11 | tmp[0], tmp[1], tmp[2] = v, tmp[0], tmp[1] 12 | } else if v > tmp[1] && v != tmp[0] { 13 | tmp[1], tmp[2] = v, tmp[1] 14 | } else if v > tmp[2] && v != tmp[0] && v != tmp[1] { 15 | tmp[2] = v 16 | } 17 | } 18 | 19 | if tmp[2] != -2<<31 { 20 | return tmp[2] 21 | } 22 | 23 | return tmp[0] 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/414-third-maximum-number/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/414-third-maximum-number/index.md -------------------------------------------------------------------------------- /algorithms/414-third-maximum-number/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 4 ms, faster than 98.81% of Go online submissions for Third Maximum Number. 2 | 3 | Memory Usage: 3 MB, less than 83.33% of Go online submissions for Third Maximum Number. 4 | ``` 5 | func thirdMax(nums []int) int { 6 | m1 := ^int(^uint(0)>>1) 7 | m2, m3 := m1, m1 8 | for _, v := range nums { 9 | if v == m1 || v == m2 || v == m3 { 10 | continue 11 | } else if v > m1 { 12 | m1, m2, m3 = v, m1, m2 13 | } else if v > m2 { 14 | m2, m3 = v, m2 15 | } else if v > m3 { 16 | m3 = v 17 | } 18 | } 19 | if m3 != ^int(^uint(0)>>1) { 20 | return m3 21 | } else { 22 | return m1 23 | } 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/417-pacific-atlantic-water-flow/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/417-pacific-atlantic-water-flow/index.md -------------------------------------------------------------------------------- /algorithms/435-non-overlapping-intervals/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func eraseOverlapIntervals(intervals [][]int) int { 3 | 4 | sort.Sort(arr(intervals)) 5 | res, j := 0, 0 6 | 7 | for i := 1; i < len(intervals); i++ { 8 | if intervals[i][0] < intervals[j][1] { 9 | if intervals[i][1] < intervals[j][1] { 10 | j = i 11 | } 12 | res++ 13 | } else { 14 | j = i 15 | } 16 | } 17 | 18 | return res 19 | } 20 | 21 | type arr [][]int 22 | 23 | func (a arr) Len() int { 24 | return len(a) 25 | } 26 | 27 | func (a arr) Less(i, j int) bool { 28 | return a[i][0] < a[j][0] 29 | } 30 | 31 | func (a arr) Swap(i, j int) { 32 | a[i], a[j] = a[j], a[i] 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /algorithms/435-non-overlapping-intervals/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/435-non-overlapping-intervals/index.md -------------------------------------------------------------------------------- /algorithms/448-find-all-numbers-disappeared-in-an-array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) 4 | 5 | ### 解題思路 6 | 7 | 這題利用標記法 8 | 9 | 首先遍歷數組,把當下標數值的絕對值減去 1 得到應該標記的位置,並把對應值改成負值 10 | 11 | 最後再遍歷一次數組,找出其值不為零的下標 + 1 即可 12 | 13 | ### 代碼 14 | 15 | ```go 16 | func findDisappearedNumbers(nums []int) []int { 17 | // 把數字對應的下標的值改成負數 18 | for _, v := range nums { 19 | index := int(abs(v) - 1) 20 | if nums[index] > 0 { 21 | nums[index] = -nums[index] 22 | } 23 | } 24 | 25 | // 找出不是負數的下標 +1 即可找出未出現的值 26 | res := make([]int, 0) 27 | for i := 0; i < len(nums); i++ { 28 | if nums[i] > 0 { 29 | res = append(res, i+1) 30 | } 31 | } 32 | 33 | return res 34 | } 35 | 36 | func abs(x int) int { 37 | if x < 0 { 38 | return -x 39 | } 40 | return x 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /algorithms/448-find-all-numbers-disappeared-in-an-array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findDisappearedNumbers(nums []int) []int { 3 | 4 | for i := range nums { 5 | for nums[i] != nums[nums[i]-1] { 6 | nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i] 7 | } 8 | } 9 | 10 | res := make([]int, 0, len(nums)) 11 | 12 | for i, v := range nums { 13 | if i+1 != v { 14 | res = append(res, i+1) 15 | } 16 | } 17 | 18 | return res 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/448-find-all-numbers-disappeared-in-an-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/448-find-all-numbers-disappeared-in-an-array/index.md -------------------------------------------------------------------------------- /algorithms/448-find-all-numbers-disappeared-in-an-array/lhwang95.md: -------------------------------------------------------------------------------- 1 | 标记法 2 | Runtime: 368 ms, faster than 95.10% of Go online submissions for Find All Numbers Disappeared in an Array. 3 | 4 | Memory Usage: 8.5 MB, less than 24.67% of Go online submissions for Find All Numbers Disappeared in an Array. 5 | ``` 6 | func findDisappearedNumbers(nums []int) []int { 7 | for i:=0; i 0 { 10 | nums[temp] = -nums[temp] 11 | } 12 | } 13 | res := make([]int, 0) 14 | for i, v := range nums { 15 | if v > 0 { 16 | res = append(res, i+1) 17 | } 18 | } 19 | return res 20 | } 21 | ``` 22 | 23 | 2、 24 | -------------------------------------------------------------------------------- /algorithms/451-sort-characters-by-frequency/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func frequencySort(s string) string { 3 | 4 | fre := make(map[rune]int, len(s)) 5 | for _, v := range s { 6 | fre[v]++ 7 | } 8 | 9 | ss := make([]string, 0, len(fre)) 10 | for k, v := range fre { 11 | tmp := make([]rune, v) 12 | for i := range tmp { 13 | tmp[i] = k 14 | } 15 | ss = append(ss, string(tmp)) 16 | } 17 | 18 | sort.Sort(str(ss)) 19 | 20 | res := "" 21 | for _, v := range ss { 22 | res += v 23 | } 24 | 25 | return res 26 | } 27 | 28 | type str []string 29 | 30 | func (s str) Len() int { 31 | return len(s) 32 | } 33 | 34 | func (s str) Less(i, j int) bool { 35 | return len(s[i]) > len(s[j]) 36 | } 37 | 38 | func (s str) Swap(i, j int) { 39 | s[i], s[j] = s[j], s[i] 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /algorithms/451-sort-characters-by-frequency/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/451-sort-characters-by-frequency/index.md -------------------------------------------------------------------------------- /algorithms/451-sort-characters-by-frequency/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 4 ms, faster than 96.34% of Go online submissions for Sort Characters By Frequency. 2 | 3 | Memory Usage: 5.8 MB, less than 52.38% of Go online submissions for Sort Characters By Frequency. 4 | ``` 5 | func frequencySort(s string) string { 6 | temp := make([]byte, len(s)) 7 | copy(temp, s) 8 | data := make([]string, 0) 9 | for i := 0; i < len(s); { 10 | begin := i 11 | for j := i + 1; j < len(s); j++ { 12 | if temp[j] == temp[i] { 13 | i++ 14 | temp[i], temp[j] = temp[j], temp[i] 15 | } 16 | } 17 | i++ 18 | data = append(data, string(temp[begin:i])) 19 | } 20 | sort.Slice(data, func (i, j int) bool { 21 | return len(data[i]) > len(data[j]) 22 | }) 23 | var res string 24 | for i:=0; i end { 23 | end = points[i][1] 24 | res++ 25 | } 26 | } 27 | 28 | return res + 1 29 | } 30 | 31 | type Sorter [][]int 32 | 33 | func (s Sorter) Len() int { 34 | return len(s) 35 | } 36 | 37 | func (s Sorter) Swap(i, j int) { 38 | s[i], s[j] = s[j], s[i] 39 | } 40 | 41 | func (s Sorter) Less(i, j int) bool { 42 | return s[i][1] < s[j][1] 43 | } 44 | ``` 45 | -------------------------------------------------------------------------------- /algorithms/452-minimum-number-of-arrows-to-burst-balloons/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findMinArrowShots(points [][]int) int { 3 | 4 | if len(points) <= 1 { 5 | return len(points) 6 | } 7 | 8 | sort.Slice(points, func (i, j int) bool { 9 | return points[i][1] < points[j][1] 10 | }) 11 | res, i := 1, 0 12 | for j := 0; j < len(points); j++ { 13 | if points[i][1] < points[j][0] { 14 | res++ 15 | i = j 16 | } 17 | } 18 | 19 | return res 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/452-minimum-number-of-arrows-to-burst-balloons/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/452-minimum-number-of-arrows-to-burst-balloons/index.md -------------------------------------------------------------------------------- /algorithms/455-assign-cookies/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [455. Assign Cookies](https://leetcode.com/problems/assign-cookies/description/) 4 | 5 | ### 解題思路 6 | 7 | 排序之後只要餅乾的大小,大於等於孩童所要的就給予不然就換下一個餅乾 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func findContentChildren(g []int, s []int) int { 13 | sort.Ints(g) 14 | sort.Ints(s) 15 | 16 | gi, si := 0, 0 17 | 18 | for gi < len(g) && si < len(s) { 19 | if g[gi] <= s[si] { 20 | gi++ 21 | } 22 | si++ 23 | } 24 | 25 | return gi 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/455-assign-cookies/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findContentChildren(g []int, s []int) int { 3 | 4 | sort.Ints(g) 5 | sort.Ints(s) 6 | res, i, j := 0, 0, 0 7 | 8 | for i < len(g) && j < len(s) { 9 | if g[i] <= s[j] { 10 | i++ 11 | res++ 12 | } 13 | j++ 14 | } 15 | 16 | return res 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/455-assign-cookies/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/455-assign-cookies/index.md -------------------------------------------------------------------------------- /algorithms/455-assign-cookies/whao246.md: -------------------------------------------------------------------------------- 1 | ### 455. 分发饼干 2 | 3 | ### 代码 4 | ``` 5 | func findContentChildren(g []int, s []int) int { 6 | j:=0 7 | sort.Ints(g) 8 | sort.Ints(s) 9 | for i:=0;i=g[j]{ 11 | j++ 12 | } 13 | } 14 | return j 15 | } 16 | ``` -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/submissions/) 4 | 5 | ### 解題思路 6 | 7 | for loop counter 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func findMaxConsecutiveOnes(nums []int) int { 13 | max, counter := 0, 0 14 | for _, v := range nums { 15 | if v == 1 { 16 | counter++ 17 | continue 18 | } 19 | if counter > max { 20 | max = counter 21 | } 22 | counter = 0 23 | } 24 | if counter > max { 25 | max = counter 26 | } 27 | return max 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findMaxConsecutiveOnes(nums []int) int { 3 | 4 | tmp, max := 0, 0 5 | 6 | for _, v := range nums { 7 | if v == 1 { 8 | tmp++ 9 | } else { 10 | tmp = 0 11 | } 12 | 13 | if max < tmp { 14 | max = tmp 15 | } 16 | } 17 | 18 | return max 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/485-max-consecutive-ones/index.md -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/lhwang95.md: -------------------------------------------------------------------------------- 1 | 直接遍历 2 | 3 | Runtime: 32 ms, faster than 98.56% of Go online submissions for Max Consecutive Ones. 4 | 5 | Memory Usage: 6.4 MB, less than 25.47% of Go online submissions for Max Consecutive Ones 6 | 7 | ``` 8 | func findMaxConsecutiveOnes(nums []int) int { 9 | max, cur := 0, 0 10 | for _, v := range nums { 11 | if v == 0 { 12 | cur = 0 13 | } else { 14 | cur++ 15 | if cur > max { 16 | max = cur 17 | } 18 | } 19 | } 20 | return max 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/mojiajuzi.md: -------------------------------------------------------------------------------- 1 | 2 | Runtime: 40 ms, faster than 48.80% of Go online submissions for Max Consecutive Ones. 3 | Memory Usage: 6.3 MB, less than 55.66% of Go online submissions for Max Consecutive Ones. 4 | 5 | 6 | ```go 7 | func findMaxConsecutiveOnes(nums []int) int { 8 | max := 0 9 | curr := 0 10 | for i := 0; i < len(nums); i++ { 11 | if nums[i] != 0 { 12 | curr++ 13 | } else { 14 | if max < curr { 15 | max = curr 16 | } 17 | curr = 0 18 | } 19 | } 20 | if max > curr { 21 | return max 22 | } 23 | return curr 24 | } 25 | ``` -------------------------------------------------------------------------------- /algorithms/485-max-consecutive-ones/whao246.md: -------------------------------------------------------------------------------- 1 | ### 485. Max Consecutive Ones 2 | 给定一个二进制数组, 计算其中最大连续1的个数。 3 | 4 | 示例 1: 5 | 6 | 输入: [1,1,0,1,1,1] 7 | 输出: 3 8 | 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 9 | 10 | 11 | ### 代码 12 | ``` 13 | func findMaxConsecutiveOnes(nums []int) int { 14 | res:=0 15 | left:=-1 16 | for i:=0;ib{ 28 | return a 29 | } 30 | return b 31 | } 32 | ``` -------------------------------------------------------------------------------- /algorithms/509-fibonacci-number/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) 4 | 5 | ### 解題思路 6 | 7 | 遞歸函數 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func fib(N int) int { 13 | if N < 0 { 14 | return 0 15 | } 16 | if N <= 1 { 17 | return N 18 | } 19 | return fib(N-1) + fib(N-2) 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/509-fibonacci-number/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [509. 斐波那契数](https://leetcode-cn.com/problems/fibonacci-number/submissions/) 3 | 4 | ## 思路 5 | 6 | 7 | ## 代码 8 | ```go 9 | func fib(N int) int { 10 | a, b := 0, 1 11 | for i:=0; i len(res) { 15 | res = str 16 | break 17 | } else if index == len(str) && len(str)==len(res) && str < res { 18 | res = str 19 | break 20 | } 21 | } 22 | } 23 | return res 24 | } 25 | ``` 26 | -------------------------------------------------------------------------------- /algorithms/524-longest-word-in-dictionary-through-deleting/whao246.md: -------------------------------------------------------------------------------- 1 | ### 524. 通过删除字母匹配到字典里最长单词 2 | 3 | ### 代码 4 | 5 | ``` 6 | func findLongestWord(s string, d []string) string { 7 | res:="" 8 | 9 | for _, str:=range d{ 10 | i:=0 11 | for _, c:=range s{ 12 | if str[i]==byte(c){ 13 | i++ 14 | } 15 | if i==len(str){ 16 | break 17 | } 18 | } 19 | if i == len(str) && len(str) >= len(res) { 20 | if len(str) > len(res) || str < res { 21 | res = str; 22 | } 23 | } 24 | } 25 | return res 26 | } 27 | ``` -------------------------------------------------------------------------------- /algorithms/532-k-diff-pairs-in-an-array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [532. K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/submissions/) 4 | 5 | ### 解題思路 6 | 7 | Hash Map 記錄次數 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func findPairs(nums []int, k int) int { 13 | // edge cases 14 | if k < 0 { 15 | return 0 16 | } 17 | 18 | dict, count := make(map[int]int), 0 19 | 20 | for _, v := range nums { 21 | dict[v]++ 22 | } 23 | 24 | for v, c := range dict { 25 | if k == 0 { 26 | // k == 0 ,算出現兩次的數字即可 27 | if c >= 2 { 28 | count++ 29 | } 30 | } else { 31 | if dict[v+k] > 0 { 32 | count++ 33 | } 34 | } 35 | } 36 | 37 | return count 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /algorithms/532-k-diff-pairs-in-an-array/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [532. 数组中的K-diff数对](https://leetcode-cn.com/problems/k-diff-pairs-in-an-array/submissions/) 3 | 4 | ## 思路 5 | 双重循环 6 | 7 | ## 代码 8 | ```go 9 | func findPairs(nums []int, k int) int { 10 | sort.Ints(nums) 11 | m := make(map[int]int) 12 | for i, v := range nums { 13 | for _, u := range nums[i+1:]{ 14 | if k == u -v { 15 | m[u] = v 16 | break 17 | }else if u - v >k { 18 | break 19 | } 20 | } 21 | } 22 | return len(m) 23 | } 24 | -------------------------------------------------------------------------------- /algorithms/532-k-diff-pairs-in-an-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/532-k-diff-pairs-in-an-array/index.md -------------------------------------------------------------------------------- /algorithms/532-k-diff-pairs-in-an-array/lhwang95.md: -------------------------------------------------------------------------------- 1 | map使我快乐 2 | 3 | Runtime: 16 ms, faster than 99.31% of Go online submissions for K-diff Pairs in an Array. 4 | 5 | Memory Usage: 6.3 MB, less than 49.18% of Go online submissions for K-diff Pairs in an Array. 6 | 7 | ``` 8 | func findPairs(nums []int, k int) int { 9 | data, res := make(map[int]int), 0 10 | if k < 0 { 11 | return 0 12 | } 13 | for _, v := range nums { 14 | if _, ok := data[v]; ok { 15 | data[v]++ 16 | } else { 17 | data[v] = 1 18 | } 19 | } 20 | if k == 0 { 21 | for _, v := range data { 22 | if v >= 2 { 23 | res += 1 24 | } 25 | } 26 | } else { 27 | for v, _ := range data { 28 | if _, ok := data[v+k]; ok { 29 | res += 1 30 | } 31 | } 32 | } 33 | return res 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /algorithms/540-single-element-in-a-sorted-array/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [540. Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/description/) 4 | 5 | ### 解題思路 6 | 7 | 二分法變種 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func singleNonDuplicate(nums []int) int { 13 | low, high := 0, len(nums)-1 14 | 15 | for low <= high { 16 | mid := low + (high-low)/2 17 | 18 | if mid-1 >= 0 && nums[mid] == nums[mid-1] { 19 | mid-- 20 | } else if mid+1 < len(nums) && nums[mid] == nums[mid+1] { 21 | } else { 22 | return nums[mid] 23 | } 24 | 25 | if ((mid - low) % 2) == 1 { 26 | high = mid - 1 27 | } else { 28 | low = mid + 2 29 | } 30 | } 31 | 32 | return -1 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /algorithms/540-single-element-in-a-sorted-array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func singleNonDuplicate(nums []int) int { 3 | 4 | if len(nums) == 1 { 5 | return nums[0] 6 | } 7 | 8 | left, right := 0, len(nums) - 1 9 | 10 | for left < right { 11 | mid := left + (right - left) >> 1 12 | tmp := mid ^ 1 13 | if nums[mid] == nums[tmp] { 14 | left = mid + 1 15 | } else { 16 | right = mid 17 | } 18 | } 19 | 20 | return nums[left] 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/540-single-element-in-a-sorted-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/540-single-element-in-a-sorted-array/index.md -------------------------------------------------------------------------------- /algorithms/540-single-element-in-a-sorted-array/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 540. Single Element in a Sorted Array 2 | 3 | ## 解法 4 | 5 | 单身狗的位置与众不同... 6 | 7 | ```golang 8 | func singleNonDuplicate(nums []int) int { 9 | l, m, r := 0, 0, len(nums)-1 10 | for l < r { 11 | m = (r-l)/2 + l 12 | if m%2 == 1 { 13 | m-- 14 | } 15 | if nums[m] == nums[m+1] { 16 | l = m + 2 17 | } else { 18 | r = m 19 | } 20 | } 21 | return nums[r] 22 | } 23 | ``` -------------------------------------------------------------------------------- /algorithms/547-friend-circles/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/547-friend-circles/index.md -------------------------------------------------------------------------------- /algorithms/547-friend-circles/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 547 friend circles 2 | 3 | ## 解法 4 | 5 | 深度优先搜索,对于每一个人,遍历他的好友,再遍历他的好友的好友,这样就遍历了一个朋友圈的人,同时标记已遍历过的好友,下次便不再遍历(这样可以跳过遍历过的朋友圈),每个朋友圈只遍历一次,这样就能统计朋友圈的数量了 6 | 7 | ```golang 8 | func findCircleNum(M [][]int) int { 9 | n := len(M) 10 | if n == 0 { 11 | return 0 12 | } 13 | visted := make([]bool, n) 14 | res := 0 15 | var walk func(k int) 16 | walk = func(k int) { 17 | visted[k] = true 18 | for i := 0; i < n; i++ { 19 | if M[k][i] == 0 || visted[i] { 20 | continue 21 | } 22 | walk(i) 23 | } 24 | } 25 | for i := 0; i < n; i++ { 26 | if visted[i] { 27 | continue 28 | } 29 | walk(i) 30 | res++ 31 | } 32 | return res 33 | } 34 | ``` -------------------------------------------------------------------------------- /algorithms/561-array-partition-i/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [561. Array Partition I](https://leetcode.com/problems/array-partition-i/) 4 | 5 | ### 解題思路 6 | 7 | 先排序然後取 0, 2, 4, ... 偶數下標的值相加即可 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func arrayPairSum(nums []int) int { 13 | sort.Ints(nums) 14 | sum := 0 15 | for i := 0; i < len(nums); i += 2 { 16 | sum += nums[i] 17 | } 18 | return sum 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/561-array-partition-i/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [561. 数组拆分 I](https://leetcode-cn.com/problems/array-partition-i/submissions/) 3 | 4 | ## 思路 5 | 隔一个加一下 6 | 7 | ## 代码 8 | ```go 9 | func arrayPairSum(nums []int) int { 10 | sort.Ints(nums) 11 | v := 0 12 | for i:=0; i 0; j-- { 19 | if tmp[j] != nums[j]{ 20 | return j+1 - l 21 | } 22 | } 23 | } 24 | } 25 | 26 | return 0 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/581-shortest-unsorted-continuous-subarray/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findUnsortedSubarray(nums []int) int { 3 | 4 | left, right := 0, -1 5 | max, min := nums[0], nums[len(nums)-1] 6 | 7 | for i := range nums { 8 | if max <= nums[i] { 9 | max = nums[i] 10 | } else { 11 | right = i 12 | } 13 | 14 | j := len(nums) - i - 1 15 | if min >= nums[j] { 16 | min = nums[j] 17 | } else { 18 | left = j 19 | } 20 | } 21 | 22 | return right - left + 1 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /algorithms/581-shortest-unsorted-continuous-subarray/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/581-shortest-unsorted-continuous-subarray/index.md -------------------------------------------------------------------------------- /algorithms/581-shortest-unsorted-continuous-subarray/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 24 ms, faster than 99.09% of Go online submissions for Shortest Unsorted Continuous Subarray. 2 | 3 | Memory Usage: 6.2 MB, less than 80.88% of Go online submissions for Shortest Unsorted Continuous Subarray. 4 | 5 | ``` 6 | func findUnsortedSubarray(nums []int) int { 7 | begin, end, m := -1, -1, 0 8 | for a := 1; a < len(nums); a++ { 9 | if nums[a] < nums[m] { 10 | end = a 11 | if begin == -1 { 12 | begin = a - 1 13 | } 14 | for begin > 0 && nums[begin - 1] > nums[a] { 15 | begin -= 1 16 | } 17 | } else if nums[a] > nums[m] { 18 | m = a; 19 | } 20 | } 21 | if begin == -1 { 22 | return 0 23 | } else { 24 | return end - begin + 1 25 | } 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/581-shortest-unsorted-continuous-subarray/whao246.md: -------------------------------------------------------------------------------- 1 | ### 581. 最短无序连续子数组 2 | 3 | 4 | ### 代码 5 | ``` 6 | func findUnsortedSubarray(nums []int) int { 7 | start:=-1 8 | res:=0 9 | for i:=1;i0&&nums[j]j{ 17 | start=j 18 | } 19 | res=i-start+1 20 | } 21 | } 22 | 23 | return res 24 | } 25 | 26 | ``` -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) 4 | 5 | ### 解題思路 6 | 7 | 當前下標的左右兩邊是 0 就代表可以種花,記得的處理特殊例子 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func canPlaceFlowers(flowerbed []int, n int) bool { 13 | // [0] 14 | if len(flowerbed) == 1 && flowerbed[0] == 0 { 15 | flowerbed[0] = 1 16 | n-- 17 | } 18 | 19 | // [0,0,1] 20 | if flowerbed[0] == 0 && flowerbed[1] == 0 { 21 | flowerbed[0] = 1 22 | n-- 23 | } 24 | 25 | for i := 1; i < len(flowerbed); i++ { 26 | if flowerbed[i] == 0 { 27 | // 處理尾巴 28 | if flowerbed[i-1] == 0 && i == len(flowerbed)-1 { 29 | flowerbed[i] = 1 30 | n-- 31 | continue 32 | } 33 | if flowerbed[i-1] == 0 && flowerbed[i+1] == 0 { 34 | flowerbed[i] = 1 35 | n-- 36 | } 37 | } 38 | 39 | } 40 | return n <= 0 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [605. 种花问题](https://leetcode-cn.com/problems/can-place-flowers/submissions/) 3 | 4 | ## 思路 5 | 当前面、后面和自己都是0时将自己变成1,在0位置时忽略前面的,在最后位置时同理 6 | 7 | 想不出来,借鉴别人写的:sweat_smile: 8 | 9 | ## 代码 10 | ```go 11 | func canPlaceFlowers(flowerbed []int, n int) bool { 12 | l := len(flowerbed) 13 | for i:= 0; i < l; i++ { 14 | if flowerbed[i] == 0 && // 必须当前为0 15 | (i+1 >= l || flowerbed[i+1] == 0 && i+1 <= l-1) && // 后一个数为0,或者当前数为最后一个 16 | (i-1 < 0 || flowerbed[i-1] == 0 && i-1 >= 0){ // 前一个数为0,或者当前数在最前面 17 | flowerbed[i] = 1 18 | n-- 19 | // 种完n朵即可 20 | if n <= 0 { 21 | return true 22 | } 23 | } 24 | } 25 | 26 | return n <= 0 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func canPlaceFlowers(flowerbed []int, n int) bool { 3 | 4 | length := len(flowerbed) 5 | 6 | for i, v := range flowerbed { 7 | if v == 0 && 8 | (i == 0 || flowerbed[i-1] == 0) && 9 | (i == length-1 || flowerbed[i+1] == 0) { 10 | flowerbed[i] = 1 11 | n-- 12 | 13 | if n <= 0 { 14 | return true 15 | } 16 | } 17 | } 18 | 19 | return n <= 0 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/605-can-place-flowers/index.md -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 16 ms, faster than 95.65% of Go online submissions for Can Place Flowers. 2 | 3 | Memory Usage: 5.9 MB, less than 30.56% of Go online submissions for Can Place Flowers. 4 | ``` 5 | func canPlaceFlowers(flowerbed []int, n int) bool { 6 | length := len(flowerbed) 7 | res, i := 0, 0 8 | for i= n { 15 | return true 16 | } 17 | } 18 | return false 19 | } 20 | ``` 21 | -------------------------------------------------------------------------------- /algorithms/605-can-place-flowers/whao246.md: -------------------------------------------------------------------------------- 1 | ### 605. 种花问题 2 | 3 | 4 | ### 代码 5 | ``` 6 | func canPlaceFlowers(flowerbed []int, n int) bool { 7 | count:=0 8 | 9 | for i:=0;i0&&i=n{ 32 | return true 33 | } 34 | return false 35 | } 36 | ``` -------------------------------------------------------------------------------- /algorithms/628-maximum-product-of-three-numbers/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func maximumProduct(nums []int) int { 3 | 4 | max1, max2, max3 := -1<<31, -1<<31, -1<<31 5 | min1, min2 := 1>>31, 1>>31 6 | 7 | for _, v := range nums { 8 | if v > max1 { 9 | max3, max2, max1 = max2, max1, v 10 | } else if v > max2 { 11 | max3, max2 = max2, v 12 | } else if v > max3 { 13 | max3 = v 14 | } 15 | 16 | if v < min1 { 17 | min2, min1 = min1, v 18 | } else if v < min2 { 19 | min2 = v 20 | } 21 | } 22 | 23 | return max(max1*max2*max3, max1*min1*min2) 24 | } 25 | 26 | func max(a, b int) int { 27 | if a > b { 28 | return a 29 | } 30 | 31 | return b 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /algorithms/628-maximum-product-of-three-numbers/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/628-maximum-product-of-three-numbers/index.md -------------------------------------------------------------------------------- /algorithms/628-maximum-product-of-three-numbers/whao246.md: -------------------------------------------------------------------------------- 1 | ### 628. 三个数的最大乘积 2 | 3 | ### 解析 4 | 找最大三个数和最小2个数 5 | ### 代码 6 | ``` 7 | func maximumProduct(nums []int) int { 8 | mn1, mn2, mn3 := -1000, -1000, -1000 9 | mx1, mx2 := 1000, 1000 10 | for _, num :=range nums{ 11 | if num>=mn1{ 12 | mn3=mn2 13 | mn2=mn1 14 | mn1=num 15 | }else if num>=mn2{ 16 | mn3=mn2 17 | mn2=num 18 | }else if num>=mn3{ 19 | mn3=num 20 | } 21 | 22 | if num<=mx1{ 23 | mx2=mx1 24 | mx1=num 25 | }else if num<=mx2{ 26 | mx2=num 27 | } 28 | } 29 | return max(mx1*mx2*mn1, mn1*mn2*mn3) 30 | 31 | } 32 | 33 | func max(a, b int)int{ 34 | if a>b{ 35 | return a 36 | } 37 | return b 38 | } 39 | ``` -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [633. Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/) 4 | 5 | ### 解題思路 6 | 7 | 雙指針 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func judgeSquareSum(c int) bool { 13 | if c == 0 { 14 | return true 15 | } 16 | 17 | left, right := 0, int(math.Sqrt(float64(c))) 18 | 19 | for left < right { 20 | sum := left*left + right*right 21 | if sum == c { 22 | return true 23 | } else if sum > c { 24 | right-- 25 | } else { 26 | left-- 27 | } 28 | } 29 | 30 | return false 31 | } 32 | ``` 33 | -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [633. 平方数之和](https://leetcode-cn.com/problems/sum-of-square-numbers) 3 | 4 | ## 代码 5 | ```go 6 | func judgeSquareSum(c int) bool { 7 | s, e := 0, int(math.Sqrt(float64(c))) 8 | for s <= e { 9 | if r := s*s + e*e; r == c { 10 | return true 11 | } else if r > c { 12 | e-- 13 | } else { 14 | s++ 15 | } 16 | } 17 | return false 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func judgeSquareSum(c int) bool { 3 | 4 | maxNum := int(math.Sqrt(float64(c))) 5 | 6 | for a := 0; a <= maxNum; a++ { 7 | b := int(math.Sqrt(float64(c-a*a))) 8 | if a*a + b*b == c { 9 | return true 10 | } 11 | } 12 | 13 | return false 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/633-sum-of-square-numbers/index.md -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 0 ms, faster than 100.00% of Go online submissions for Sum of Square Numbers. 2 | 3 | Memory Usage: 1.9 MB, less than 100.00% of Go online submissions for Sum of Square Numbers. 4 | ``` 5 | func judgeSquareSum(c int) bool { 6 | min, max := 0, int(math.Sqrt(float64(c))) 7 | for min <= max { 8 | res := min*min + max*max 9 | if res > c { 10 | max-- 11 | } else if res < c { 12 | min++ 13 | } else { 14 | return true 15 | } 16 | } 17 | return false 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /algorithms/633-sum-of-square-numbers/whao246.md: -------------------------------------------------------------------------------- 1 | ###633. Sum of Square Numbers 2 | 3 | ### 代码 4 | 5 | ``` 6 | func judgeSquareSum(c int) bool { 7 | i:=0 8 | j:=int(math.Sqrt(float64(c))) 9 | for i<=j{ 10 | if i*i+j*j==c{ 11 | return true 12 | }else if i*i+j*j>c{ 13 | j-- 14 | }else{ 15 | i++ 16 | } 17 | } 18 | return false 19 | } 20 | ``` -------------------------------------------------------------------------------- /algorithms/643-maximum-average-subarray-i/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [643. Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) 4 | 5 | ### 解題思路 6 | 7 | Sliding window algorithm. 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func findMaxAverage(nums []int, k int) float64 { 13 | sum := 0 14 | 15 | for _, v := range nums[:k] { 16 | sum += v 17 | } 18 | 19 | maxSum, windowSum := sum, sum 20 | 21 | for i := 0; i < len(nums)-k; i++ { 22 | windowSum += nums[k+i] - nums[i] 23 | if windowSum > maxSum { 24 | maxSum = windowSum 25 | } 26 | } 27 | 28 | return float64(maxSum) / float64(k) 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /algorithms/643-maximum-average-subarray-i/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [643. 子数组最大平均数 I](https://leetcode-cn.com/problems/maximum-average-subarray-i/submissions/) 3 | 4 | ## 代码 5 | ```go 6 | func findMaxAverage(nums []int, k int) float64 { 7 | temp := 0 8 | // 算出第一轮k个数和 9 | for _, v := range nums[:k] { 10 | temp += v 11 | } 12 | max := temp 13 | 14 | for i, v := range nums[k:] { 15 | // 减去前一个,加上后一个 16 | temp = temp - nums[i] + v 17 | if temp > max { 18 | max = temp 19 | } 20 | } 21 | 22 | return float64(max)/float64(k) 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /algorithms/643-maximum-average-subarray-i/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findMaxAverage(nums []int, k int) float64 { 3 | 4 | tmp := 0 5 | 6 | for i := 0; i < k; i++ { 7 | tmp += nums[i] 8 | } 9 | 10 | max := tmp 11 | 12 | for i := 0; i < len(nums)-k; i++ { 13 | tmp = tmp - nums[i] + nums[i+k] 14 | if max < tmp { 15 | max = tmp 16 | } 17 | } 18 | 19 | return float64(max) / float64(k) 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/643-maximum-average-subarray-i/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/643-maximum-average-subarray-i/index.md -------------------------------------------------------------------------------- /algorithms/643-maximum-average-subarray-i/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 124 ms, faster than 97.26% of Go online submissions for Maximum Average Subarray I. 2 | 3 | Memory Usage: 7.5 MB, less than 52.00% of Go online submissions for Maximum Average Subarray I. 4 | ``` 5 | func findMaxAverage(nums []int, k int) float64 { 6 | kfront, kend, sum, max := 0, k-1, 0, 0 7 | for i:=0; i=len(nums){ 9 | return average(nums, 0, len(nums)) 10 | } 11 | for i:=0;i<=len(nums)-k;i++{ 12 | temp:= average(nums, i, i+k) 13 | if res end { 15 | end = pair[1] 16 | res++ 17 | } 18 | } 19 | 20 | return res 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/646-maximum-length-of-pair-chain/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/646-maximum-length-of-pair-chain/index.md -------------------------------------------------------------------------------- /algorithms/646-maximum-length-of-pair-chain/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 646. 最长数对链 2 | 3 | ## 解法 4 | 5 | 题目重要的条件是链中前者的第二个元素比后者的第一个元素小 6 | 7 | 用贪心算法的思路,每一步选择都采取当前状态下最优的选择,在本题中,每一步选择下,前者的第二个元素越小越好。 8 | 9 | 先对数对进行排序,以第二个元素为排序条件,然后遍历排序后的二维数组,对比第一个元素,判断能组成链的数量。 10 | 11 | ```golang 12 | import "sort" 13 | 14 | const INT_MAX = int(^uint(0) >> 1) 15 | const INT_MIN = ^INT_MAX 16 | 17 | func findLongestChain(pairs [][]int) int { 18 | sort.SliceStable(pairs, func(i, j int) bool { 19 | return pairs[i][1] < pairs[j][1] 20 | }) 21 | var count, end int = 0, INT_MIN 22 | for _, pair := range pairs { 23 | if pair[0] > end { 24 | count++ 25 | end = pair[1] 26 | } 27 | } 28 | return count 29 | } 30 | ``` -------------------------------------------------------------------------------- /algorithms/661-image-smoother/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [661. Image Smoother](https://leetcode.com/problems/image-smoother/) 4 | 5 | ### 解題思路 6 | 7 | 遍歷實現即可 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func imageSmoother(M [][]int) [][]int { 13 | m, n := len(M), len(M[0]) 14 | out := make([][]int, m) 15 | for r := range M { 16 | out[r] = make([]int, n) 17 | } 18 | 19 | for i := range out { 20 | for j := range out[i] { 21 | out[i][j] = getValue(M, i, j) 22 | } 23 | } 24 | 25 | return out 26 | } 27 | 28 | func getValue(M [][]int, r, c int) int { 29 | value, count := 0, 0 30 | for i := r - 1; i < r+2; i++ { 31 | for j := c - 1; j < c+2; j++ { 32 | if 0 <= i && i < len(M) && 0 <= j && j < len(M[0]) { 33 | value += M[i][j] 34 | count++ 35 | } 36 | } 37 | } 38 | return value / count 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /algorithms/661-image-smoother/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [661 图片平滑器](https://leetcode-cn.com/problems/image-smoother) 3 | 4 | ## 思路 5 | 先列出所有可能,超过边界的去掉,符合要求的加到数组后面(思路看的大佬的 6 | 7 | ## 代码 8 | 9 | ```go 10 | func imageSmoother(M [][]int) [][]int { 11 | l, e := len(M), len(M[0]) 12 | if l == 0 { 13 | return M 14 | } 15 | // 列出所有可能 16 | smooth := [][]int{{-1, 1}, {0, 1}, {1, 1}, {-1, 0}, {1, 0}, {-1, -1}, {0, -1}, {1, -1}} 17 | N := []int{} 18 | for x, v := range M { 19 | for y, a := range v { 20 | total, count := a, 1 21 | for _, s := range smooth { 22 | // 超过界限的去掉 23 | if x+s[0] < 0 || x+s[0] > l-1 || y+s[1] < 0 || y+s[1] > e-1 { 24 | continue 25 | } else { 26 | total += M[x+s[0]][y+s[1]] 27 | count++ 28 | } 29 | } 30 | // 将算出来的值加到数组中 31 | N = append(N, total/count) 32 | } 33 | // 将第一条加到M后面将数组清零 34 | M = append(M, N) 35 | N = []int{} 36 | } 37 | // 返回新加的数组 38 | return M[l:][:] 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /algorithms/661-image-smoother/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func imageSmoother(M [][]int) [][]int { 3 | 4 | res := make([][]int, len(M)) 5 | for i := range res { 6 | res[i] = make([]int, len(M[i])) 7 | } 8 | 9 | x := [8]int{-1, 1, 0, 0, -1, -1, 1, 1} 10 | y := [8]int{0, 0, -1, 1, -1, 1, -1, 1} 11 | 12 | for i := range M { 13 | for j := range M[i] { 14 | sum, count := M[i][j], 1 15 | for k := 0; k < 8; k++ { 16 | if i+x[k] >= 0 && i+x[k] < len(M) && j+y[k] >= 0 && j+y[k] < len(M[i]) { 17 | sum += M[i+x[k]][j+y[k]] 18 | count++ 19 | } 20 | } 21 | 22 | res[i][j] = sum / count 23 | } 24 | } 25 | 26 | return res 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/661-image-smoother/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/661-image-smoother/index.md -------------------------------------------------------------------------------- /algorithms/661-image-smoother/whao246.md: -------------------------------------------------------------------------------- 1 | ### 661. 图片平滑器 2 | 3 | 4 | ### 代码 5 | ``` 6 | func imageSmoother(M [][]int) [][]int { 7 | res:=make([][]int, len(M)) 8 | for i:=range res{ 9 | res[i]=make([]int, len(M[0])) 10 | } 11 | dict:=[][]int{{1,1}, {1,0},{1,-1},{0,1},{0,0},{0,-1},{-1,1},{-1,0},{-1,-1}} 12 | for i:=0;i=0&&x=0&&y nums[i] { 8 | count++ 9 | if count > 1 { 10 | return false 11 | } 12 | 13 | if i - 2 < 0 || nums[i-2] <= nums[i] { 14 | nums[i-1] = nums[i] 15 | } else { 16 | nums[i] = nums[i-1] 17 | } 18 | } 19 | } 20 | 21 | return true 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/665-non-decreasing-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/665-non-decreasing-array/index.md -------------------------------------------------------------------------------- /algorithms/665-non-decreasing-array/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 20 ms, faster than 98.31% of Go online submissions for Non-decreasing Array. 2 | 3 | Memory Usage: 6.2 MB, less than 73.91% of Go online submissions for Non-decreasing Array. 4 | ``` 5 | func checkPossibility(nums []int) bool { 6 | time := 0 7 | for i:=0; i= 2 { 12 | return false 13 | } 14 | } 15 | time2 := 0 16 | if len(nums) >= 3 { 17 | for i:=1; i= 2 { 23 | return false 24 | } 25 | } 26 | return true 27 | } 28 | ``` 29 | -------------------------------------------------------------------------------- /algorithms/665-non-decreasing-array/whao246.md: -------------------------------------------------------------------------------- 1 | ### 665. 非递减数列 2 | 3 | ### 代码 4 | ``` 5 | func checkPossibility(nums []int) bool { 6 | count:=1 7 | for i:=0;inums[i+1]{ 9 | if count==0{ 10 | return false 11 | } 12 | if i==0{ 13 | nums[i]=nums[i+1] 14 | }else{ 15 | 16 | if nums[i-1]<=nums[i+1]{ 17 | nums[i]=nums[i+1] 18 | }else{ 19 | nums[i+1]=nums[i] 20 | } 21 | } 22 | count-- 23 | 24 | } 25 | } 26 | return true 27 | } 28 | 29 | ``` -------------------------------------------------------------------------------- /algorithms/674-longest-continuous-increasing-subsequence/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [674. Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) 4 | 5 | ### 解題思路 6 | 7 | 遍歷比大小 8 | 9 | ### 代碼 10 | 11 | ```go 12 | 13 | func findLengthOfLCIS(nums []int) int { 14 | if len(nums) == 0 { 15 | return 0 16 | } 17 | 18 | right, count, max := 1, 1, 1 19 | 20 | for right < len(nums) { 21 | if nums[right-1] < nums[right] { 22 | count++ 23 | if max < count { 24 | max = count 25 | } 26 | } else { 27 | count = 1 28 | } 29 | right++ 30 | } 31 | 32 | return max 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /algorithms/674-longest-continuous-increasing-subsequence/RudeFish.md: -------------------------------------------------------------------------------- 1 | ## 题目 2 | [674. 最长连续递增序列](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/) 3 | 4 | ## 思路 5 | 循环数组,增序加一,返回最大值 6 | 7 | ## 代码 8 | ```go 9 | func findLengthOfLCIS(nums []int) int { 10 | if len(nums) == 0{ 11 | return 0 12 | } 13 | n, r := 1, 1 14 | for i, v := range nums[1:] { 15 | if v > nums[i] { 16 | n++ 17 | if n>r{ 18 | r=n 19 | } 20 | }else { 21 | n=1 22 | } 23 | } 24 | 25 | return r 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /algorithms/674-longest-continuous-increasing-subsequence/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findLengthOfLCIS(nums []int) int { 3 | 4 | if len(nums) <= 1 { 5 | return len(nums) 6 | } 7 | 8 | res, tmp := 1, 1 9 | 10 | for i := 1; i < len(nums); i++ { 11 | if nums[i] > nums[i-1] { 12 | tmp++ 13 | } else { 14 | tmp = 1 15 | } 16 | 17 | if tmp > res { 18 | res = tmp 19 | } 20 | } 21 | 22 | return res 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /algorithms/674-longest-continuous-increasing-subsequence/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 8 ms, faster than 95.69% of Go online submissions for Longest Continuous Increasing Subsequence. 2 | 3 | Memory Usage: 4.5 MB, less than 27.45% of Go online submissions for Longest Continuous Increasing Subsequence. 4 | ``` 5 | func findLengthOfLCIS(nums []int) int { 6 | if len(nums) == 0 { 7 | return 0 8 | } 9 | res, max := 1, -1 10 | for i:=0; inums[i-1]{ 12 | res=max(res, i-start) 13 | }else{ 14 | start=i-1 15 | } 16 | } 17 | return res 18 | } 19 | 20 | func max(a, b int)int{ 21 | if a>b{ 22 | return a 23 | } 24 | return b 25 | } 26 | ``` -------------------------------------------------------------------------------- /algorithms/680-valid-palindrome-ii/LIYINGZHEN.md: -------------------------------------------------------------------------------- 1 | ### 題目 2 | 3 | [680. Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) 4 | 5 | ### 解題思路 6 | 7 | 雙指針,比較是否為 Palindrome 8 | 9 | ### 代碼 10 | 11 | ```go 12 | func validPalindrome(s string) bool { 13 | l, r := 0, len(s)-1 14 | 15 | for l < r { 16 | if s[l] != s[r] { 17 | return isPalindrome(s, l+1, r) || isPalindrome(s, l, r-1) 18 | } 19 | l++ 20 | r-- 21 | } 22 | 23 | return true 24 | } 25 | 26 | func isPalindrome(s string, l, r int) bool { 27 | for l < r { 28 | if s[l] != s[r] { 29 | return false 30 | } 31 | l++ 32 | r-- 33 | } 34 | return true 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /algorithms/680-valid-palindrome-ii/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func validPalindrome(s string) bool { 3 | 4 | i, j := 0, len(s)-1 5 | 6 | for i < j { 7 | if s[i] != s[j] { 8 | return isPalindrome(s, i+1, j) || 9 | isPalindrome(s, i, j-1) 10 | } else { 11 | i++ 12 | j-- 13 | } 14 | } 15 | 16 | return true 17 | } 18 | 19 | func isPalindrome(s string, i, j int) bool { 20 | for i < j { 21 | if s[i] != s[j] { 22 | return false 23 | } 24 | i++ 25 | j-- 26 | } 27 | 28 | return true 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /algorithms/680-valid-palindrome-ii/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/680-valid-palindrome-ii/index.md -------------------------------------------------------------------------------- /algorithms/680-valid-palindrome-ii/whao246.md: -------------------------------------------------------------------------------- 1 | ### 680. 验证回文字符串 Ⅱ 2 | 3 | ### 代码 4 | 5 | ``` 6 | func validPalindrome(s string) bool { 7 | i:=0 8 | j:=len(s)-1 9 | for i= w || x < 0 || y >= h || y < 0 || grid[y][x] == 0 { 30 | return 0 31 | } 32 | grid[y][x] = 0 33 | return 1 + area(grid, x+1, y, w, h) + area(grid, x-1, y, w, h) + area(grid, x, y-1, w, h) + area(grid, x, y+1, w, h) 34 | } 35 | 36 | func maxInt(x, y int) int { 37 | if x > y { 38 | return x 39 | } 40 | return y 41 | } 42 | ``` -------------------------------------------------------------------------------- /algorithms/697-degree-of-an-array/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func findShortestSubArray(nums []int) int { 3 | 4 | size := len(nums) 5 | first := make(map[int]int, size) 6 | count := make(map[int]int, size) 7 | maxCount, minLen := 0, size 8 | 9 | for i, v:= range nums { 10 | count[v]++ 11 | if count[v] == 1 { 12 | first[v] = i 13 | } 14 | l := i - first[v] + 1 15 | 16 | if maxCount < count[v] || (maxCount == count[v] && l < minLen) { 17 | maxCount = count[v] 18 | minLen = l 19 | } 20 | } 21 | 22 | return minLen 23 | } 24 | ``` 25 | -------------------------------------------------------------------------------- /algorithms/697-degree-of-an-array/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/697-degree-of-an-array/index.md -------------------------------------------------------------------------------- /algorithms/697-degree-of-an-array/whao246.md: -------------------------------------------------------------------------------- 1 | ### 697. 数组的度 2 | 3 | ### 代码 4 | ``` 5 | func findShortestSubArray(nums []int) int { 6 | m:=make(map[int]int) 7 | startIdx:=make(map[int]int) 8 | degree:=1 9 | res:=1<<32 10 | for i:=0;idegree{ 18 | res= i - startIdx[nums[i]]+1 19 | degree = m[nums[i]] 20 | } 21 | } 22 | return res 23 | } 24 | 25 | func min(a, b int)int{ 26 | if a letters[len(letters)-1] { 5 | return letters[0] 6 | } 7 | 8 | left, right := 0, len(letters) - 1 9 | res := letters[0] 10 | for left <= right { 11 | mid := left + (right-left) >> 1 12 | if letters[mid] > target { 13 | res = letters[mid] 14 | right = mid - 1 15 | } else { 16 | left = mid + 1 17 | } 18 | } 19 | 20 | return res 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /algorithms/744-find-smallest-letter-greater-than-target/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/744-find-smallest-letter-greater-than-target/index.md -------------------------------------------------------------------------------- /algorithms/744-find-smallest-letter-greater-than-target/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 744. 寻找比目标字母大的最小字 2 | 3 | ```golang 4 | func nextGreatestLetter(letters []byte, target byte) byte { 5 | l, m, r := 0, 0, len(letters) 6 | for l < r { 7 | m = (r + l) / 2 8 | if letters[m] <= target { 9 | l = m + 1 10 | } else { 11 | r = m 12 | } 13 | } 14 | return letters[l%len(letters)] 15 | } 16 | ``` -------------------------------------------------------------------------------- /algorithms/763-partition-labels/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func partitionLabels(S string) []int { 3 | 4 | last := make(map[rune]int, 26) 5 | for i, c := range S { 6 | last[c] = i 7 | } 8 | 9 | firstC, lastC := 0, 0 10 | res := make([]int, 0) 11 | for i, c := range S { 12 | if last[c] > lastC { 13 | lastC = last[c] 14 | } 15 | if i == lastC { 16 | res = append(res, lastC-firstC+1) 17 | firstC = i + 1 18 | } 19 | } 20 | 21 | return res 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/763-partition-labels/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/763-partition-labels/index.md -------------------------------------------------------------------------------- /algorithms/763-partition-labels/lightfish-zhang.md: -------------------------------------------------------------------------------- 1 | # 763 划分字母区间 2 | 3 | ## 解法 4 | 5 | 题目定义26个小写字母,先遍历一次S,找到每个字母的最后一个位置。再遍历一遍,确保找到的每个字母的第一个位置到最后一个位置之中的字母的最后一个位置也在其中。 6 | 7 | ```golang 8 | func partitionLabels(S string) []int { 9 | flag := 0 10 | tmp := 0 11 | start := -1 12 | last := [26]int{} 13 | ret := []int{} 14 | for i := 0; i < len(S); i++ { 15 | last[S[i]-'a'] = i 16 | } 17 | for i := 0; i < len(S); i++ { 18 | tmp = last[S[i]-'a'] 19 | if flag < tmp { 20 | flag = tmp 21 | } 22 | if flag == i { 23 | ret = append(ret, flag-start) 24 | start = flag 25 | } 26 | } 27 | return ret 28 | } 29 | ``` -------------------------------------------------------------------------------- /algorithms/764-find-pivot-index/byxjxj.md: -------------------------------------------------------------------------------- 1 | ```golang 2 | func pivotIndex(nums []int) int { 3 | 4 | lSum, rSum := 0, 0 5 | for _, v := range nums { 6 | rSum += v 7 | } 8 | 9 | for i, v := range nums { 10 | rSum -= v 11 | if lSum == rSum { 12 | return i 13 | } 14 | lSum += v 15 | } 16 | 17 | return -1 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /algorithms/764-find-pivot-index/index.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studygolang/leetcode/0451ba8414faaa1e9752a63ab3ff1e1141ff1cc2/algorithms/764-find-pivot-index/index.md -------------------------------------------------------------------------------- /algorithms/764-find-pivot-index/lhwang95.md: -------------------------------------------------------------------------------- 1 | Runtime: 20 ms, faster than 93.03% of Go online submissions for Find Pivot Index. 2 | 3 | Memory Usage: 5.9 MB, less than 70.13% of Go online submissions for Find Pivot Index. 4 | ``` 5 | func pivotIndex(nums []int) int { 6 | length := len(nums) 7 | if length == 0 { 8 | return -1 9 | } else if length == 1 { 10 | return 0 11 | } 12 | left, right, curIndex := 0, 0, 0 13 | for i:=1; i