├── .gitignore ├── medium ├── 78.子集 │ ├── 78.子集.py │ ├── 78.子集.js │ └── 78.子集.go ├── 74.搜索二维矩阵 │ ├── 74.搜索二维矩阵.js │ ├── 74.搜索二维矩阵.go │ └── 74.搜索二维矩阵.py ├── 80.删除排序数组中的重复项-ii │ ├── 80.删除排序数组中的重复项-ii.py │ └── 80.删除排序数组中的重复项-ii.js ├── 89.格雷编码 │ └── 89.格雷编码.js ├── 62.不同路径 │ ├── 62.不同路径.go │ ├── 62.不同路径.py │ └── 62.不同路径.js ├── 75.颜色分类 │ ├── 75.颜色分类.go │ ├── 75.颜色分类.js │ └── 75.颜色分类.py ├── 60.第k个排列 │ ├── 60.第k个排列.py │ ├── 60.第k个排列.js │ └── 60.第k个排列.go ├── 77.组合 │ ├── 77.组合.py │ ├── 77.组合.js │ └── 77.组合.go ├── 71.简化路径 │ ├── 71.简化路径.py │ ├── 71.简化路径.go │ └── 71.简化路径.js ├── 64.最小路径和 │ ├── 64.最小路径和.go │ ├── 64.最小路径和.js │ └── 64.最小路径和.py ├── 61.旋转链表 │ ├── 61.旋转链表.go │ ├── 61.旋转链表.js │ └── 61.旋转链表.py ├── 73.矩阵置零 │ └── 73.矩阵置零.js ├── 63.不同路径-ii │ ├── 63.不同路径-ii.go │ ├── 63.不同路径-ii.js │ └── 63.不同路径-ii.py ├── 201.数字范围按位与.py ├── 79.单词搜索 │ ├── 79.单词搜索.go │ └── 79.单词搜索.py ├── 86.分隔链表 │ ├── 86.分隔链表.py │ └── 86.分隔链表.js ├── 137.只出现一次的数字-ii │ ├── 137.只出现一次的数字-ii.go │ ├── 137.只出现一次的数字-ii.py │ └── 137.只出现一次的数字-ii.js ├── 201.数字范围按位与.js ├── 179.最大数.js ├── 96.不同的二叉搜索树 │ ├── 96.不同的二叉搜索树.go │ ├── 96.不同的二叉搜索树.py │ └── 96.不同的二叉搜索树.js ├── 82.删除排序链表中的重复元素-ii │ ├── 82.删除排序链表中的重复元素-ii.js │ └── 82.删除排序链表中的重复元素-ii.py ├── 24.两两交换链表中的节点 │ ├── 24.两两交换链表中的节点.go │ ├── 24.两两交换链表中的节点.py │ └── 24.两两交换链表中的节点.js ├── 46.全排列 │ ├── 46.全排列.py │ └── 46.全排列.js ├── 179.最大数.py ├── 55.跳跃游戏 │ ├── 55.跳跃游戏.go │ ├── 55.跳跃游戏.py │ └── 55.跳跃游戏.js ├── 81.搜索旋转排序数组-ii │ └── 81.搜索旋转排序数组-ii.js ├── 22.括号生成 │ ├── 22.括号生成.py │ ├── 22.括号生成.go │ └── 22.括号生成.js ├── 152.乘积最大子序列 │ └── 152.乘积最大子序列.py ├── 49.字母异位词分组 │ └── 49.字母异位词分组.py ├── 187.重复的dna序列.py ├── 50.pow-x-n │ ├── 50.pow-x-n.go │ ├── 50.pow-x-n.py │ └── 50.pow-x-n.js ├── 31.下一个排列 │ └── 31.下一个排列.go ├── 11.盛最多水的容器 │ ├── 11.盛最多水的容器.go │ └── 11.盛最多水的容器.py ├── 187.重复的dna序列.js ├── 94.二叉树的中序遍历 │ ├── 94.二叉树的中序遍历.js │ └── 94.二叉树的中序遍历.go ├── 144.二叉树的前序遍历 │ ├── 144.二叉树的前序遍历.js │ └── 144.二叉树的前序遍历.go └── 47.全排列-ii │ ├── 47.全排列-ii.js │ └── 47.全排列-ii.py └── easy ├── 258.各位相加 ├── 258.各位相加.go ├── 258.各位相加.py └── 258.各位相加.js ├── 231.2-的幂 ├── 231.2-的幂.go ├── 231.2-的幂.py └── 231.2-的幂.js ├── 292.nim游戏 ├── 292.nim游戏.go ├── 292.nim游戏.py └── 292.nim游戏.js ├── 371.两整数之和 ├── 371.两整数之和.go ├── 371.两整数之和.js └── 371.两整数之和.py ├── 342.4-的幂 ├── 342.4-的幂.go ├── 342.4-的幂.py └── 342.4-的幂.js ├── 172.阶乘后的零 ├── 172.阶乘后的零.go ├── 172.阶乘后的零.py └── 172.阶乘后的零.js ├── 434.字符串中的单词数 ├── 434.字符串中的单词数.js ├── 434.字符串中的单词数.go └── 434.字符串中的单词数.py ├── 136.只出现一次的数字 ├── 136.只出现一次的数字.go ├── 136.只出现一次的数字.py └── 136.只出现一次的数字.js ├── 453.最小移动次数使数组元素相等 ├── 453.最小移动次数使数组元素相等.py ├── 453.最小移动次数使数组元素相等.js └── 453.最小移动次数使数组元素相等.go ├── 125.验证回文串 ├── 125.验证回文串.py └── 125.验证回文串.js ├── 268.缺失数字 ├── 268.缺失数字.go ├── 268.缺失数字.py └── 268.缺失数字.js ├── 326.3-的幂 ├── 326.3-的幂.go ├── 326.3-的幂.py └── 326.3-的幂.js ├── 204.计数质数 ├── 204.计数质数.py ├── 204.计数质数.go └── 204.计数质数.js ├── 461.汉明距离 ├── 461.汉明距离.go ├── 461.汉明距离.py └── 461.汉明距离.js ├── 389.找不同 ├── 389.找不同.py ├── 389.找不同.js └── 389.找不同.go ├── 387.字符串中的第一个唯一字符 ├── 387.字符串中的第一个唯一字符.go ├── 387.字符串中的第一个唯一字符.py └── 387.字符串中的第一个唯一字符.js ├── 169.求众数 ├── 169.求众数.py ├── 169.求众数.go └── 169.求众数.js ├── 367.有效的完全平方数 ├── 367.有效的完全平方数.go ├── 367.有效的完全平方数.py └── 367.有效的完全平方数.js ├── 476.数字的补数 ├── 476.数字的补数.go ├── 476.数字的补数.py └── 476.数字的补数.js ├── 53.最大子序和 ├── 53.最大子序和.go ├── 53.最大子序和.py └── 53.最大子序和.js ├── 217.存在重复元素 ├── 217.存在重复元素.go ├── 217.存在重复元素.py └── 217.存在重复元素.js ├── 58.最后一个单词的长度 ├── 58.最后一个单词的长度.go ├── 58.最后一个单词的长度.js └── 58.最后一个单词的长度.py ├── 1.两数之和 ├── 1.两数之和.py ├── 1.两数之和.js └── 1.两数之和.rs ├── 344.反转字符串 ├── 344.反转字符串.go ├── 344.反转字符串.js └── 344.反转字符串.py ├── 119.杨辉三角-ii ├── 119.杨辉三角-ii.go ├── 119.杨辉三角-ii.js └── 119.杨辉三角-ii.py ├── 485.最大连续-1-的个数 ├── 485.最大连续-1-的个数.py ├── 485.最大连续-1-的个数.go └── 485.最大连续-1-的个数.js ├── 202.快乐数 ├── 202.快乐数.go ├── 202.快乐数.js └── 202.快乐数.py ├── 28.实现str-str └── 28.实现str-str.py ├── 206.反转链表 ├── 206.反转链表.go ├── 206.反转链表.js └── 206.反转链表.py ├── 69.x-的平方根 ├── 69.x-的平方根.go ├── 69.x-的平方根.py └── 69.x-的平方根.js ├── 171.excel表列序号 ├── 171.excel表列序号.py ├── 171.excel表列序号.go └── 171.excel表列序号.js ├── 88.合并两个有序数组 ├── 88.合并两个有序数组.py └── 88.合并两个有序数组.go ├── 83.删除排序链表中的重复元素 ├── 83.删除排序链表中的重复元素.go ├── 83.删除排序链表中的重复元素.py └── 83.删除排序链表中的重复元素.js ├── 283.移动零 └── 283.移动零.js ├── 203.移除链表元素 ├── 203.移除链表元素.go ├── 203.移除链表元素.py └── 203.移除链表元素.js ├── 7.整数反转 ├── 7.整数反转.go └── 7.整数反转.js ├── 70.爬楼梯 ├── 70.爬楼梯.js ├── 70.爬楼梯.py └── 70.爬楼梯.go ├── 219.存在重复元素-ii ├── 219.存在重复元素-ii.go ├── 219.存在重复元素-ii.py └── 219.存在重复元素-ii.js ├── 182.查找重复的电子邮箱 └── 182.查找重复的电子邮箱.sql ├── 448.找到所有数组中消失的数字 ├── 448.找到所有数组中消失的数字.py ├── 448.找到所有数组中消失的数字.js └── 448.找到所有数组中消失的数字.go ├── 234.回文链表 └── 234.回文链表.js ├── 66.加一 ├── 66.加一.go └── 66.加一.py ├── 242.有效的字母异位词 ├── 242.有效的字母异位词.go └── 242.有效的字母异位词.py ├── 349.两个数组的交集 ├── 349.两个数组的交集.py ├── 349.两个数组的交集.go └── 349.两个数组的交集.js ├── 104.二叉树的最大深度 └── 104.二叉树的最大深度.go ├── 263.丑数 ├── 263.丑数.go ├── 263.丑数.py └── 263.丑数.js ├── 404.左叶子之和 ├── 404.左叶子之和.go ├── 404.左叶子之和.js └── 404.左叶子之和.py ├── 167.两数之和-ii-输入有序数组 └── 167.两数之和-ii-输入有序数组.go ├── 400.第n个数字 ├── 400.第n个数字.py └── 400.第n个数字.js ├── 9.回文数 ├── 9.回文数.js ├── 9.回文数.py └── 9.回文数.go ├── 118.杨辉三角 ├── 118.杨辉三角.go └── 118.杨辉三角.js ├── 14.最长公共前缀 ├── 14.最长公共前缀.py ├── 14.最长公共前缀.go └── 14.最长公共前缀.js ├── 35.搜索插入位置 └── 35.search-insert-position.go ├── 121.买卖股票的最佳时机 ├── 121.买卖股票的最佳时机.py └── 121.买卖股票的最佳时机.go ├── 415.字符串相加 └── 415.字符串相加.py ├── 409.最长回文串 └── 409.最长回文串.go ├── 168.excel表列名称 └── 168.excel表列名称.go ├── 405.数字转换为十六进制数 └── 405.数字转换为十六进制数.py └── 176.第二高的薪水 └── 176.第二高的薪水.sql /.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode -------------------------------------------------------------------------------- /medium/78.子集/78.子集.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=78 lang=python3 3 | # 4 | # [78] 子集 5 | # 6 | 7 | 8 | class Solution: 9 | def subsets(self, nums): 10 | res = [[]] 11 | for n in nums: 12 | l = len(res) 13 | for i in range(l): 14 | res.append(res[i] + [n]) 15 | return res 16 | -------------------------------------------------------------------------------- /medium/74.搜索二维矩阵/74.搜索二维矩阵.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=74 lang=javascript 3 | * 4 | * [74] 搜索二维矩阵 5 | */ 6 | /** 7 | * @param {number[][]} matrix 8 | * @param {number} target 9 | * @return {boolean} 10 | */ 11 | var searchMatrix = function(matrix, target) { 12 | return new Set(matrix.reduce((res, arr) => res.concat(arr), [])).has(target) 13 | } 14 | -------------------------------------------------------------------------------- /medium/78.子集/78.子集.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=78 lang=javascript 3 | * 4 | * [78] 子集 5 | */ 6 | /** 7 | * @param {number[]} nums 8 | * @return {number[][]} 9 | */ 10 | var subsets = function(nums) { 11 | const result = [[]] 12 | nums.forEach(n => { 13 | result.forEach(item => { 14 | result.push(item.concat(n)) 15 | }) 16 | }) 17 | return result 18 | } 19 | -------------------------------------------------------------------------------- /medium/80.删除排序数组中的重复项-ii/80.删除排序数组中的重复项-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=80 lang=python3 3 | # 4 | # [80] 删除排序数组中的重复项 II 5 | # 6 | class Solution: 7 | def removeDuplicates(self, nums: List[int]) -> int: 8 | i = 0 9 | for n in nums: 10 | if i < 2 or n > nums[i - 2]: 11 | nums[i] = n 12 | i += 1 13 | return i 14 | 15 | 16 | -------------------------------------------------------------------------------- /medium/80.删除排序数组中的重复项-ii/80.删除排序数组中的重复项-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=80 lang=javascript 3 | * 4 | * [80] 删除排序数组中的重复项 II 5 | */ 6 | /** 7 | * @param {number[]} nums 8 | * @return {number} 9 | */ 10 | var removeDuplicates = function(nums) { 11 | let i = 0 12 | for (const n of nums) { 13 | if (i < 2 || n > nums[i - 2]) { 14 | nums[i++] = n 15 | } 16 | } 17 | return i 18 | } 19 | -------------------------------------------------------------------------------- /medium/89.格雷编码/89.格雷编码.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=89 lang=javascript 3 | * 4 | * [89] 格雷编码 5 | */ 6 | /** 7 | * @param {number} n 8 | * @return {number[]} 9 | */ 10 | var grayCode = function(n) { 11 | if (n === 0) return [0] 12 | const res = grayCode(n - 1) 13 | const mask = 1 << (n - 1) 14 | for (let i = res.length - 1; i >= 0; i--) { 15 | res.push(res[i] | mask) 16 | } 17 | return res 18 | } 19 | -------------------------------------------------------------------------------- /medium/62.不同路径/62.不同路径.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=62 lang=golang 3 | * 4 | * [62] 不同路径 5 | */ 6 | func uniquePaths(m int, n int) int { 7 | dp := make([][]int, m) 8 | for i := 0; i < m; i++ { 9 | dp[i] = make([]int, n) 10 | for j := 0; j < n; j++ { 11 | if i == 0 || j == 0 { 12 | dp[i][j] = 1 13 | } else { 14 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 15 | } 16 | } 17 | } 18 | return dp[m-1][n-1] 19 | } 20 | 21 | -------------------------------------------------------------------------------- /medium/75.颜色分类/75.颜色分类.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=75 lang=golang 3 | * 4 | * [75] 颜色分类 5 | */ 6 | func sortColors(nums []int) { 7 | l := len(nums) 8 | i, j, k := 0, 0, l-1 9 | for j <= k { 10 | if nums[j] == 1 { 11 | j++ 12 | } else if nums[j] == 0 { 13 | nums[i], nums[j] = nums[j], nums[i] 14 | i++ 15 | j++ 16 | } else { 17 | nums[j], nums[k] = nums[k], nums[j] 18 | k-- 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /medium/62.不同路径/62.不同路径.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=62 lang=python3 3 | # 4 | # [62] 不同路径 5 | # 6 | 7 | 8 | class Solution: 9 | def uniquePaths(self, m: int, n: int) -> int: 10 | dp = [[0] * n] * m 11 | for i in range(m): 12 | for j in range(n): 13 | if i == 0 or j == 0: 14 | dp[i][j] = 1 15 | else: 16 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 17 | return dp[m - 1][n - 1] 18 | -------------------------------------------------------------------------------- /medium/60.第k个排列/60.第k个排列.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=60 lang=python3 3 | # 4 | # [60] 第k个排列 5 | # 6 | class Solution: 7 | def getPermutation(self, n: int, k: int) -> str: 8 | arr = [x for x in range(1, n + 1)] 9 | res = [] 10 | pos = k - 1 11 | fac = 1 12 | for n in arr: 13 | fac *= n 14 | for j in range(n, 0, -1): 15 | fac //= j 16 | res.append(str(arr.pop(pos // fac))) 17 | pos %= fac 18 | return ''.join(res) 19 | -------------------------------------------------------------------------------- /medium/77.组合/77.组合.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=77 lang=python3 3 | # 4 | # [77] 组合 5 | # 6 | 7 | 8 | class Solution: 9 | def combine(self, n: int, k: int): 10 | if k > n or k == 0: 11 | return [] 12 | if k == 1: 13 | return [[x + 1] for x in range(n)] 14 | if k == n: 15 | return [[x + 1 for x in range(n)]] 16 | res = self.combine(n - 1, k) 17 | for item in self.combine(n - 1, k - 1): 18 | item.append(n) 19 | res.append(item) 20 | return res 21 | -------------------------------------------------------------------------------- /medium/60.第k个排列/60.第k个排列.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=60 lang=javascript 3 | * 4 | * [60] 第k个排列 5 | */ 6 | /** 7 | * @param {number} n 8 | * @param {number} k 9 | * @return {string} 10 | */ 11 | var getPermutation = function(n, k) { 12 | const arr = Array.from({ length: n }, (_, i) => i + 1) 13 | const res = [] 14 | let pos = k - 1 15 | let fac = arr.reduce((a, b) => a * b) 16 | for (let j = n; j >= 1; --j) { 17 | fac /= j 18 | res.push(arr.splice(~~(pos / fac), 1)[0]) 19 | pos %= fac 20 | } 21 | return res.join('') 22 | } 23 | -------------------------------------------------------------------------------- /medium/74.搜索二维矩阵/74.搜索二维矩阵.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=74 lang=golang 3 | * 4 | * [74] 搜索二维矩阵 5 | */ 6 | func searchMatrix(matrix [][]int, target int) bool { 7 | if len(matrix) == 0 { 8 | return false 9 | } 10 | x, y := len(matrix), len(matrix[0]) 11 | l, r := 0, x*y-1 12 | for l <= r { 13 | mid := (l + r) / 2 14 | ix, iy := mid/y, mid%y 15 | if matrix[ix][iy] == target { 16 | return true 17 | } else if matrix[ix][iy] > target { 18 | r = mid - 1 19 | } else { 20 | l = mid + 1 21 | } 22 | } 23 | return false 24 | } 25 | 26 | -------------------------------------------------------------------------------- /medium/77.组合/77.组合.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=77 lang=javascript 3 | * 4 | * [77] 组合 5 | */ 6 | /** 7 | * @param {number} n 8 | * @param {number} k 9 | * @return {number[][]} 10 | */ 11 | var combine = function(n, k) { 12 | if (k > n || k === 0) return [] 13 | if (k === 1) return Array.from({ length: n }, (_, i) => [i + 1]) 14 | if (k === n) return [Array.from({ length: n }, (_, i) => i + 1)] 15 | const res = combine(n - 1, k) 16 | for (const item of combine(n - 1, k - 1)) { 17 | item.push(n) 18 | res.push(item) 19 | } 20 | return res 21 | } 22 | -------------------------------------------------------------------------------- /medium/78.子集/78.子集.go: -------------------------------------------------------------------------------- 1 | import "math" 2 | 3 | /* 4 | * @lc app=leetcode.cn id=78 lang=golang 5 | * 6 | * [78] 子集 7 | */ 8 | func subsets(nums []int) [][]int { 9 | var ret [][]int 10 | ret = append(ret, []int{}) 11 | size := len(nums) 12 | subsize := int(math.Pow(2, float64(size))) 13 | hash := 1 14 | for hash < subsize { 15 | var temp []int 16 | for k := 0; k < size; k++ { 17 | a := int(math.Pow(2, float64(k))) 18 | if (a & hash) != 0 { 19 | temp = append(temp, nums[k]) 20 | } 21 | } 22 | ret = append(ret, temp) 23 | hash++ 24 | } 25 | return ret 26 | } 27 | 28 | -------------------------------------------------------------------------------- /medium/62.不同路径/62.不同路径.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=62 lang=javascript 3 | * 4 | * [62] 不同路径 5 | */ 6 | /** 7 | * @param {number} m 8 | * @param {number} n 9 | * @return {number} 10 | */ 11 | var uniquePaths = function(m, n) { 12 | const dp = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)) 13 | for (let i = 0; i < m; i++) { 14 | for (let j = 0; j < n; j++) { 15 | if (i === 0 || j === 0) { 16 | dp[i][j] = 1 17 | } else { 18 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 19 | } 20 | } 21 | } 22 | return dp[m - 1][n - 1] 23 | } 24 | -------------------------------------------------------------------------------- /medium/71.简化路径/71.简化路径.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=71 lang=python3 3 | # 4 | # [71] 简化路径 5 | # 6 | 7 | 8 | class Solution: 9 | def simplifyPath(self, path: str) -> str: 10 | stack = [''] 11 | arr = path.split('/') 12 | for s in arr: 13 | if s == '' or s == '.': 14 | continue 15 | elif s == '..': 16 | stack.pop() 17 | if len(stack) == 0: 18 | stack.append('') 19 | else: 20 | stack.append(s) 21 | return '/' if len(stack) == 1 else '/'.join(stack) 22 | -------------------------------------------------------------------------------- /medium/75.颜色分类/75.颜色分类.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=75 lang=javascript 3 | * 4 | * [75] 颜色分类 5 | */ 6 | /** 7 | * @param {number[]} nums 8 | * @return {void} Do not return anything, modify nums in-place instead. 9 | */ 10 | var sortColors = function(nums) { 11 | let [i, j, k] = [0, 0, nums.length - 1] 12 | while (j <= k) { 13 | if (nums[j] === 1) { 14 | j++ 15 | } else if (nums[j] === 0) { 16 | ;[nums[i], nums[j]] = [nums[j], nums[i]] 17 | i++ 18 | j++ 19 | } else { 20 | ;[nums[j], nums[k]] = [nums[k], nums[j]] 21 | k-- 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /medium/71.简化路径/71.简化路径.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=71 lang=golang 3 | * 4 | * [71] 简化路径 5 | */ 6 | import "strings" 7 | 8 | func simplifyPath(path string) string { 9 | stack := []string{""} 10 | arr := strings.Split(path, "/") 11 | for _, str := range arr { 12 | if str == "" || str == "." { 13 | continue 14 | } else if str == ".." { 15 | stack = stack[:len(stack)-1] 16 | if len(stack) == 0 { 17 | stack = []string{""} 18 | } 19 | } else { 20 | stack = append(stack, str) 21 | } 22 | } 23 | if len(stack) == 1 { 24 | return "/" 25 | } 26 | return strings.Join(stack, "/") 27 | } 28 | 29 | -------------------------------------------------------------------------------- /medium/71.简化路径/71.简化路径.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=71 lang=javascript 3 | * 4 | * [71] 简化路径 5 | */ 6 | /** 7 | * @param {string} path 8 | * @return {string} 9 | */ 10 | var simplifyPath = function(path) { 11 | const stack = [''] 12 | const arr = path.split('/') 13 | for (const str of arr) { 14 | if (str === '' || str === '.') { 15 | continue 16 | } else if (str === '..') { 17 | stack.pop() 18 | if (!stack.length) { 19 | stack.push('') 20 | } 21 | } else { 22 | stack.push(str) 23 | } 24 | } 25 | return stack.length === 1 ? '/' : stack.join('/') 26 | } 27 | -------------------------------------------------------------------------------- /medium/60.第k个排列/60.第k个排列.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=60 lang=golang 3 | * 4 | * [60] 第k个排列 5 | */ 6 | import "strconv" 7 | 8 | func getPermutation(n int, k int) string { 9 | arr := []int{} 10 | fac := 1 11 | for i := 1; i <= n; i++ { 12 | arr = append(arr, i) 13 | fac *= i 14 | } 15 | res := []int{} 16 | pos := k - 1 17 | for j := n; j > 0; j-- { 18 | fac /= j 19 | index := pos / fac 20 | res = append(res, arr[index]) 21 | arr = append(arr[:index], arr[index+1:]...) 22 | pos %= fac 23 | } 24 | result := `` 25 | for _, n := range res { 26 | result += strconv.Itoa(n) 27 | } 28 | return result 29 | } 30 | 31 | -------------------------------------------------------------------------------- /easy/258.各位相加/258.各位相加.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=258 lang=golang 3 | * 4 | * [258] 各位相加 5 | * 6 | * https://leetcode-cn.com/problems/add-digits/description/ 7 | * 8 | * algorithms 9 | * Easy (62.30%) 10 | * Total Accepted: 10.1K 11 | * Total Submissions: 16.1K 12 | * Testcase Example: '38' 13 | * 14 | * 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 38 19 | * 输出: 2 20 | * 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。 21 | * 22 | * 23 | * 进阶: 24 | * 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? 25 | * 26 | */ 27 | func addDigits(num int) int { 28 | return 1+((num-1)%9) 29 | } 30 | 31 | -------------------------------------------------------------------------------- /medium/75.颜色分类/75.颜色分类.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=75 lang=python3 3 | # 4 | # [75] 颜色分类 5 | # 6 | 7 | 8 | class Solution: 9 | def sortColors(self, nums: List[int]) -> None: 10 | """ 11 | Do not return anything, modify nums in-place instead. 12 | """ 13 | i, j, k = 0, 0, len(nums) - 1 14 | while j <= k: 15 | if nums[j] == 0: 16 | nums[i], nums[j] = nums[j], nums[i] 17 | i += 1 18 | j += 1 19 | elif nums[j] == 2: 20 | nums[j], nums[k] = nums[k], nums[j] 21 | k -= 1 22 | else: 23 | j += 1 24 | -------------------------------------------------------------------------------- /medium/77.组合/77.组合.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=77 lang=golang 3 | * 4 | * [77] 组合 5 | */ 6 | func combine(n int, k int) [][]int { 7 | var res [][]int 8 | if k > n || k == 0 { 9 | return res 10 | } 11 | if k == 1 { 12 | for i := 0; i < n; i++ { 13 | res = append(res, []int{i + 1}) 14 | } 15 | return res 16 | } 17 | if k == n { 18 | var arr []int 19 | for i := 0; i < n; i++ { 20 | arr = append(arr, i+1) 21 | } 22 | res = append(res, arr) 23 | return res 24 | } 25 | res = combine(n-1, k) 26 | for _, item := range combine(n-1, k-1) { 27 | item = append(item, n) 28 | res = append(res, item) 29 | } 30 | return res 31 | } 32 | 33 | -------------------------------------------------------------------------------- /easy/231.2-的幂/231.2-的幂.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=231 lang=golang 3 | * 4 | * [231] 2的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-two/description/ 7 | * 8 | * algorithms 9 | * Easy (43.83%) 10 | * Total Accepted: 12.1K 11 | * Total Submissions: 27.5K 12 | * Testcase Example: '1' 13 | * 14 | * 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 1 19 | * 输出: true 20 | * 解释: 20 = 1 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 16 25 | * 输出: true 26 | * 解释: 24 = 16 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 218 31 | * 输出: false 32 | * 33 | */ 34 | func isPowerOfTwo(n int) bool { 35 | return n > 0 && (n&(n-1)) == 0 36 | } 37 | 38 | -------------------------------------------------------------------------------- /medium/74.搜索二维矩阵/74.搜索二维矩阵.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=74 lang=python3 3 | # 4 | # [74] 搜索二维矩阵 5 | # 6 | 7 | 8 | class Solution: 9 | def searchMatrix(self, matrix, target: int) -> bool: 10 | if len(matrix) == 0: 11 | return False 12 | x, y = len(matrix), len(matrix[0]) 13 | l, r = 0, x * y - 1 14 | while l <= r: 15 | mid = (l + r) // 2 16 | ix, iy = mid // y, mid % y 17 | if matrix[ix][iy] == target: 18 | return True 19 | elif matrix[ix][iy] > target: 20 | r = mid - 1 21 | else: 22 | l = mid + 1 23 | return False 24 | -------------------------------------------------------------------------------- /easy/231.2-的幂/231.2-的幂.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=231 lang=python3 3 | # 4 | # [231] 2的幂 5 | # 6 | # https://leetcode-cn.com/problems/power-of-two/description/ 7 | # 8 | # algorithms 9 | # Easy (43.83%) 10 | # Total Accepted: 12.1K 11 | # Total Submissions: 27.5K 12 | # Testcase Example: '1' 13 | # 14 | # 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 1 19 | # 输出: true 20 | # 解释: 20 = 1 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 16 25 | # 输出: true 26 | # 解释: 24 = 16 27 | # 28 | # 示例 3: 29 | # 30 | # 输入: 218 31 | # 输出: false 32 | # 33 | # 34 | 35 | 36 | class Solution: 37 | def isPowerOfTwo(self, n: int) -> bool: 38 | return n > 0 and not (n & (n - 1)) 39 | -------------------------------------------------------------------------------- /easy/292.nim游戏/292.nim游戏.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=292 lang=golang 3 | * 4 | * [292] Nim游戏 5 | * 6 | * https://leetcode-cn.com/problems/nim-game/description/ 7 | * 8 | * algorithms 9 | * Easy (65.84%) 10 | * Total Accepted: 10.6K 11 | * Total Submissions: 16K 12 | * Testcase Example: '4' 13 | * 14 | * 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 15 | * 16 | * 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 17 | * 18 | * 示例: 19 | * 20 | * 输入: 4 21 | * 输出: false 22 | * 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛; 23 | * 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。 24 | * 25 | * 26 | */ 27 | func canWinNim(n int) bool { 28 | return (n & 3) != 0 29 | } 30 | 31 | -------------------------------------------------------------------------------- /easy/258.各位相加/258.各位相加.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=258 lang=python3 3 | # 4 | # [258] 各位相加 5 | # 6 | # https://leetcode-cn.com/problems/add-digits/description/ 7 | # 8 | # algorithms 9 | # Easy (62.30%) 10 | # Total Accepted: 10.1K 11 | # Total Submissions: 16.1K 12 | # Testcase Example: '38' 13 | # 14 | # 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 15 | # 16 | # 示例: 17 | # 18 | # 输入: 38 19 | # 输出: 2 20 | # 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。 21 | # 22 | # 23 | # 进阶: 24 | # 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? 25 | # 26 | # 27 | 28 | 29 | class Solution: 30 | def addDigits(self, num: int) -> int: 31 | if num == 0: 32 | return 0 33 | return 1 + ((num - 1) % 9) 34 | -------------------------------------------------------------------------------- /easy/292.nim游戏/292.nim游戏.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=292 lang=python3 3 | # 4 | # [292] Nim游戏 5 | # 6 | # https://leetcode-cn.com/problems/nim-game/description/ 7 | # 8 | # algorithms 9 | # Easy (65.84%) 10 | # Total Accepted: 10.6K 11 | # Total Submissions: 16K 12 | # Testcase Example: '4' 13 | # 14 | # 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 15 | # 16 | # 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 17 | # 18 | # 示例: 19 | # 20 | # 输入: 4 21 | # 输出: false 22 | # 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛; 23 | # 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。 24 | # 25 | # 26 | # 27 | 28 | 29 | class Solution: 30 | def canWinNim(self, n: int) -> bool: 31 | return (n & 3) != 0 32 | -------------------------------------------------------------------------------- /easy/258.各位相加/258.各位相加.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=258 lang=javascript 3 | * 4 | * [258] 各位相加 5 | * 6 | * https://leetcode-cn.com/problems/add-digits/description/ 7 | * 8 | * algorithms 9 | * Easy (62.03%) 10 | * Total Accepted: 10K 11 | * Total Submissions: 16.1K 12 | * Testcase Example: '38' 13 | * 14 | * 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 38 19 | * 输出: 2 20 | * 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。 21 | * 22 | * 23 | * 进阶: 24 | * 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? 25 | * 26 | */ 27 | /** 28 | * @param {number} num 29 | * @return {number} 30 | */ 31 | var addDigits = function(num) { 32 | return 1 + ((num - 1) % 9) // 数根公式 33 | } 34 | -------------------------------------------------------------------------------- /easy/371.两整数之和/371.两整数之和.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=371 lang=golang 3 | * 4 | * [371] 两整数之和 5 | * 6 | * https://leetcode-cn.com/problems/sum-of-two-integers/description/ 7 | * 8 | * algorithms 9 | * Easy (57.99%) 10 | * Total Accepted: 7.4K 11 | * Total Submissions: 12.9K 12 | * Testcase Example: '1\n2' 13 | * 14 | * 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: a = 1, b = 2 19 | * 输出: 3 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: a = -2, b = 3 25 | * 输出: 1 26 | * 27 | */ 28 | func getSum(a int, b int) int { 29 | sum := a ^ b 30 | carry := (a & b) << 1 31 | if carry != 0 { 32 | return getSum(sum, carry) 33 | } 34 | return sum 35 | } 36 | 37 | -------------------------------------------------------------------------------- /easy/342.4-的幂/342.4-的幂.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=342 lang=golang 3 | * 4 | * [342] 4的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-four/description/ 7 | * 8 | * algorithms 9 | * Easy (44.11%) 10 | * Total Accepted: 5.5K 11 | * Total Submissions: 12.5K 12 | * Testcase Example: '16' 13 | * 14 | * 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 16 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 5 25 | * 输出: false 26 | * 27 | * 进阶: 28 | * 你能不使用循环或者递归来完成本题吗? 29 | * 30 | */ 31 | func isPowerOfFour(num int) bool { 32 | if num == 0 { 33 | return false 34 | } 35 | for num%4 == 0 { 36 | num /= 4 37 | } 38 | return num == 1 39 | } 40 | 41 | -------------------------------------------------------------------------------- /easy/172.阶乘后的零/172.阶乘后的零.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=172 lang=golang 3 | * 4 | * [172] 阶乘后的零 5 | * 6 | * https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/ 7 | * 8 | * algorithms 9 | * Easy (36.61%) 10 | * Total Accepted: 7.8K 11 | * Total Submissions: 21.1K 12 | * Testcase Example: '3' 13 | * 14 | * 给定一个整数 n,返回 n! 结果尾数中零的数量。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 3 19 | * 输出: 0 20 | * 解释: 3! = 6, 尾数中没有零。 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 5 25 | * 输出: 1 26 | * 解释: 5! = 120, 尾数中有 1 个零. 27 | * 28 | * 说明: 你算法的时间复杂度应为 O(log n) 。 29 | * 30 | */ 31 | func trailingZeroes(n int) int { 32 | res := 0 33 | for n >= 1 { 34 | n /= 5 35 | res += n 36 | } 37 | return res 38 | } 39 | 40 | -------------------------------------------------------------------------------- /easy/342.4-的幂/342.4-的幂.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=342 lang=python3 3 | # 4 | # [342] 4的幂 5 | # 6 | # https://leetcode-cn.com/problems/power-of-four/description/ 7 | # 8 | # algorithms 9 | # Easy (44.11%) 10 | # Total Accepted: 5.5K 11 | # Total Submissions: 12.5K 12 | # Testcase Example: '16' 13 | # 14 | # 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 16 19 | # 输出: true 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 5 25 | # 输出: false 26 | # 27 | # 进阶: 28 | # 你能不使用循环或者递归来完成本题吗? 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def isPowerOfFour(self, num: int) -> bool: 35 | if ((num - 1) & num) != 0 or num < 0: 36 | return False 37 | return num & 0x555555555 != 0 38 | -------------------------------------------------------------------------------- /medium/64.最小路径和/64.最小路径和.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=64 lang=golang 3 | * 4 | * [64] 最小路径和 5 | */ 6 | func minPathSum(grid [][]int) int { 7 | x, y := len(grid), len(grid[0]) 8 | dp := make([][]int, x) 9 | for i := 0; i < x; i++ { 10 | dp[i] = make([]int, y) 11 | for j := 0; j < y; j++ { 12 | if i == 0 && j == 0 { 13 | dp[i][j] = grid[i][j] 14 | } else if i == 0 { 15 | dp[i][j] = dp[i][j-1] + grid[i][j] 16 | } else if j == 0 { 17 | dp[i][j] = dp[i-1][j] + grid[i][j] 18 | } else { 19 | dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1]) 20 | } 21 | } 22 | } 23 | return dp[x-1][y-1] 24 | } 25 | 26 | func min(a, b int) int { 27 | if a < b { 28 | return a 29 | } 30 | return b 31 | } 32 | 33 | -------------------------------------------------------------------------------- /medium/61.旋转链表/61.旋转链表.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=61 lang=golang 3 | * 4 | * [61] 旋转链表 5 | */ 6 | /** 7 | * Definition for singly-linked list. 8 | * type ListNode struct { 9 | * Val int 10 | * Next *ListNode 11 | * } 12 | */ 13 | func rotateRight(head *ListNode, k int) *ListNode { 14 | if head == nil || k == 0 { 15 | return head 16 | } 17 | cursor, length := head, 1 18 | var tail *ListNode 19 | for cursor.Next != nil { 20 | cursor = cursor.Next 21 | length++ 22 | } 23 | steps := length - (k % length) 24 | tail = cursor 25 | cursor.Next = head 26 | cursor = head 27 | for i := 0; i < steps; i++ { 28 | cursor = cursor.Next 29 | tail = tail.Next 30 | } 31 | tail.Next = nil 32 | return cursor 33 | } 34 | 35 | -------------------------------------------------------------------------------- /easy/434.字符串中的单词数/434.字符串中的单词数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=434 lang=javascript 3 | * 4 | * [434] 字符串中的单词数 5 | * 6 | * https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/ 7 | * 8 | * algorithms 9 | * Easy (28.93%) 10 | * Total Accepted: 3.9K 11 | * Total Submissions: 13.4K 12 | * Testcase Example: '"Hello, my name is John"' 13 | * 14 | * 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。 15 | * 16 | * 请注意,你可以假定字符串里不包括任何不可打印的字符。 17 | * 18 | * 示例: 19 | * 20 | * 输入: "Hello, my name is John" 21 | * 输出: 5 22 | * 23 | * 24 | */ 25 | /** 26 | * @param {string} s 27 | * @return {number} 28 | */ 29 | var countSegments = function(s) { 30 | if (!s.trim()) return 0 31 | return s.split(' ').filter(item => item).length 32 | } 33 | -------------------------------------------------------------------------------- /easy/136.只出现一次的数字/136.只出现一次的数字.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=136 lang=golang 3 | * 4 | * [136] 只出现一次的数字 5 | * 6 | * https://leetcode-cn.com/problems/single-number/description/ 7 | * 8 | * algorithms 9 | * Easy (57.57%) 10 | * Total Accepted: 39.5K 11 | * Total Submissions: 68.6K 12 | * Testcase Example: '[2,2,1]' 13 | * 14 | * 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 15 | * 16 | * 说明: 17 | * 18 | * 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [2,2,1] 23 | * 输出: 1 24 | * 25 | * 26 | * 示例 2: 27 | * 28 | * 输入: [4,1,2,1,2] 29 | * 输出: 4 30 | * 31 | */ 32 | func singleNumber(nums []int) int { 33 | res := 0 34 | for _, n := range nums { 35 | res ^= n 36 | } 37 | return res 38 | } 39 | 40 | -------------------------------------------------------------------------------- /medium/73.矩阵置零/73.矩阵置零.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=73 lang=javascript 3 | * 4 | * [73] 矩阵置零 5 | */ 6 | /** 7 | * @param {number[][]} matrix 8 | * @return {void} Do not return anything, modify matrix in-place instead. 9 | */ 10 | var setZeroes = function(matrix) { 11 | const xSet = new Set() 12 | const ySet = new Set() 13 | for (let i = 0; i < matrix.length; i++) { 14 | for (let j = 0; j < matrix[i].length; j++) { 15 | if (matrix[i][j] === 0) { 16 | xSet.add(i) 17 | ySet.add(j) 18 | } 19 | } 20 | } 21 | for (let n of xSet.values()) { 22 | matrix[n].fill(0) 23 | } 24 | for (let n of ySet.values()) { 25 | for (let i = 0; i < matrix.length; i++) { 26 | matrix[i][n] = 0 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /easy/292.nim游戏/292.nim游戏.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=292 lang=javascript 3 | * 4 | * [292] Nim游戏 5 | * 6 | * https://leetcode-cn.com/problems/nim-game/description/ 7 | * 8 | * algorithms 9 | * Easy (65.84%) 10 | * Total Accepted: 10.6K 11 | * Total Submissions: 16K 12 | * Testcase Example: '4' 13 | * 14 | * 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 15 | * 16 | * 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 17 | * 18 | * 示例: 19 | * 20 | * 输入: 4 21 | * 输出: false 22 | * 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛; 23 | * 因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。 24 | * 25 | * 26 | */ 27 | /** 28 | * @param {number} n 29 | * @return {boolean} 30 | */ 31 | var canWinNim = function(n) { 32 | return (n & 3) !== 0 33 | } 34 | -------------------------------------------------------------------------------- /easy/172.阶乘后的零/172.阶乘后的零.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=172 lang=python3 3 | # 4 | # [172] 阶乘后的零 5 | # 6 | # https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/ 7 | # 8 | # algorithms 9 | # Easy (36.61%) 10 | # Total Accepted: 7.8K 11 | # Total Submissions: 21.1K 12 | # Testcase Example: '3' 13 | # 14 | # 给定一个整数 n,返回 n! 结果尾数中零的数量。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 3 19 | # 输出: 0 20 | # 解释: 3! = 6, 尾数中没有零。 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 5 25 | # 输出: 1 26 | # 解释: 5! = 120, 尾数中有 1 个零. 27 | # 28 | # 说明: 你算法的时间复杂度应为 O(log n) 。 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def trailingZeroes(self, n: int) -> int: 35 | res = 0 36 | while n >= 1: 37 | n //= 5 38 | res += n 39 | return res 40 | -------------------------------------------------------------------------------- /easy/136.只出现一次的数字/136.只出现一次的数字.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=136 lang=python3 3 | # 4 | # [136] 只出现一次的数字 5 | # 6 | # https://leetcode-cn.com/problems/single-number/description/ 7 | # 8 | # algorithms 9 | # Easy (57.30%) 10 | # Total Accepted: 39.3K 11 | # Total Submissions: 68.4K 12 | # Testcase Example: '[2,2,1]' 13 | # 14 | # 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 15 | # 16 | # 说明: 17 | # 18 | # 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: [2,2,1] 23 | # 输出: 1 24 | # 25 | # 26 | # 示例 2: 27 | # 28 | # 输入: [4,1,2,1,2] 29 | # 输出: 4 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def singleNumber(self, nums: 'List[int]') -> 'int': 36 | res = 0 37 | for n in nums: 38 | res ^= n 39 | return res 40 | -------------------------------------------------------------------------------- /easy/453.最小移动次数使数组元素相等/453.最小移动次数使数组元素相等.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=453 lang=python3 3 | # 4 | # [453] 最小移动次数使数组元素相等 5 | # 6 | # https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/description/ 7 | # 8 | # algorithms 9 | # Easy (49.41%) 10 | # Total Accepted: 2.7K 11 | # Total Submissions: 5.4K 12 | # Testcase Example: '[1,2,3]' 13 | # 14 | # 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。 15 | # 16 | # 示例: 17 | # 18 | # 19 | # 输入: 20 | # [1,2,3] 21 | # 22 | # 输出: 23 | # 3 24 | # 25 | # 解释: 26 | # 只需要3次移动(注意每次移动会增加两个元素的值): 27 | # 28 | # [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 29 | # 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def minMoves(self, nums: List[int]) -> int: 36 | return sum(nums) - len(nums) * min(nums) 37 | -------------------------------------------------------------------------------- /easy/125.验证回文串/125.验证回文串.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=125 lang=python3 3 | # 4 | # [125] 验证回文串 5 | # 6 | # https://leetcode-cn.com/problems/valid-palindrome/description/ 7 | # 8 | # algorithms 9 | # Easy (37.77%) 10 | # Total Accepted: 22.3K 11 | # Total Submissions: 58.9K 12 | # Testcase Example: '"A man, a plan, a canal: Panama"' 13 | # 14 | # 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 15 | # 16 | # 说明:本题中,我们将空字符串定义为有效的回文串。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: "A man, a plan, a canal: Panama" 21 | # 输出: true 22 | # 23 | # 24 | # 示例 2: 25 | # 26 | # 输入: "race a car" 27 | # 输出: false 28 | # 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def isPalindrome(self, s: 'str') -> 'bool': 35 | s = ''.join(filter(str.isalnum, s)).lower() 36 | return s == s[::-1] 37 | -------------------------------------------------------------------------------- /easy/342.4-的幂/342.4-的幂.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=342 lang=javascript 3 | * 4 | * [342] 4的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-four/description/ 7 | * 8 | * algorithms 9 | * Easy (44.11%) 10 | * Total Accepted: 5.5K 11 | * Total Submissions: 12.5K 12 | * Testcase Example: '16' 13 | * 14 | * 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 16 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 5 25 | * 输出: false 26 | * 27 | * 进阶: 28 | * 你能不使用循环或者递归来完成本题吗? 29 | * 30 | */ 31 | /** 32 | * @param {number} num 33 | * @return {boolean} 34 | */ 35 | var isPowerOfFour = function(num) { 36 | if (num < 0 || num & (num - 1)) { 37 | return false 38 | } 39 | return num & 0x55555555 40 | } 41 | -------------------------------------------------------------------------------- /medium/63.不同路径-ii/63.不同路径-ii.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=63 lang=golang 3 | * 4 | * [63] 不同路径 II 5 | */ 6 | func uniquePathsWithObstacles(obstacleGrid [][]int) int { 7 | if len(obstacleGrid) == 0 { 8 | return 0 9 | } 10 | x, y := len(obstacleGrid), len(obstacleGrid[0]) 11 | dp := make([][]int, x) 12 | for i := 0; i < x; i++ { 13 | dp[i] = make([]int, y) 14 | for j := 0; j < y; j++ { 15 | if obstacleGrid[i][j] == 1 { 16 | dp[i][j] = 0 17 | } else { 18 | if i == 0 && j == 0 { 19 | dp[i][j] = 1 20 | } else if i == 0 { 21 | dp[i][j] = dp[i][j-1] 22 | } else if j == 0 { 23 | dp[i][j] = dp[i-1][j] 24 | } else { 25 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 26 | } 27 | } 28 | } 29 | } 30 | return dp[x-1][y-1] 31 | } 32 | 33 | -------------------------------------------------------------------------------- /medium/64.最小路径和/64.最小路径和.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=64 lang=javascript 3 | * 4 | * [64] 最小路径和 5 | */ 6 | /** 7 | * @param {number[][]} grid 8 | * @return {number} 9 | */ 10 | var minPathSum = function(grid) { 11 | const x = grid.length 12 | const y = grid[0].length 13 | const dp = [] 14 | for (let i = 0; i < x; i++) { 15 | dp[i] = [] 16 | for (let j = 0; j < y; j++) { 17 | if (i === 0 && j === 0) { 18 | dp[i][j] = grid[i][j] 19 | } else if (i === 0) { 20 | dp[i][j] = dp[i][j - 1] + grid[i][j] 21 | } else if (j === 0) { 22 | dp[i][j] = dp[i - 1][j] + grid[i][j] 23 | } else { 24 | dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]) 25 | } 26 | } 27 | } 28 | return dp[x - 1][y - 1] 29 | } 30 | -------------------------------------------------------------------------------- /easy/268.缺失数字/268.缺失数字.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=268 lang=golang 3 | * 4 | * [268] 缺失数字 5 | * 6 | * https://leetcode-cn.com/problems/missing-number/description/ 7 | * 8 | * algorithms 9 | * Easy (47.95%) 10 | * Total Accepted: 13.7K 11 | * Total Submissions: 28.6K 12 | * Testcase Example: '[3,0,1]' 13 | * 14 | * 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: [3,0,1] 19 | * 输出: 2 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: [9,6,4,2,3,5,7,0,1] 25 | * 输出: 8 26 | * 27 | * 28 | * 说明: 29 | * 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现? 30 | * 31 | */ 32 | func missingNumber(nums []int) int { 33 | l := len(nums) 34 | sum := l*(l+1)/2 35 | for _, n := range nums { 36 | sum -= n 37 | } 38 | return sum 39 | } 40 | 41 | -------------------------------------------------------------------------------- /medium/64.最小路径和/64.最小路径和.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=64 lang=python3 3 | # 4 | # [64] 最小路径和 5 | # 6 | 7 | 8 | class Solution: 9 | def minPathSum(self, grid: List[List[int]]) -> int: 10 | x = len(grid) 11 | y = len(grid[0]) 12 | dp = [[]] * x 13 | for i in range(x): 14 | dp[i] = [0] * y 15 | for j in range(y): 16 | if i == 0 and j == 0: 17 | dp[i][j] = grid[i][j] 18 | elif i == 0: 19 | dp[i][j] = dp[i][j - 1] + grid[i][j] 20 | elif j == 0: 21 | dp[i][j] = dp[i - 1][j] + grid[i][j] 22 | else: 23 | dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]) 24 | return dp[x - 1][y - 1] 25 | -------------------------------------------------------------------------------- /easy/136.只出现一次的数字/136.只出现一次的数字.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=136 lang=javascript 3 | * 4 | * [136] 只出现一次的数字 5 | * 6 | * https://leetcode-cn.com/problems/single-number/description/ 7 | * 8 | * algorithms 9 | * Easy (57.30%) 10 | * Total Accepted: 39.3K 11 | * Total Submissions: 68.4K 12 | * Testcase Example: '[2,2,1]' 13 | * 14 | * 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 15 | * 16 | * 说明: 17 | * 18 | * 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [2,2,1] 23 | * 输出: 1 24 | * 25 | * 26 | * 示例 2: 27 | * 28 | * 输入: [4,1,2,1,2] 29 | * 输出: 4 30 | * 31 | */ 32 | /** 33 | * @param {number[]} nums 34 | * @return {number} 35 | */ 36 | var singleNumber = function(nums) { 37 | return nums.reduce((res, item) => res ^ item, 0) 38 | } 39 | -------------------------------------------------------------------------------- /easy/371.两整数之和/371.两整数之和.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=371 lang=javascript 3 | * 4 | * [371] 两整数之和 5 | * 6 | * https://leetcode-cn.com/problems/sum-of-two-integers/description/ 7 | * 8 | * algorithms 9 | * Easy (57.99%) 10 | * Total Accepted: 7.4K 11 | * Total Submissions: 12.9K 12 | * Testcase Example: '1\n2' 13 | * 14 | * 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: a = 1, b = 2 19 | * 输出: 3 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: a = -2, b = 3 25 | * 输出: 1 26 | * 27 | */ 28 | /** 29 | * @param {number} a 30 | * @param {number} b 31 | * @return {number} 32 | */ 33 | var getSum = function(a, b) { 34 | const sum = a ^ b 35 | const carry = (a & b) << 1 36 | if (carry != 0) return getSum(sum, carry) 37 | return sum 38 | } 39 | -------------------------------------------------------------------------------- /medium/201.数字范围按位与.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=201 lang=python3 3 | # 4 | # [201] 数字范围按位与 5 | # 6 | # https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/description/ 7 | # 8 | # algorithms 9 | # Medium (40.76%) 10 | # Likes: 39 11 | # Dislikes: 0 12 | # Total Accepted: 3.2K 13 | # Total Submissions: 7.7K 14 | # Testcase Example: '5\n7' 15 | # 16 | # 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。 17 | # 18 | # 示例 1:  19 | # 20 | # 输入: [5,7] 21 | # 输出: 4 22 | # 23 | # 示例 2: 24 | # 25 | # 输入: [0,1] 26 | # 输出: 0 27 | # 28 | # 29 | 30 | 31 | class Solution: 32 | def rangeBitwiseAnd(self, m: int, n: int) -> int: 33 | count = 0 34 | while m != n: 35 | m >>= 1 36 | n >>= 1 37 | count += 1 38 | return m << count 39 | -------------------------------------------------------------------------------- /medium/79.单词搜索/79.单词搜索.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=79 lang=golang 3 | * 4 | * [79] 单词搜索 5 | */ 6 | func exist(board [][]byte, word string) bool { 7 | for i := range board { 8 | for j := range board[i] { 9 | if dfs(board, i, j, 0, word) { 10 | return true 11 | } 12 | } 13 | } 14 | return false 15 | } 16 | 17 | func dfs(board [][]byte, i, j, k int, word string) bool { 18 | if k == len(word) { 19 | return true 20 | } 21 | if i < 0 || i >= len(board) || j < 0 || j >= len(board[0]) || board[i][j] != word[k] { 22 | return false 23 | } 24 | s := board[i][j] 25 | board[i][j] = '*' 26 | res := dfs(board, i+1, j, k+1, word) || 27 | dfs(board, i-1, j, k+1, word) || 28 | dfs(board, i, j+1, k+1, word) || 29 | dfs(board, i, j-1, k+1, word) 30 | board[i][j] = s 31 | return res 32 | } 33 | -------------------------------------------------------------------------------- /easy/268.缺失数字/268.缺失数字.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=268 lang=python3 3 | # 4 | # [268] 缺失数字 5 | # 6 | # https://leetcode-cn.com/problems/missing-number/description/ 7 | # 8 | # algorithms 9 | # Easy (47.95%) 10 | # Total Accepted: 13.7K 11 | # Total Submissions: 28.6K 12 | # Testcase Example: '[3,0,1]' 13 | # 14 | # 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: [3,0,1] 19 | # 输出: 2 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: [9,6,4,2,3,5,7,0,1] 25 | # 输出: 8 26 | # 27 | # 28 | # 说明: 29 | # 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现? 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def missingNumber(self, nums: List[int]) -> int: 36 | l = len(nums) 37 | sum = l * (l + 1) // 2 38 | for n in nums: 39 | sum -= n 40 | return sum 41 | -------------------------------------------------------------------------------- /easy/371.两整数之和/371.两整数之和.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=371 lang=python3 3 | # 4 | # [371] 两整数之和 5 | # 6 | # https://leetcode-cn.com/problems/sum-of-two-integers/description/ 7 | # 8 | # algorithms 9 | # Easy (57.99%) 10 | # Total Accepted: 7.4K 11 | # Total Submissions: 12.9K 12 | # Testcase Example: '1\n2' 13 | # 14 | # 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: a = 1, b = 2 19 | # 输出: 3 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: a = -2, b = 3 25 | # 输出: 1 26 | # 27 | # 28 | 29 | 30 | class Solution: 31 | def getSum(self, a: int, b: int) -> int: 32 | MAX_INT = 0x7FFFFFFF 33 | MASK = 0x100000000 34 | while b: 35 | a, b = (a ^ b) % MASK, ((a & b) << 1) % MASK 36 | return a if a <= MAX_INT else ~((a & MAX_INT) ^ MAX_INT) 37 | -------------------------------------------------------------------------------- /easy/172.阶乘后的零/172.阶乘后的零.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=172 lang=javascript 3 | * 4 | * [172] 阶乘后的零 5 | * 6 | * https://leetcode-cn.com/problems/factorial-trailing-zeroes/description/ 7 | * 8 | * algorithms 9 | * Easy (36.61%) 10 | * Total Accepted: 7.8K 11 | * Total Submissions: 21.1K 12 | * Testcase Example: '3' 13 | * 14 | * 给定一个整数 n,返回 n! 结果尾数中零的数量。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 3 19 | * 输出: 0 20 | * 解释: 3! = 6, 尾数中没有零。 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 5 25 | * 输出: 1 26 | * 解释: 5! = 120, 尾数中有 1 个零. 27 | * 28 | * 说明: 你算法的时间复杂度应为 O(log n) 。 29 | * 30 | */ 31 | /** 32 | * @param {number} n 33 | * @return {number} 34 | */ 35 | var trailingZeroes = function(n) { 36 | let res = 0 37 | while (n >= 1) { 38 | n = (n / 5) | 0 39 | res += n 40 | } 41 | return res 42 | } 43 | -------------------------------------------------------------------------------- /easy/326.3-的幂/326.3-的幂.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=326 lang=golang 3 | * 4 | * [326] 3的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-three/description/ 7 | * 8 | * algorithms 9 | * Easy (42.27%) 10 | * Total Accepted: 12.9K 11 | * Total Submissions: 30.4K 12 | * Testcase Example: '27' 13 | * 14 | * 给定一个整数,写一个函数来判断它是否是 3 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 27 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 0 25 | * 输出: false 26 | * 27 | * 示例 3: 28 | * 29 | * 输入: 9 30 | * 输出: true 31 | * 32 | * 示例 4: 33 | * 34 | * 输入: 45 35 | * 输出: false 36 | * 37 | * 进阶: 38 | * 你能不使用循环或者递归来完成本题吗? 39 | * 40 | */ 41 | func isPowerOfThree(n int) bool { 42 | if n == 0 { 43 | return false 44 | } 45 | for n%3 == 0 { 46 | n /= 3 47 | } 48 | return n == 1 49 | } 50 | 51 | -------------------------------------------------------------------------------- /medium/86.分隔链表/86.分隔链表.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=86 lang=python3 3 | # 4 | # [86] 分隔链表 5 | # 6 | # Definition for singly-linked list. 7 | # class ListNode: 8 | # def __init__(self, x): 9 | # self.val = x 10 | # self.next = None 11 | 12 | 13 | class Solution: 14 | def partition(self, head: ListNode, x: int) -> ListNode: 15 | small, bigHead = ListNode(-1000), ListNode(-1000) 16 | res, big = small, bigHead 17 | while head != None: 18 | if head.val < x: 19 | small.next = head 20 | small = small.next 21 | else: 22 | bigHead.next = head 23 | bigHead = bigHead.next 24 | head = head.next 25 | small.next = big.next 26 | bigHead.next = None 27 | return res.next 28 | -------------------------------------------------------------------------------- /easy/204.计数质数/204.计数质数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=204 lang=python3 3 | # 4 | # [204] 计数质数 5 | # 6 | # https://leetcode-cn.com/problems/count-primes/description/ 7 | # 8 | # algorithms 9 | # Easy (25.80%) 10 | # Total Accepted: 12.4K 11 | # Total Submissions: 47.4K 12 | # Testcase Example: '10' 13 | # 14 | # 统计所有小于非负整数 n 的质数的数量。 15 | # 16 | # 示例: 17 | # 18 | # 输入: 10 19 | # 输出: 4 20 | # 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 21 | # 22 | # 23 | # 24 | 25 | 26 | class Solution: 27 | def countPrimes(self, n: int) -> int: 28 | if n < 3: 29 | return 0 30 | primes = [True] * n 31 | primes[0] = primes[1] = False 32 | for i in range(2, int(n ** 0.5) + 1): 33 | if primes[i]: 34 | primes[i * i: n: i] = [False] * len(primes[i * i: n: i]) 35 | return sum(primes) 36 | -------------------------------------------------------------------------------- /easy/461.汉明距离/461.汉明距离.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=461 lang=golang 3 | * 4 | * [461] 汉明距离 5 | * 6 | * https://leetcode-cn.com/problems/hamming-distance/description/ 7 | * 8 | * algorithms 9 | * Easy (68.23%) 10 | * Total Accepted: 14.8K 11 | * Total Submissions: 21.7K 12 | * Testcase Example: '1\n4' 13 | * 14 | * 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 15 | * 16 | * 给出两个整数 x 和 y,计算它们之间的汉明距离。 17 | * 18 | * 注意: 19 | * 0 ≤ x, y < 2^31. 20 | * 21 | * 示例: 22 | * 23 | * 24 | * 输入: x = 1, y = 4 25 | * 26 | * 输出: 2 27 | * 28 | * 解释: 29 | * 1 (0 0 0 1) 30 | * 4 (0 1 0 0) 31 | * ⁠ ↑ ↑ 32 | * 33 | * 上面的箭头指出了对应二进制位不同的位置。 34 | * 35 | * 36 | */ 37 | func hammingDistance(x int, y int) int { 38 | ans, num := 0, x^y 39 | for num != 0 { 40 | num &= num - 1 41 | ans++ 42 | } 43 | return ans 44 | } 45 | 46 | -------------------------------------------------------------------------------- /easy/326.3-的幂/326.3-的幂.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=326 lang=python3 3 | # 4 | # [326] 3的幂 5 | # 6 | # https://leetcode-cn.com/problems/power-of-three/description/ 7 | # 8 | # algorithms 9 | # Easy (42.27%) 10 | # Total Accepted: 12.9K 11 | # Total Submissions: 30.4K 12 | # Testcase Example: '27' 13 | # 14 | # 给定一个整数,写一个函数来判断它是否是 3 的幂次方。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 27 19 | # 输出: true 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 0 25 | # 输出: false 26 | # 27 | # 示例 3: 28 | # 29 | # 输入: 9 30 | # 输出: true 31 | # 32 | # 示例 4: 33 | # 34 | # 输入: 45 35 | # 输出: false 36 | # 37 | # 进阶: 38 | # 你能不使用循环或者递归来完成本题吗? 39 | # 40 | # 41 | 42 | 43 | class Solution: 44 | def isPowerOfThree(self, n: int) -> bool: 45 | if n == 0: 46 | return False 47 | while n % 3 == 0: 48 | n //= 3 49 | return n == 1 50 | -------------------------------------------------------------------------------- /medium/61.旋转链表/61.旋转链表.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=61 lang=javascript 3 | * 4 | * [61] 旋转链表 5 | */ 6 | /** 7 | * Definition for singly-linked list. 8 | * function ListNode(val) { 9 | * this.val = val; 10 | * this.next = null; 11 | * } 12 | */ 13 | /** 14 | * @param {ListNode} head 15 | * @param {number} k 16 | * @return {ListNode} 17 | */ 18 | var rotateRight = function(head, k) { 19 | if (!head || k === 0) return head 20 | let cursor = head 21 | let tail = null 22 | let len = 1 23 | while (cursor.next) { 24 | cursor = cursor.next 25 | len++ 26 | } 27 | let steps = len - (k % len) 28 | tail = cursor 29 | cursor.next = head 30 | cursor = head 31 | for (let i = 0; i < steps; i++) { 32 | cursor = cursor.next 33 | tail = tail.next 34 | } 35 | tail.next = null 36 | return cursor 37 | } 38 | -------------------------------------------------------------------------------- /easy/389.找不同/389.找不同.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=389 lang=python3 3 | # 4 | # [389] 找不同 5 | # 6 | # https://leetcode-cn.com/problems/find-the-difference/description/ 7 | # 8 | # algorithms 9 | # Easy (54.04%) 10 | # Total Accepted: 6.5K 11 | # Total Submissions: 12K 12 | # Testcase Example: '"abcd"\n"abcde"' 13 | # 14 | # 给定两个字符串 s 和 t,它们只包含小写字母。 15 | # 16 | # 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 17 | # 18 | # 请找出在 t 中被添加的字母。 19 | # 20 | # 21 | # 22 | # 示例: 23 | # 24 | # 输入: 25 | # s = "abcd" 26 | # t = "abcde" 27 | # 28 | # 输出: 29 | # e 30 | # 31 | # 解释: 32 | # 'e' 是那个被添加的字母。 33 | # 34 | # 35 | # 36 | 37 | 38 | class Solution: 39 | def findTheDifference(self, s: str, t: str) -> str: 40 | res = 0 41 | for n in s: 42 | res ^= ord(n) 43 | for n in t: 44 | res ^= ord(n) 45 | return chr(res) 46 | -------------------------------------------------------------------------------- /medium/137.只出现一次的数字-ii/137.只出现一次的数字-ii.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=137 lang=golang 3 | * 4 | * [137] 只出现一次的数字 II 5 | * 6 | * https://leetcode-cn.com/problems/single-number-ii/description/ 7 | * 8 | * algorithms 9 | * Medium (63.92%) 10 | * Likes: 136 11 | * Dislikes: 0 12 | * Total Accepted: 7.9K 13 | * Total Submissions: 12.3K 14 | * Testcase Example: '[2,2,3,2]' 15 | * 16 | * 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。 17 | * 18 | * 说明: 19 | * 20 | * 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 21 | * 22 | * 示例 1: 23 | * 24 | * 输入: [2,2,3,2] 25 | * 输出: 3 26 | * 27 | * 28 | * 示例 2: 29 | * 30 | * 输入: [0,1,0,1,0,1,99] 31 | * 输出: 99 32 | * 33 | */ 34 | func singleNumber(nums []int) int { 35 | a, b := 0, 0 36 | for _, n := range nums { 37 | b = b ^ n & ^a 38 | a = a ^ n & ^b 39 | } 40 | return b 41 | } 42 | 43 | -------------------------------------------------------------------------------- /medium/61.旋转链表/61.旋转链表.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=61 lang=python3 3 | # 4 | # [61] 旋转链表 5 | # 6 | # Definition for singly-linked list. 7 | # class ListNode: 8 | # def __init__(self, x): 9 | # self.val = x 10 | # self.next = None 11 | 12 | 13 | class Solution: 14 | def rotateRight(self, head: ListNode, k: int) -> ListNode: 15 | if head is None or k == 0: 16 | return head 17 | cursor, tail, length = head, None, 1 18 | while cursor.next: 19 | cursor = cursor.next 20 | length += 1 21 | steps = length - (k % length) 22 | tail = cursor 23 | cursor.next = head 24 | cursor = head 25 | for i in range(steps): 26 | cursor = cursor.next 27 | tail = tail.next 28 | tail.next = None 29 | return cursor 30 | -------------------------------------------------------------------------------- /medium/201.数字范围按位与.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=201 lang=javascript 3 | * 4 | * [201] 数字范围按位与 5 | * 6 | * https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/description/ 7 | * 8 | * algorithms 9 | * Medium (40.76%) 10 | * Likes: 39 11 | * Dislikes: 0 12 | * Total Accepted: 3.2K 13 | * Total Submissions: 7.7K 14 | * Testcase Example: '5\n7' 15 | * 16 | * 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [5,7] 21 | * 输出: 4 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [0,1] 26 | * 输出: 0 27 | * 28 | */ 29 | /** 30 | * @param {number} m 31 | * @param {number} n 32 | * @return {number} 33 | */ 34 | var rangeBitwiseAnd = function(m, n) { 35 | let count = 0 36 | while (m !== n) { 37 | m >>= 1 38 | n >>= 1 39 | count++ 40 | } 41 | return m << count 42 | } 43 | -------------------------------------------------------------------------------- /easy/231.2-的幂/231.2-的幂.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=231 lang=javascript 3 | * 4 | * [231] 2的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-two/description/ 7 | * 8 | * algorithms 9 | * Easy (43.83%) 10 | * Total Accepted: 12.1K 11 | * Total Submissions: 27.5K 12 | * Testcase Example: '1' 13 | * 14 | * 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 1 19 | * 输出: true 20 | * 解释: 20 = 1 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 16 25 | * 输出: true 26 | * 解释: 24 = 16 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 218 31 | * 输出: false 32 | * 33 | */ 34 | /** 35 | * @param {number} n 36 | * @return {boolean} 37 | */ 38 | var isPowerOfTwo = function(n) { 39 | let count = 0 40 | while (n >= 1) { 41 | if (n & 1) count++ 42 | if (count === 2) return false 43 | n >>= 1 44 | } 45 | return count === 1 46 | } 47 | -------------------------------------------------------------------------------- /easy/387.字符串中的第一个唯一字符/387.字符串中的第一个唯一字符.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=387 lang=golang 3 | * 4 | * [387] 字符串中的第一个唯一字符 5 | * 6 | * https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/ 7 | * 8 | * algorithms 9 | * Easy (35.49%) 10 | * Total Accepted: 21.3K 11 | * Total Submissions: 59.4K 12 | * Testcase Example: '"leetcode"' 13 | * 14 | * 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 15 | * 16 | * 案例: 17 | * 18 | * 19 | * s = "leetcode" 20 | * 返回 0. 21 | * 22 | * s = "loveleetcode", 23 | * 返回 2. 24 | * 25 | * 26 | * 27 | * 28 | * 注意事项:您可以假定该字符串只包含小写字母。 29 | * 30 | */ 31 | func firstUniqChar(s string) int { 32 | arr := make([]int, 26) 33 | for _, str := range s { 34 | arr[str-'a']++ 35 | } 36 | for i, str := range s { 37 | if arr[str-'a'] == 1 { 38 | return i 39 | } 40 | } 41 | return -1 42 | } 43 | 44 | -------------------------------------------------------------------------------- /easy/461.汉明距离/461.汉明距离.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=461 lang=python3 3 | # 4 | # [461] 汉明距离 5 | # 6 | # https://leetcode-cn.com/problems/hamming-distance/description/ 7 | # 8 | # algorithms 9 | # Easy (68.23%) 10 | # Total Accepted: 14.8K 11 | # Total Submissions: 21.7K 12 | # Testcase Example: '1\n4' 13 | # 14 | # 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 15 | # 16 | # 给出两个整数 x 和 y,计算它们之间的汉明距离。 17 | # 18 | # 注意: 19 | # 0 ≤ x, y < 2^31. 20 | # 21 | # 示例: 22 | # 23 | # 24 | # 输入: x = 1, y = 4 25 | # 26 | # 输出: 2 27 | # 28 | # 解释: 29 | # 1 (0 0 0 1) 30 | # 4 (0 1 0 0) 31 | # ⁠ ↑ ↑ 32 | # 33 | # 上面的箭头指出了对应二进制位不同的位置。 34 | # 35 | # 36 | # 37 | 38 | 39 | class Solution: 40 | def hammingDistance(self, x: int, y: int) -> int: 41 | ans, num = 0, x ^ y 42 | while num: 43 | num &= num - 1 44 | ans += 1 45 | return ans 46 | -------------------------------------------------------------------------------- /medium/179.最大数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=179 lang=javascript 3 | * 4 | * [179] 最大数 5 | * 6 | * https://leetcode-cn.com/problems/largest-number/description/ 7 | * 8 | * algorithms 9 | * Medium (32.25%) 10 | * Likes: 122 11 | * Dislikes: 0 12 | * Total Accepted: 7.4K 13 | * Total Submissions: 23K 14 | * Testcase Example: '[10,2]' 15 | * 16 | * 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [10,2] 21 | * 输出: 210 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [3,30,34,5,9] 26 | * 输出: 9534330 27 | * 28 | * 说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。 29 | * 30 | */ 31 | /** 32 | * @param {number[]} nums 33 | * @return {string} 34 | */ 35 | var largestNumber = function(nums) { 36 | const res = nums 37 | .sort((a, b) => Number(`${b}${a}`) - Number(`${a}${b}`)) 38 | .join('') 39 | return res[0] === '0' ? '0' : res 40 | } 41 | -------------------------------------------------------------------------------- /medium/86.分隔链表/86.分隔链表.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=86 lang=javascript 3 | * 4 | * [86] 分隔链表 5 | */ 6 | /** 7 | * Definition for singly-linked list. 8 | * function ListNode(val) { 9 | * this.val = val; 10 | * this.next = null; 11 | * } 12 | */ 13 | /** 14 | * @param {ListNode} head 15 | * @param {number} x 16 | * @return {ListNode} 17 | */ 18 | var partition = function(head, x) { 19 | let smallHead = new ListNode(null) 20 | let bigHead = new ListNode(null) 21 | const res = smallHead 22 | const big = bigHead 23 | while (head) { 24 | if (head.val < x) { 25 | smallHead.next = head 26 | smallHead = smallHead.next 27 | } else { 28 | bigHead.next = head 29 | bigHead = bigHead.next 30 | } 31 | head = head.next 32 | } 33 | smallHead.next = big.next 34 | bigHead.next = null 35 | return res.next 36 | } 37 | -------------------------------------------------------------------------------- /easy/169.求众数/169.求众数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=169 lang=python3 3 | # 4 | # [169] 求众数 5 | # 6 | # https://leetcode-cn.com/problems/majority-element/description/ 7 | # 8 | # algorithms 9 | # Easy (56.45%) 10 | # Total Accepted: 19.5K 11 | # Total Submissions: 34.4K 12 | # Testcase Example: '[3,2,3]' 13 | # 14 | # 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 15 | # 16 | # 你可以假设数组是非空的,并且给定的数组总是存在众数。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: [3,2,3] 21 | # 输出: 3 22 | # 23 | # 示例 2: 24 | # 25 | # 输入: [2,2,1,1,1,2,2] 26 | # 输出: 2 27 | # 28 | # 29 | # 30 | 31 | 32 | class Solution: 33 | def majorityElement(self, nums: 'List[int]') -> 'int': 34 | m = {} 35 | for n in nums: 36 | if n in m: 37 | m[n] += 1 38 | else: 39 | m[n] = 1 40 | if m[n] > len(nums) / 2.0: 41 | return n 42 | -------------------------------------------------------------------------------- /easy/367.有效的完全平方数/367.有效的完全平方数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=367 lang=golang 3 | * 4 | * [367] 有效的完全平方数 5 | * 6 | * https://leetcode-cn.com/problems/valid-perfect-square/description/ 7 | * 8 | * algorithms 9 | * Easy (39.57%) 10 | * Total Accepted: 6.3K 11 | * Total Submissions: 15.8K 12 | * Testcase Example: '16' 13 | * 14 | * 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 15 | * 16 | * 说明:不要使用任何内置的库函数,如  sqrt。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入:16 21 | * 输出:True 22 | * 23 | * 示例 2: 24 | * 25 | * 输入:14 26 | * 输出:False 27 | * 28 | * 29 | */ 30 | func isPerfectSquare(num int) bool { 31 | l := 0 32 | r := num 33 | for l <= r { 34 | mid := l + (r-l)/2 35 | if mid*mid == num { 36 | return true 37 | } else if mid*mid < num { 38 | l = mid + 1 39 | } else { 40 | r = mid - 1 41 | } 42 | } 43 | return false 44 | } 45 | 46 | -------------------------------------------------------------------------------- /medium/137.只出现一次的数字-ii/137.只出现一次的数字-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=137 lang=python3 3 | # 4 | # [137] 只出现一次的数字 II 5 | # 6 | # https://leetcode-cn.com/problems/single-number-ii/description/ 7 | # 8 | # algorithms 9 | # Medium (63.92%) 10 | # Likes: 136 11 | # Dislikes: 0 12 | # Total Accepted: 7.9K 13 | # Total Submissions: 12.3K 14 | # Testcase Example: '[2,2,3,2]' 15 | # 16 | # 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。 17 | # 18 | # 说明: 19 | # 20 | # 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 21 | # 22 | # 示例 1: 23 | # 24 | # 输入: [2,2,3,2] 25 | # 输出: 3 26 | # 27 | # 28 | # 示例 2: 29 | # 30 | # 输入: [0,1,0,1,0,1,99] 31 | # 输出: 99 32 | # 33 | # 34 | 35 | 36 | class Solution: 37 | def singleNumber(self, nums: List[int]) -> int: 38 | a, b = 0, 0 39 | for n in nums: 40 | b = b ^ n & ~a 41 | a = a ^ n & ~b 42 | return b 43 | -------------------------------------------------------------------------------- /easy/268.缺失数字/268.缺失数字.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=268 lang=javascript 3 | * 4 | * [268] 缺失数字 5 | * 6 | * https://leetcode-cn.com/problems/missing-number/description/ 7 | * 8 | * algorithms 9 | * Easy (47.39%) 10 | * Total Accepted: 13.7K 11 | * Total Submissions: 28.6K 12 | * Testcase Example: '[3,0,1]' 13 | * 14 | * 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: [3,0,1] 19 | * 输出: 2 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: [9,6,4,2,3,5,7,0,1] 25 | * 输出: 8 26 | * 27 | * 28 | * 说明: 29 | * 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现? 30 | * 31 | */ 32 | /** 33 | * @param {number[]} nums 34 | * @return {number} 35 | */ 36 | var missingNumber = function(nums) { 37 | const l = nums.length 38 | let sum = (l * (l + 1)) / 2 39 | for (let n of nums) { 40 | sum -= n 41 | } 42 | return sum 43 | } 44 | -------------------------------------------------------------------------------- /easy/169.求众数/169.求众数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=169 lang=golang 3 | * 4 | * [169] 求众数 5 | * 6 | * https://leetcode-cn.com/problems/majority-element/description/ 7 | * 8 | * algorithms 9 | * Easy (56.45%) 10 | * Total Accepted: 19.5K 11 | * Total Submissions: 34.4K 12 | * Testcase Example: '[3,2,3]' 13 | * 14 | * 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 15 | * 16 | * 你可以假设数组是非空的,并且给定的数组总是存在众数。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [3,2,3] 21 | * 输出: 3 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [2,2,1,1,1,2,2] 26 | * 输出: 2 27 | * 28 | * 29 | */ 30 | func majorityElement(nums []int) int { 31 | m := map[int]int{} 32 | res := 0 33 | for _, n := range nums { 34 | if _, ok := m[n]; ok { 35 | m[n]++ 36 | } else { 37 | m[n] = 1 38 | } 39 | if m[n] > len(nums)/2 { 40 | res = n 41 | break 42 | } 43 | } 44 | return res 45 | } 46 | 47 | -------------------------------------------------------------------------------- /easy/476.数字的补数/476.数字的补数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=476 lang=golang 3 | * 4 | * [476] 数字的补数 5 | * 6 | * https://leetcode-cn.com/problems/number-complement/description/ 7 | * 8 | * algorithms 9 | * Easy (67.07%) 10 | * Total Accepted: 7.5K 11 | * Total Submissions: 11.2K 12 | * Testcase Example: '5' 13 | * 14 | * 给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。 15 | * 16 | * 注意: 17 | * 18 | * 19 | * 给定的整数保证在32位带符号整数的范围内。 20 | * 你可以假定二进制数不包含前导零位。 21 | * 22 | * 23 | * 示例 1: 24 | * 25 | * 26 | * 输入: 5 27 | * 输出: 2 28 | * 解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2。 29 | * 30 | * 31 | * 示例 2: 32 | * 33 | * 34 | * 输入: 1 35 | * 输出: 0 36 | * 解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0。 37 | * 38 | * 39 | */ 40 | func findComplement(num int) int { 41 | ans := num 42 | n := 0 43 | for num != 0 { 44 | num >>= 1 45 | n = (n << 1) | 1 46 | } 47 | return ans ^ n 48 | } 49 | 50 | -------------------------------------------------------------------------------- /easy/53.最大子序和/53.最大子序和.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=53 lang=golang 3 | * 4 | * [53] 最大子序和 5 | * 6 | * https://leetcode-cn.com/problems/maximum-subarray/description/ 7 | * 8 | * algorithms 9 | * Easy (42.02%) 10 | * Total Accepted: 35.4K 11 | * Total Submissions: 84.1K 12 | * Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]' 13 | * 14 | * 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 15 | * 16 | * 示例: 17 | * 18 | * 输入: [-2,1,-3,4,-1,2,1,-5,4], 19 | * 输出: 6 20 | * 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 21 | * 22 | * 23 | * 进阶: 24 | * 25 | * 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 26 | * 27 | */ 28 | func maxSubArray(nums []int) int { 29 | res := nums[:] 30 | max := nums[0] 31 | for i := 1; i < len(nums); i++ { 32 | if res[i-1] > 0 { 33 | res[i] = nums[i] + res[i-1] 34 | } 35 | if res[i] > max { 36 | max = res[i] 37 | } 38 | } 39 | return max 40 | } 41 | -------------------------------------------------------------------------------- /easy/326.3-的幂/326.3-的幂.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=326 lang=javascript 3 | * 4 | * [326] 3的幂 5 | * 6 | * https://leetcode-cn.com/problems/power-of-three/description/ 7 | * 8 | * algorithms 9 | * Easy (42.27%) 10 | * Total Accepted: 12.9K 11 | * Total Submissions: 30.4K 12 | * Testcase Example: '27' 13 | * 14 | * 给定一个整数,写一个函数来判断它是否是 3 的幂次方。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 27 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 0 25 | * 输出: false 26 | * 27 | * 示例 3: 28 | * 29 | * 输入: 9 30 | * 输出: true 31 | * 32 | * 示例 4: 33 | * 34 | * 输入: 45 35 | * 输出: false 36 | * 37 | * 进阶: 38 | * 你能不使用循环或者递归来完成本题吗? 39 | * 40 | */ 41 | /** 42 | * @param {number} n 43 | * @return {boolean} 44 | */ 45 | var isPowerOfThree = function(n) { 46 | if (!n) return false 47 | while (n % 3 === 0) { 48 | n /= 3 49 | } 50 | return n === 1 51 | } 52 | -------------------------------------------------------------------------------- /easy/453.最小移动次数使数组元素相等/453.最小移动次数使数组元素相等.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=453 lang=javascript 3 | * 4 | * [453] 最小移动次数使数组元素相等 5 | * 6 | * https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/description/ 7 | * 8 | * algorithms 9 | * Easy (49.41%) 10 | * Total Accepted: 2.7K 11 | * Total Submissions: 5.4K 12 | * Testcase Example: '[1,2,3]' 13 | * 14 | * 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。 15 | * 16 | * 示例: 17 | * 18 | * 19 | * 输入: 20 | * [1,2,3] 21 | * 22 | * 输出: 23 | * 3 24 | * 25 | * 解释: 26 | * 只需要3次移动(注意每次移动会增加两个元素的值): 27 | * 28 | * [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 29 | * 30 | * 31 | */ 32 | /** 33 | * @param {number[]} nums 34 | * @return {number} 35 | */ 36 | var minMoves = function(nums) { 37 | const sum = nums.reduce((a, b) => a + b, 0) 38 | return sum - nums.length * Math.min(...nums) 39 | } 40 | -------------------------------------------------------------------------------- /easy/217.存在重复元素/217.存在重复元素.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=217 lang=golang 3 | * 4 | * [217] 存在重复元素 5 | * 6 | * https://leetcode-cn.com/problems/contains-duplicate/description/ 7 | * 8 | * algorithms 9 | * Easy (45.88%) 10 | * Total Accepted: 41.3K 11 | * Total Submissions: 89.3K 12 | * Testcase Example: '[1,2,3,1]' 13 | * 14 | * 给定一个整数数组,判断是否存在重复元素。 15 | * 16 | * 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [1,2,3,1] 21 | * 输出: true 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [1,2,3,4] 26 | * 输出: false 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: [1,1,1,3,3,4,3,2,4,2] 31 | * 输出: true 32 | * 33 | */ 34 | func containsDuplicate(nums []int) bool { 35 | m := map[int]int{} 36 | for _, n := range nums { 37 | if _, ok := m[n]; ok { 38 | return true 39 | } else { 40 | m[n] = 1 41 | } 42 | } 43 | return false 44 | } 45 | 46 | -------------------------------------------------------------------------------- /easy/58.最后一个单词的长度/58.最后一个单词的长度.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=58 lang=golang 3 | * 4 | * [58] 最后一个单词的长度 5 | * 6 | * https://leetcode-cn.com/problems/length-of-last-word/description/ 7 | * 8 | * algorithms 9 | * Easy (28.61%) 10 | * Total Accepted: 16.7K 11 | * Total Submissions: 58.5K 12 | * Testcase Example: '"Hello World"' 13 | * 14 | * 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。 15 | * 16 | * 如果不存在最后一个单词,请返回 0 。 17 | * 18 | * 说明:一个单词是指由字母组成,但不包含任何空格的字符串。 19 | * 20 | * 示例: 21 | * 22 | * 输入: "Hello World" 23 | * 输出: 5 24 | * 25 | * 26 | */ 27 | func lengthOfLastWord(s string) int { 28 | n := len(s) - 1 29 | flag := true 30 | res := 0 31 | for n >= 0 { 32 | if string(s[n]) == " " { 33 | if flag { 34 | s = s[0:n] 35 | } else { 36 | return res 37 | } 38 | } else { 39 | flag = false 40 | res++ 41 | } 42 | n-- 43 | } 44 | return res 45 | } 46 | -------------------------------------------------------------------------------- /easy/1.两数之和/1.两数之和.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=1 lang=python3 3 | # 4 | # [1] 两数之和 5 | # 6 | # https://leetcode-cn.com/problems/two-sum/description/ 7 | # 8 | # algorithms 9 | # Easy (44.37%) 10 | # Total Accepted: 238.5K 11 | # Total Submissions: 537.5K 12 | # Testcase Example: '[2,7,11,15]\n9' 13 | # 14 | # 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 15 | # 16 | # 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 17 | # 18 | # 示例: 19 | # 20 | # 给定 nums = [2, 7, 11, 15], target = 9 21 | # 22 | # 因为 nums[0] + nums[1] = 2 + 7 = 9 23 | # 所以返回 [0, 1] 24 | # 25 | # 26 | # 27 | class Solution: 28 | def twoSum(self, nums: 'List[int]', target: 'int') -> 'List[int]': 29 | m = {} 30 | for i in range(len(nums)): 31 | n = nums[i] 32 | if target - n in m: 33 | return [m[target - n], i] 34 | else: 35 | m[n] = i 36 | -------------------------------------------------------------------------------- /easy/204.计数质数/204.计数质数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=204 lang=golang 3 | * 4 | * [204] 计数质数 5 | * 6 | * https://leetcode-cn.com/problems/count-primes/description/ 7 | * 8 | * algorithms 9 | * Easy (25.80%) 10 | * Total Accepted: 12.4K 11 | * Total Submissions: 47.4K 12 | * Testcase Example: '10' 13 | * 14 | * 统计所有小于非负整数 n 的质数的数量。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 10 19 | * 输出: 4 20 | * 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 21 | * 22 | * 23 | */ 24 | func countPrimes(n int) int { 25 | isPrime := make([]bool, n) 26 | for i := 2; i < n; i++ { 27 | isPrime[i] = true 28 | } 29 | for i := 2; i*i < n; i++ { 30 | if isPrime[i] == false { 31 | continue 32 | } 33 | for j := i * i; j < n; j += i { 34 | isPrime[j] = false 35 | } 36 | } 37 | count := 0 38 | for i := 2; i < n; i++ { 39 | if isPrime[i] { 40 | count++ 41 | } 42 | } 43 | return count 44 | } 45 | 46 | -------------------------------------------------------------------------------- /easy/344.反转字符串/344.反转字符串.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=344 lang=golang 3 | * 4 | * [344] 反转字符串 5 | * 6 | * https://leetcode-cn.com/problems/reverse-string/description/ 7 | * 8 | * algorithms 9 | * Easy (65.08%) 10 | * Total Accepted: 35.9K 11 | * Total Submissions: 55.2K 12 | * Testcase Example: '["h","e","l","l","o"]' 13 | * 14 | * 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 15 | * 16 | * 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 17 | * 18 | * 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 19 | * 20 | * 21 | * 22 | * 示例 1: 23 | * 24 | * 输入:["h","e","l","l","o"] 25 | * 输出:["o","l","l","e","h"] 26 | * 27 | * 28 | * 示例 2: 29 | * 30 | * 输入:["H","a","n","n","a","h"] 31 | * 输出:["h","a","n","n","a","H"] 32 | * 33 | */ 34 | func reverseString(s []byte) { 35 | l := 0 36 | r := len(s) - 1 37 | for l <= r { 38 | s[l], s[r] = s[r], s[l] 39 | l++ 40 | r-- 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /easy/217.存在重复元素/217.存在重复元素.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=217 lang=python3 3 | # 4 | # [217] 存在重复元素 5 | # 6 | # https://leetcode-cn.com/problems/contains-duplicate/description/ 7 | # 8 | # algorithms 9 | # Easy (45.88%) 10 | # Total Accepted: 41.3K 11 | # Total Submissions: 89.3K 12 | # Testcase Example: '[1,2,3,1]' 13 | # 14 | # 给定一个整数数组,判断是否存在重复元素。 15 | # 16 | # 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: [1,2,3,1] 21 | # 输出: true 22 | # 23 | # 示例 2: 24 | # 25 | # 输入: [1,2,3,4] 26 | # 输出: false 27 | # 28 | # 示例 3: 29 | # 30 | # 输入: [1,1,1,3,3,4,3,2,4,2] 31 | # 输出: true 32 | # 33 | # 34 | 35 | 36 | class Solution: 37 | def containsDuplicate(self, nums: List[int]) -> bool: 38 | m = {} 39 | for n in nums: 40 | if n in m: 41 | return True 42 | else: 43 | m[n] = 1 44 | return False 45 | -------------------------------------------------------------------------------- /easy/387.字符串中的第一个唯一字符/387.字符串中的第一个唯一字符.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=387 lang=python3 3 | # 4 | # [387] 字符串中的第一个唯一字符 5 | # 6 | # https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/ 7 | # 8 | # algorithms 9 | # Easy (35.49%) 10 | # Total Accepted: 21.3K 11 | # Total Submissions: 59.4K 12 | # Testcase Example: '"leetcode"' 13 | # 14 | # 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 15 | # 16 | # 案例: 17 | # 18 | # 19 | # s = "leetcode" 20 | # 返回 0. 21 | # 22 | # s = "loveleetcode", 23 | # 返回 2. 24 | # 25 | # 26 | # 27 | # 28 | # 注意事项:您可以假定该字符串只包含小写字母。 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def firstUniqChar(self, s: str) -> int: 35 | arr = [0] * 26 36 | for n in s: 37 | arr[ord(n) - ord('a')] += 1 38 | for i in range(len(s)): 39 | if arr[ord(s[i]) - ord('a')] == 1: 40 | return i 41 | return -1 42 | -------------------------------------------------------------------------------- /easy/434.字符串中的单词数/434.字符串中的单词数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=434 lang=golang 3 | * 4 | * [434] 字符串中的单词数 5 | * 6 | * https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/ 7 | * 8 | * algorithms 9 | * Easy (28.91%) 10 | * Total Accepted: 3.9K 11 | * Total Submissions: 13.4K 12 | * Testcase Example: '"Hello, my name is John"' 13 | * 14 | * 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。 15 | * 16 | * 请注意,你可以假定字符串里不包括任何不可打印的字符。 17 | * 18 | * 示例: 19 | * 20 | * 输入: "Hello, my name is John" 21 | * 输出: 5 22 | * 23 | * 24 | */ 25 | func countSegments(s string) int { 26 | flag := true 27 | count := 0 28 | for _, n := range s { 29 | if flag { 30 | if n != ' ' { 31 | flag = false 32 | } 33 | } else { 34 | if n == ' ' { 35 | count ++ 36 | flag = true 37 | } 38 | } 39 | } 40 | if flag { 41 | return count 42 | } 43 | return count + 1 44 | } 45 | 46 | -------------------------------------------------------------------------------- /easy/476.数字的补数/476.数字的补数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=476 lang=python3 3 | # 4 | # [476] 数字的补数 5 | # 6 | # https://leetcode-cn.com/problems/number-complement/description/ 7 | # 8 | # algorithms 9 | # Easy (67.07%) 10 | # Total Accepted: 7.5K 11 | # Total Submissions: 11.2K 12 | # Testcase Example: '5' 13 | # 14 | # 给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。 15 | # 16 | # 注意: 17 | # 18 | # 19 | # 给定的整数保证在32位带符号整数的范围内。 20 | # 你可以假定二进制数不包含前导零位。 21 | # 22 | # 23 | # 示例 1: 24 | # 25 | # 26 | # 输入: 5 27 | # 输出: 2 28 | # 解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2。 29 | # 30 | # 31 | # 示例 2: 32 | # 33 | # 34 | # 输入: 1 35 | # 输出: 0 36 | # 解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0。 37 | # 38 | # 39 | # 40 | 41 | 42 | class Solution: 43 | def findComplement(self, num: int) -> int: 44 | temp, c = num, 0 45 | while temp > 0: 46 | temp >>= 1 47 | c = (c << 1) + 1 48 | return num ^ c 49 | -------------------------------------------------------------------------------- /medium/96.不同的二叉搜索树/96.不同的二叉搜索树.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=96 lang=golang 3 | * 4 | * [96] 不同的二叉搜索树 5 | * 6 | * https://leetcode-cn.com/problems/unique-binary-search-trees/description/ 7 | * 8 | * algorithms 9 | * Medium (59.07%) 10 | * Likes: 149 11 | * Dislikes: 0 12 | * Total Accepted: 8.2K 13 | * Total Submissions: 13.7K 14 | * Testcase Example: '3' 15 | * 16 | * 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 17 | * 18 | * 示例: 19 | * 20 | * 输入: 3 21 | * 输出: 5 22 | * 解释: 23 | * 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 24 | * 25 | * ⁠ 1 3 3 2 1 26 | * ⁠ \ / / / \ \ 27 | * ⁠ 3 2 1 1 3 2 28 | * ⁠ / / \ \ 29 | * ⁠ 2 1 2 3 30 | * 31 | */ 32 | func numTrees(n int) int { 33 | if n == 1 { 34 | return 1 35 | } 36 | return (numTrees(n-1) * (4*n - 2)) / (n + 1) 37 | } 38 | 39 | -------------------------------------------------------------------------------- /medium/82.删除排序链表中的重复元素-ii/82.删除排序链表中的重复元素-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=82 lang=javascript 3 | * 4 | * [82] 删除排序链表中的重复元素 II 5 | */ 6 | /** 7 | * Definition for singly-linked list. 8 | * function ListNode(val) { 9 | * this.val = val; 10 | * this.next = null; 11 | * } 12 | */ 13 | /** 14 | * @param {ListNode} head 15 | * @return {ListNode} 16 | */ 17 | var deleteDuplicates = function(head) { 18 | const dummyHead = new ListNode(-(1 << 20)) 19 | dummyHead.next = head 20 | let node = dummyHead 21 | let prev = dummyHead 22 | while (node && node.next) { 23 | if (node.val === node.next.val) { 24 | while (node && node.next && node.val === node.next.val) { 25 | node = node.next 26 | } 27 | prev.next = node.next 28 | node = node.next 29 | } else { 30 | prev = node 31 | node = node.next 32 | } 33 | } 34 | return dummyHead.next 35 | } 36 | -------------------------------------------------------------------------------- /easy/453.最小移动次数使数组元素相等/453.最小移动次数使数组元素相等.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=453 lang=golang 3 | * 4 | * [453] 最小移动次数使数组元素相等 5 | * 6 | * https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/description/ 7 | * 8 | * algorithms 9 | * Easy (49.41%) 10 | * Total Accepted: 2.7K 11 | * Total Submissions: 5.4K 12 | * Testcase Example: '[1,2,3]' 13 | * 14 | * 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动可以使 n - 1 个元素增加 1。 15 | * 16 | * 示例: 17 | * 18 | * 19 | * 输入: 20 | * [1,2,3] 21 | * 22 | * 输出: 23 | * 3 24 | * 25 | * 解释: 26 | * 只需要3次移动(注意每次移动会增加两个元素的值): 27 | * 28 | * [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 29 | * 30 | * 31 | */ 32 | import "math" 33 | 34 | func minMoves(nums []int) int { 35 | sum := 0 36 | min := float64(^uint(0)) 37 | for _, n := range nums { 38 | sum += n 39 | min = math.Min(min, float64(n)) 40 | } 41 | return sum - int(min)*len(nums) 42 | } 43 | 44 | -------------------------------------------------------------------------------- /medium/24.两两交换链表中的节点/24.两两交换链表中的节点.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=24 lang=golang 3 | * 4 | * [24] 两两交换链表中的节点 5 | * 6 | * https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/ 7 | * 8 | * algorithms 9 | * Medium (58.02%) 10 | * Total Accepted: 15.8K 11 | * Total Submissions: 27.2K 12 | * Testcase Example: '[1,2,3,4]' 13 | * 14 | * 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 15 | * 16 | * 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 17 | * 18 | * 19 | * 20 | * 示例: 21 | * 22 | * 给定 1->2->3->4, 你应该返回 2->1->4->3. 23 | * 24 | * 25 | */ 26 | /** 27 | * Definition for singly-linked list. 28 | * type ListNode struct { 29 | * Val int 30 | * Next *ListNode 31 | * } 32 | */ 33 | func swapPairs(head *ListNode) *ListNode { 34 | if head == nil || head.Next == nil { 35 | return head 36 | } 37 | next := head.Next 38 | head.Next = swapPairs(next.Next) 39 | next.Next = head 40 | return next 41 | } 42 | 43 | -------------------------------------------------------------------------------- /medium/46.全排列/46.全排列.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=46 lang=python3 3 | # 4 | # [46] 全排列 5 | # 6 | # https://leetcode-cn.com/problems/permutations/description/ 7 | # 8 | # algorithms 9 | # Medium (67.44%) 10 | # Total Accepted: 19.9K 11 | # Total Submissions: 29.5K 12 | # Testcase Example: '[1,2,3]' 13 | # 14 | # 给定一个没有重复数字的序列,返回其所有可能的全排列。 15 | # 16 | # 示例: 17 | # 18 | # 输入: [1,2,3] 19 | # 输出: 20 | # [ 21 | # ⁠ [1,2,3], 22 | # ⁠ [1,3,2], 23 | # ⁠ [2,1,3], 24 | # ⁠ [2,3,1], 25 | # ⁠ [3,1,2], 26 | # ⁠ [3,2,1] 27 | # ] 28 | # 29 | # 30 | class Solution: 31 | def permute(self, nums: List[int]) -> List[List[int]]: 32 | if nums is None: return [] 33 | if len(nums) == 1: return [nums] 34 | res = [] 35 | for x in nums: 36 | ys = nums + [] 37 | ys.remove(x) 38 | for y in self.permute(ys): 39 | res.append([x] + y) 40 | return res 41 | 42 | -------------------------------------------------------------------------------- /medium/82.删除排序链表中的重复元素-ii/82.删除排序链表中的重复元素-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=82 lang=python3 3 | # 4 | # [82] 删除排序链表中的重复元素 II 5 | # 6 | # Definition for singly-linked list. 7 | # class ListNode: 8 | # def __init__(self, x): 9 | # self.val = x 10 | # self.next = None 11 | 12 | class Solution: 13 | def deleteDuplicates(self, head: ListNode) -> ListNode: 14 | dummy = ListNode(-100000) 15 | dummy.next = head 16 | node, prev = dummy, dummy 17 | while node != None and node.next != None: 18 | if node.val == node.next.val: 19 | while node != None and node.next != None and node.val == node.next.val: 20 | node = node.next 21 | prev.next = node.next 22 | node = node.next 23 | else: 24 | prev = node 25 | node = node.next 26 | 27 | return dummy.next 28 | 29 | -------------------------------------------------------------------------------- /easy/119.杨辉三角-ii/119.杨辉三角-ii.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=119 lang=golang 3 | * 4 | * [119] 杨辉三角 II 5 | * 6 | * https://leetcode-cn.com/problems/pascals-triangle-ii/description/ 7 | * 8 | * algorithms 9 | * Easy (53.57%) 10 | * Total Accepted: 9.7K 11 | * Total Submissions: 18K 12 | * Testcase Example: '3' 13 | * 14 | * 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 15 | * 16 | * 17 | * 18 | * 在杨辉三角中,每个数是它左上方和右上方的数的和。 19 | * 20 | * 示例: 21 | * 22 | * 输入: 3 23 | * 输出: [1,3,3,1] 24 | * 25 | * 26 | * 进阶: 27 | * 28 | * 你可以优化你的算法到 O(k) 空间复杂度吗? 29 | * 30 | */ 31 | func getRow(rowIndex int) []int { 32 | res := []int{1} 33 | for rowIndex > 0 { 34 | cur := []int{} 35 | for i := 0; i <= len(res); i++ { 36 | if i == 0 || i == len(res) { 37 | cur = append(cur, 1) 38 | } else { 39 | cur = append(cur, res[i]+res[i-1]) 40 | } 41 | } 42 | res = cur 43 | rowIndex-- 44 | } 45 | return res 46 | } 47 | -------------------------------------------------------------------------------- /easy/169.求众数/169.求众数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=169 lang=javascript 3 | * 4 | * [169] 求众数 5 | * 6 | * https://leetcode-cn.com/problems/majority-element/description/ 7 | * 8 | * algorithms 9 | * Easy (56.45%) 10 | * Total Accepted: 19.5K 11 | * Total Submissions: 34.4K 12 | * Testcase Example: '[3,2,3]' 13 | * 14 | * 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 15 | * 16 | * 你可以假设数组是非空的,并且给定的数组总是存在众数。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [3,2,3] 21 | * 输出: 3 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [2,2,1,1,1,2,2] 26 | * 输出: 2 27 | * 28 | * 29 | */ 30 | /** 31 | * @param {number[]} nums 32 | * @return {number} 33 | */ 34 | var majorityElement = function(nums) { 35 | const map = [] 36 | for (let n of nums) { 37 | if (n in map) { 38 | map[n]++ 39 | } else { 40 | map[n] = 1 41 | } 42 | if (map[n] > nums.length / 2) { 43 | return n 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /easy/485.最大连续-1-的个数/485.最大连续-1-的个数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=485 lang=python3 3 | # 4 | # [485] 最大连续1的个数 5 | # 6 | # https://leetcode-cn.com/problems/max-consecutive-ones/description/ 7 | # 8 | # algorithms 9 | # Easy (52.18%) 10 | # Total Accepted: 9.4K 11 | # Total Submissions: 18K 12 | # Testcase Example: '[1,0,1,1,0,1]' 13 | # 14 | # 给定一个二进制数组, 计算其中最大连续1的个数。 15 | # 16 | # 示例 1: 17 | # 18 | # 19 | # 输入: [1,1,0,1,1,1] 20 | # 输出: 3 21 | # 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 22 | # 23 | # 24 | # 注意: 25 | # 26 | # 27 | # 输入的数组只包含 0 和1。 28 | # 输入数组的长度是正整数,且不超过 10,000。 29 | # 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 36 | ans = 0 37 | cur = 0 38 | for n in nums: 39 | if n & 1: 40 | cur += 1 41 | ans = max(ans, cur) 42 | else: 43 | cur = 0 44 | return ans 45 | -------------------------------------------------------------------------------- /easy/53.最大子序和/53.最大子序和.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=53 lang=python3 3 | # 4 | # [53] 最大子序和 5 | # 6 | # https://leetcode-cn.com/problems/maximum-subarray/description/ 7 | # 8 | # algorithms 9 | # Easy (42.02%) 10 | # Total Accepted: 35.4K 11 | # Total Submissions: 84.1K 12 | # Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]' 13 | # 14 | # 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 15 | # 16 | # 示例: 17 | # 18 | # 输入: [-2,1,-3,4,-1,2,1,-5,4], 19 | # 输出: 6 20 | # 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 21 | # 22 | # 23 | # 进阶: 24 | # 25 | # 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 26 | # 27 | # 28 | class Solution: 29 | def maxSubArray(self, nums: 'List[int]') -> 'int': 30 | res = nums[:] 31 | cur = nums[0] 32 | for i in range(1, len(nums)): 33 | if res[i - 1] > 0: 34 | res[i] = nums[i] + res[i - 1] 35 | if res[i] > cur: 36 | cur = res[i] 37 | return cur 38 | -------------------------------------------------------------------------------- /medium/179.最大数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=179 lang=python3 3 | # 4 | # [179] 最大数 5 | # 6 | # https://leetcode-cn.com/problems/largest-number/description/ 7 | # 8 | # algorithms 9 | # Medium (32.25%) 10 | # Likes: 122 11 | # Dislikes: 0 12 | # Total Accepted: 7.4K 13 | # Total Submissions: 23K 14 | # Testcase Example: '[10,2]' 15 | # 16 | # 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: [10,2] 21 | # 输出: 210 22 | # 23 | # 示例 2: 24 | # 25 | # 输入: [3,30,34,5,9] 26 | # 输出: 9534330 27 | # 28 | # 说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。 29 | # 30 | # 31 | import functools 32 | 33 | 34 | def compare(a, b): 35 | return int(str(b)+str(a)) - int(str(a)+str(b)) 36 | 37 | 38 | class Solution: 39 | 40 | def largestNumber(self, nums) -> str: 41 | res = '' 42 | for n in sorted(nums, key=functools.cmp_to_key(compare)): 43 | res += str(n) 44 | return '0' if res[0] == '0' else res 45 | -------------------------------------------------------------------------------- /medium/96.不同的二叉搜索树/96.不同的二叉搜索树.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=96 lang=python3 3 | # 4 | # [96] 不同的二叉搜索树 5 | # 6 | # https://leetcode-cn.com/problems/unique-binary-search-trees/description/ 7 | # 8 | # algorithms 9 | # Medium (59.07%) 10 | # Likes: 149 11 | # Dislikes: 0 12 | # Total Accepted: 8.2K 13 | # Total Submissions: 13.7K 14 | # Testcase Example: '3' 15 | # 16 | # 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 17 | # 18 | # 示例: 19 | # 20 | # 输入: 3 21 | # 输出: 5 22 | # 解释: 23 | # 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 24 | # 25 | # ⁠ 1 3 3 2 1 26 | # ⁠ \ / / / \ \ 27 | # ⁠ 3 2 1 1 3 2 28 | # ⁠ / / \ \ 29 | # ⁠ 2 1 2 3 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def numTrees(self, n: int) -> int: 36 | if n == 1: 37 | return 1 38 | return (self.numTrees(n - 1) * (4 * n - 2)) // (n + 1) 39 | -------------------------------------------------------------------------------- /easy/202.快乐数/202.快乐数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=202 lang=golang 3 | * 4 | * [202] 快乐数 5 | * 6 | * https://leetcode-cn.com/problems/happy-number/description/ 7 | * 8 | * algorithms 9 | * Easy (51.51%) 10 | * Total Accepted: 11.4K 11 | * Total Submissions: 22K 12 | * Testcase Example: '19' 13 | * 14 | * 编写一个算法来判断一个数是不是“快乐数”。 15 | * 16 | * 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 17 | * 1。如果可以变为 1,那么这个数就是快乐数。 18 | * 19 | * 示例: 20 | * 21 | * 输入: 19 22 | * 输出: true 23 | * 解释: 24 | * 12 + 92 = 82 25 | * 82 + 22 = 68 26 | * 62 + 82 = 100 27 | * 12 + 02 + 02 = 1 28 | * 29 | * 30 | */ 31 | func isHappy(n int) bool { 32 | m := map[int]int{} 33 | for n != 1 { 34 | if _, ok := m[n]; ok { 35 | return false 36 | } 37 | m[n] = 1 38 | cur := 0 39 | for n != 0 { 40 | cur += (n % 10) * (n % 10) 41 | n /= 10 42 | } 43 | n = cur 44 | } 45 | return true 46 | } 47 | 48 | -------------------------------------------------------------------------------- /easy/461.汉明距离/461.汉明距离.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=461 lang=javascript 3 | * 4 | * [461] 汉明距离 5 | * 6 | * https://leetcode-cn.com/problems/hamming-distance/description/ 7 | * 8 | * algorithms 9 | * Easy (68.23%) 10 | * Total Accepted: 14.8K 11 | * Total Submissions: 21.7K 12 | * Testcase Example: '1\n4' 13 | * 14 | * 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 15 | * 16 | * 给出两个整数 x 和 y,计算它们之间的汉明距离。 17 | * 18 | * 注意: 19 | * 0 ≤ x, y < 2^31. 20 | * 21 | * 示例: 22 | * 23 | * 24 | * 输入: x = 1, y = 4 25 | * 26 | * 输出: 2 27 | * 28 | * 解释: 29 | * 1 (0 0 0 1) 30 | * 4 (0 1 0 0) 31 | * ⁠ ↑ ↑ 32 | * 33 | * 上面的箭头指出了对应二进制位不同的位置。 34 | * 35 | * 36 | */ 37 | /** 38 | * @param {number} x 39 | * @param {number} y 40 | * @return {number} 41 | */ 42 | var hammingDistance = function(x, y) { 43 | let ans = 0 44 | let n = x ^ y 45 | while (n) { 46 | n &= n - 1 47 | ans++ 48 | } 49 | return ans 50 | } 51 | -------------------------------------------------------------------------------- /medium/137.只出现一次的数字-ii/137.只出现一次的数字-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=137 lang=javascript 3 | * 4 | * [137] 只出现一次的数字 II 5 | * 6 | * https://leetcode-cn.com/problems/single-number-ii/description/ 7 | * 8 | * algorithms 9 | * Medium (63.92%) 10 | * Likes: 136 11 | * Dislikes: 0 12 | * Total Accepted: 7.9K 13 | * Total Submissions: 12.3K 14 | * Testcase Example: '[2,2,3,2]' 15 | * 16 | * 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。 17 | * 18 | * 说明: 19 | * 20 | * 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 21 | * 22 | * 示例 1: 23 | * 24 | * 输入: [2,2,3,2] 25 | * 输出: 3 26 | * 27 | * 28 | * 示例 2: 29 | * 30 | * 输入: [0,1,0,1,0,1,99] 31 | * 输出: 99 32 | * 33 | */ 34 | /** 35 | * @param {number[]} nums 36 | * @return {number} 37 | */ 38 | var singleNumber = function(nums) { 39 | let a = 0, 40 | b = 0 41 | for (const n of nums) { 42 | b = (b ^ n) & ~a 43 | a = (a ^ n) & ~b 44 | } 45 | return b 46 | } 47 | -------------------------------------------------------------------------------- /medium/63.不同路径-ii/63.不同路径-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=63 lang=javascript 3 | * 4 | * [63] 不同路径 II 5 | */ 6 | /** 7 | * @param {number[][]} obstacleGrid 8 | * @return {number} 9 | */ 10 | var uniquePathsWithObstacles = function(obstacleGrid) { 11 | if (!obstacleGrid.length) return 0 12 | const dp = [] 13 | const x = obstacleGrid.length 14 | const y = obstacleGrid[0].length 15 | for (let i = 0; i < x; i++) { 16 | dp[i] = [] 17 | for (let j = 0; j < y; j++) { 18 | if (obstacleGrid[i][j] === 1) { 19 | dp[i][j] = 0 20 | } else { 21 | if (i === 0 && j === 0) { 22 | dp[i][j] = 1 23 | } else if (i === 0) { 24 | dp[i][j] = dp[i][j - 1] 25 | } else if (j === 0) { 26 | dp[i][j] = dp[i - 1][j] 27 | } else { 28 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 29 | } 30 | } 31 | } 32 | } 33 | return dp[x - 1][y - 1] 34 | } 35 | -------------------------------------------------------------------------------- /easy/217.存在重复元素/217.存在重复元素.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=217 lang=javascript 3 | * 4 | * [217] 存在重复元素 5 | * 6 | * https://leetcode-cn.com/problems/contains-duplicate/description/ 7 | * 8 | * algorithms 9 | * Easy (45.88%) 10 | * Total Accepted: 41.3K 11 | * Total Submissions: 89.3K 12 | * Testcase Example: '[1,2,3,1]' 13 | * 14 | * 给定一个整数数组,判断是否存在重复元素。 15 | * 16 | * 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [1,2,3,1] 21 | * 输出: true 22 | * 23 | * 示例 2: 24 | * 25 | * 输入: [1,2,3,4] 26 | * 输出: false 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: [1,1,1,3,3,4,3,2,4,2] 31 | * 输出: true 32 | * 33 | */ 34 | /** 35 | * @param {number[]} nums 36 | * @return {boolean} 37 | */ 38 | var containsDuplicate = function(nums) { 39 | const map = {} 40 | for (let n of nums) { 41 | if (n in map) { 42 | return true 43 | } 44 | map[n] = 1 45 | } 46 | return false 47 | } 48 | -------------------------------------------------------------------------------- /easy/28.实现str-str/28.实现str-str.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=28 lang=python3 3 | # 4 | # [28] 实现strStr() 5 | # 6 | # https://leetcode-cn.com/problems/implement-strstr/description/ 7 | # 8 | # algorithms 9 | # Easy (37.50%) 10 | # Total Accepted: 35.5K 11 | # Total Submissions: 94.4K 12 | # Testcase Example: '"hello"\n"ll"' 13 | # 14 | # 实现 strStr() 函数。 15 | # 16 | # 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 17 | # (从0开始)。如果不存在,则返回  -1。 18 | # 19 | # 示例 1: 20 | # 21 | # 输入: haystack = "hello", needle = "ll" 22 | # 输出: 2 23 | # 24 | # 25 | # 示例 2: 26 | # 27 | # 输入: haystack = "aaaaa", needle = "bba" 28 | # 输出: -1 29 | # 30 | # 31 | # 说明: 32 | # 33 | # 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。 34 | # 35 | # 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。 36 | # 37 | # 38 | class Solution: 39 | def strStr(self, haystack: 'str', needle: 'str') -> 'int': 40 | return haystack.find(needle) 41 | -------------------------------------------------------------------------------- /easy/206.反转链表/206.反转链表.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=206 lang=golang 3 | * 4 | * [206] 反转链表 5 | * 6 | * https://leetcode-cn.com/problems/reverse-linked-list/description/ 7 | * 8 | * algorithms 9 | * Easy (57.57%) 10 | * Total Accepted: 35.2K 11 | * Total Submissions: 60.6K 12 | * Testcase Example: '[1,2,3,4,5]' 13 | * 14 | * 反转一个单链表。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 1->2->3->4->5->NULL 19 | * 输出: 5->4->3->2->1->NULL 20 | * 21 | * 进阶: 22 | * 你可以迭代或递归地反转链表。你能否用两种方法解决这道题? 23 | * 24 | */ 25 | /** 26 | * Definition for singly-linked list. 27 | * type ListNode struct { 28 | * Val int 29 | * Next *ListNode 30 | * } 31 | */ 32 | func reverseList(head *ListNode) *ListNode { 33 | node := head 34 | var temp *ListNode = nil 35 | for head != nil { 36 | head = head.Next 37 | node.Next = temp 38 | temp = node 39 | if head == nil { 40 | return node 41 | } 42 | node = head 43 | } 44 | return nil 45 | } 46 | 47 | -------------------------------------------------------------------------------- /easy/389.找不同/389.找不同.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=389 lang=javascript 3 | * 4 | * [389] 找不同 5 | * 6 | * https://leetcode-cn.com/problems/find-the-difference/description/ 7 | * 8 | * algorithms 9 | * Easy (54.04%) 10 | * Total Accepted: 6.5K 11 | * Total Submissions: 12K 12 | * Testcase Example: '"abcd"\n"abcde"' 13 | * 14 | * 给定两个字符串 s 和 t,它们只包含小写字母。 15 | * 16 | * 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 17 | * 18 | * 请找出在 t 中被添加的字母。 19 | * 20 | * 21 | * 22 | * 示例: 23 | * 24 | * 输入: 25 | * s = "abcd" 26 | * t = "abcde" 27 | * 28 | * 输出: 29 | * e 30 | * 31 | * 解释: 32 | * 'e' 是那个被添加的字母。 33 | * 34 | * 35 | */ 36 | /** 37 | * @param {string} s 38 | * @param {string} t 39 | * @return {character} 40 | */ 41 | var findTheDifference = function(s, t) { 42 | let res = 0 43 | const str = s + t 44 | for (let i = 0; i < str.length; i++) { 45 | res ^= str[i].charCodeAt() 46 | } 47 | return String.fromCharCode(res) 48 | } 49 | -------------------------------------------------------------------------------- /easy/434.字符串中的单词数/434.字符串中的单词数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=434 lang=python3 3 | # 4 | # [434] 字符串中的单词数 5 | # 6 | # https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/ 7 | # 8 | # algorithms 9 | # Easy (28.91%) 10 | # Total Accepted: 3.9K 11 | # Total Submissions: 13.4K 12 | # Testcase Example: '"Hello, my name is John"' 13 | # 14 | # 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。 15 | # 16 | # 请注意,你可以假定字符串里不包括任何不可打印的字符。 17 | # 18 | # 示例: 19 | # 20 | # 输入: "Hello, my name is John" 21 | # 输出: 5 22 | # 23 | # 24 | # 25 | 26 | 27 | class Solution: 28 | def countSegments(self, s: str) -> int: 29 | flag = True 30 | count = 0 31 | for n in s: 32 | if flag: 33 | if n != ' ': 34 | flag = False 35 | else: 36 | if n == ' ': 37 | count += 1 38 | flag = True 39 | return count if flag else count + 1 40 | -------------------------------------------------------------------------------- /easy/69.x-的平方根/69.x-的平方根.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=69 lang=golang 3 | * 4 | * [69] x 的平方根 5 | * 6 | * https://leetcode-cn.com/problems/sqrtx/description/ 7 | * 8 | * algorithms 9 | * Easy (34.28%) 10 | * Total Accepted: 22.7K 11 | * Total Submissions: 65.9K 12 | * Testcase Example: '4' 13 | * 14 | * 实现 int sqrt(int x) 函数。 15 | * 16 | * 计算并返回 x 的平方根,其中 x 是非负整数。 17 | * 18 | * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: 4 23 | * 输出: 2 24 | * 25 | * 26 | * 示例 2: 27 | * 28 | * 输入: 8 29 | * 输出: 2 30 | * 说明: 8 的平方根是 2.82842..., 31 | * 由于返回类型是整数,小数部分将被舍去。 32 | * 33 | * 34 | */ 35 | func mySqrt(x int) int { 36 | l := 0 37 | r := x 38 | res := 0 39 | for l <= r { 40 | mid := l + (r-l)/2 41 | if mid*mid <= x && (mid+1)*(mid+1) > x { 42 | res = mid 43 | break 44 | } else if mid*mid < x { 45 | l = mid + 1 46 | } else { 47 | r = mid - 1 48 | } 49 | } 50 | return res 51 | } 52 | -------------------------------------------------------------------------------- /medium/24.两两交换链表中的节点/24.两两交换链表中的节点.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=24 lang=python3 3 | # 4 | # [24] 两两交换链表中的节点 5 | # 6 | # https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/ 7 | # 8 | # algorithms 9 | # Medium (58.02%) 10 | # Total Accepted: 15.8K 11 | # Total Submissions: 27.2K 12 | # Testcase Example: '[1,2,3,4]' 13 | # 14 | # 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 15 | # 16 | # 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 17 | # 18 | # 19 | # 20 | # 示例: 21 | # 22 | # 给定 1->2->3->4, 你应该返回 2->1->4->3. 23 | # 24 | # 25 | # 26 | # Definition for singly-linked list. 27 | # class ListNode: 28 | # def __init__(self, x): 29 | # self.val = x 30 | # self.next = None 31 | 32 | 33 | class Solution: 34 | def swapPairs(self, head: ListNode) -> ListNode: 35 | if not head or not head.next: 36 | return head 37 | n = head.next 38 | head.next = self.swapPairs(n.next) 39 | n.next = head 40 | return n 41 | -------------------------------------------------------------------------------- /easy/125.验证回文串/125.验证回文串.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=125 lang=javascript 3 | * 4 | * [125] 验证回文串 5 | * 6 | * https://leetcode-cn.com/problems/valid-palindrome/description/ 7 | * 8 | * algorithms 9 | * Easy (37.77%) 10 | * Total Accepted: 22.3K 11 | * Total Submissions: 58.9K 12 | * Testcase Example: '"A man, a plan, a canal: Panama"' 13 | * 14 | * 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 15 | * 16 | * 说明:本题中,我们将空字符串定义为有效的回文串。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: "A man, a plan, a canal: Panama" 21 | * 输出: true 22 | * 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: "race a car" 27 | * 输出: false 28 | * 29 | * 30 | */ 31 | /** 32 | * @param {string} s 33 | * @return {boolean} 34 | */ 35 | var isPalindrome = function(s) { 36 | const str = s.replace(/[^0-9a-zA-Z]/g, '').toLowerCase() 37 | let i = 0 38 | let j = str.length - 1 39 | while (i <= j) { 40 | if (str[i++] !== str[j--]) return false 41 | } 42 | return true 43 | } 44 | -------------------------------------------------------------------------------- /easy/367.有效的完全平方数/367.有效的完全平方数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=367 lang=python3 3 | # 4 | # [367] 有效的完全平方数 5 | # 6 | # https://leetcode-cn.com/problems/valid-perfect-square/description/ 7 | # 8 | # algorithms 9 | # Easy (39.57%) 10 | # Total Accepted: 6.3K 11 | # Total Submissions: 15.8K 12 | # Testcase Example: '16' 13 | # 14 | # 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 15 | # 16 | # 说明:不要使用任何内置的库函数,如  sqrt。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入:16 21 | # 输出:True 22 | # 23 | # 示例 2: 24 | # 25 | # 输入:14 26 | # 输出:False 27 | # 28 | # 29 | # 30 | 31 | 32 | class Solution: 33 | def isPerfectSquare(self, num: int) -> bool: 34 | l = 1 35 | r = num 36 | while l <= r: 37 | mid = l + (r - l) // 2 38 | if mid ** 2 == num: 39 | return True 40 | elif mid ** 2 < num: 41 | l = mid + 1 42 | else: 43 | r = mid - 1 44 | return False 45 | -------------------------------------------------------------------------------- /easy/485.最大连续-1-的个数/485.最大连续-1-的个数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=485 lang=golang 3 | * 4 | * [485] 最大连续1的个数 5 | * 6 | * https://leetcode-cn.com/problems/max-consecutive-ones/description/ 7 | * 8 | * algorithms 9 | * Easy (52.18%) 10 | * Total Accepted: 9.4K 11 | * Total Submissions: 18K 12 | * Testcase Example: '[1,0,1,1,0,1]' 13 | * 14 | * 给定一个二进制数组, 计算其中最大连续1的个数。 15 | * 16 | * 示例 1: 17 | * 18 | * 19 | * 输入: [1,1,0,1,1,1] 20 | * 输出: 3 21 | * 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 22 | * 23 | * 24 | * 注意: 25 | * 26 | * 27 | * 输入的数组只包含 0 和1。 28 | * 输入数组的长度是正整数,且不超过 10,000。 29 | * 30 | * 31 | */ 32 | func findMaxConsecutiveOnes(nums []int) int { 33 | ans, cur := 0, 0 34 | for _, n := range nums { 35 | if (n & 1) == 1 { 36 | cur++ 37 | ans = max(ans, cur) 38 | } else { 39 | cur = 0 40 | } 41 | } 42 | return ans 43 | } 44 | 45 | func max(x, y int) int { 46 | if x > y { 47 | return x 48 | } 49 | return y 50 | } 51 | -------------------------------------------------------------------------------- /medium/96.不同的二叉搜索树/96.不同的二叉搜索树.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=96 lang=javascript 3 | * 4 | * [96] 不同的二叉搜索树 5 | * 6 | * https://leetcode-cn.com/problems/unique-binary-search-trees/description/ 7 | * 8 | * algorithms 9 | * Medium (59.07%) 10 | * Likes: 149 11 | * Dislikes: 0 12 | * Total Accepted: 8.2K 13 | * Total Submissions: 13.7K 14 | * Testcase Example: '3' 15 | * 16 | * 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 17 | * 18 | * 示例: 19 | * 20 | * 输入: 3 21 | * 输出: 5 22 | * 解释: 23 | * 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 24 | * 25 | * ⁠ 1 3 3 2 1 26 | * ⁠ \ / / / \ \ 27 | * ⁠ 3 2 1 1 3 2 28 | * ⁠ / / \ \ 29 | * ⁠ 2 1 2 3 30 | * 31 | */ 32 | /** 33 | * @param {number} n 34 | * @return {number} 35 | */ 36 | var numTrees = function(n) { 37 | if (n === 1) return 1 38 | return (numTrees(n - 1) * (4 * n - 2)) / (n + 1) 39 | } 40 | -------------------------------------------------------------------------------- /easy/171.excel表列序号/171.excel表列序号.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=171 lang=python3 3 | # 4 | # [171] Excel表列序号 5 | # 6 | # https://leetcode-cn.com/problems/excel-sheet-column-number/description/ 7 | # 8 | # algorithms 9 | # Easy (63.07%) 10 | # Total Accepted: 9K 11 | # Total Submissions: 14.2K 12 | # Testcase Example: '"A"' 13 | # 14 | # 给定一个Excel表格中的列名称,返回其相应的列序号。 15 | # 16 | # 例如, 17 | # 18 | # ⁠ A -> 1 19 | # ⁠ B -> 2 20 | # ⁠ C -> 3 21 | # ⁠ ... 22 | # ⁠ Z -> 26 23 | # ⁠ AA -> 27 24 | # ⁠ AB -> 28 25 | # ⁠ ... 26 | # 27 | # 28 | # 示例 1: 29 | # 30 | # 输入: "A" 31 | # 输出: 1 32 | # 33 | # 34 | # 示例 2: 35 | # 36 | # 输入: "AB" 37 | # 输出: 28 38 | # 39 | # 40 | # 示例 3: 41 | # 42 | # 输入: "ZY" 43 | # 输出: 701 44 | # 45 | # 致谢: 46 | # 特别感谢 @ts 添加此问题并创建所有测试用例。 47 | # 48 | # 49 | 50 | 51 | class Solution: 52 | def titleToNumber(self, s: str) -> int: 53 | res = 0 54 | for i in s: 55 | res = 26 * res + ord(i) - 64 56 | return res 57 | -------------------------------------------------------------------------------- /easy/204.计数质数/204.计数质数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=204 lang=javascript 3 | * 4 | * [204] 计数质数 5 | * 6 | * https://leetcode-cn.com/problems/count-primes/description/ 7 | * 8 | * algorithms 9 | * Easy (25.80%) 10 | * Total Accepted: 12.4K 11 | * Total Submissions: 47.4K 12 | * Testcase Example: '10' 13 | * 14 | * 统计所有小于非负整数 n 的质数的数量。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 10 19 | * 输出: 4 20 | * 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 21 | * 22 | * 23 | */ 24 | /** 25 | * @param {number} n 26 | * @return {number} 27 | */ 28 | var countPrimes = function(n) { 29 | const isPrime = [] 30 | for (let i = 2; i < n; i++) { 31 | isPrime[i] = true 32 | } 33 | for (let i = 2; i * i < n; i++) { 34 | if (!isPrime[i]) continue 35 | for (let j = i * i; j < n; j += i) { 36 | isPrime[j] = false 37 | } 38 | } 39 | let count = 0 40 | for (let i = 2; i < n; i++) { 41 | if (isPrime[i]) count++ 42 | } 43 | return count 44 | } 45 | -------------------------------------------------------------------------------- /easy/485.最大连续-1-的个数/485.最大连续-1-的个数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=485 lang=javascript 3 | * 4 | * [485] 最大连续1的个数 5 | * 6 | * https://leetcode-cn.com/problems/max-consecutive-ones/description/ 7 | * 8 | * algorithms 9 | * Easy (52.18%) 10 | * Total Accepted: 9.4K 11 | * Total Submissions: 18K 12 | * Testcase Example: '[1,0,1,1,0,1]' 13 | * 14 | * 给定一个二进制数组, 计算其中最大连续1的个数。 15 | * 16 | * 示例 1: 17 | * 18 | * 19 | * 输入: [1,1,0,1,1,1] 20 | * 输出: 3 21 | * 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 22 | * 23 | * 24 | * 注意: 25 | * 26 | * 27 | * 输入的数组只包含 0 和1。 28 | * 输入数组的长度是正整数,且不超过 10,000。 29 | * 30 | * 31 | */ 32 | /** 33 | * @param {number[]} nums 34 | * @return {number} 35 | */ 36 | var findMaxConsecutiveOnes = function(nums) { 37 | let res = 0 38 | let cur = 0 39 | for (const n of nums) { 40 | if (n & 1) { 41 | cur++ 42 | res = Math.max(res, cur) 43 | } else { 44 | cur = 0 45 | } 46 | } 47 | return res 48 | } 49 | -------------------------------------------------------------------------------- /easy/88.合并两个有序数组/88.合并两个有序数组.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=88 lang=python3 3 | # 4 | # [88] 合并两个有序数组 5 | # 6 | # https://leetcode-cn.com/problems/merge-sorted-array/description/ 7 | # 8 | # algorithms 9 | # Easy (42.33%) 10 | # Total Accepted: 25.8K 11 | # Total Submissions: 60.8K 12 | # Testcase Example: '[1,2,3,0,0,0]\n3\n[2,5,6]\n3' 13 | # 14 | # 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。 15 | # 16 | # 说明: 17 | # 18 | # 19 | # 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。 20 | # 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。 21 | # 22 | # 23 | # 示例: 24 | # 25 | # 输入: 26 | # nums1 = [1,2,3,0,0,0], m = 3 27 | # nums2 = [2,5,6], n = 3 28 | # 29 | # 输出: [1,2,2,3,5,6] 30 | # 31 | # 32 | class Solution: 33 | def merge(self, nums1: 'List[int]', m: 'int', nums2: 'List[int]', n: 'int') -> 'None': 34 | """ 35 | Do not return anything, modify nums1 in-place instead. 36 | """ 37 | nums1[m:] = nums2[:n] 38 | nums1.sort() 39 | -------------------------------------------------------------------------------- /easy/119.杨辉三角-ii/119.杨辉三角-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=119 lang=javascript 3 | * 4 | * [119] 杨辉三角 II 5 | * 6 | * https://leetcode-cn.com/problems/pascals-triangle-ii/description/ 7 | * 8 | * algorithms 9 | * Easy (53.57%) 10 | * Total Accepted: 9.7K 11 | * Total Submissions: 18K 12 | * Testcase Example: '3' 13 | * 14 | * 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 15 | * 16 | * 17 | * 18 | * 在杨辉三角中,每个数是它左上方和右上方的数的和。 19 | * 20 | * 示例: 21 | * 22 | * 输入: 3 23 | * 输出: [1,3,3,1] 24 | * 25 | * 26 | * 进阶: 27 | * 28 | * 你可以优化你的算法到 O(k) 空间复杂度吗? 29 | * 30 | */ 31 | /** 32 | * @param {number} rowIndex 33 | * @return {number[]} 34 | */ 35 | var getRow = function(rowIndex) { 36 | let res = [1] 37 | let cur = [] 38 | for (let i = 0; i < rowIndex; i++) { 39 | for (let j = 0; j <= res.length; j++) { 40 | cur[j] = (res[j] ? res[j] : 0) + (res[j - 1] ? res[j - 1] : 0) 41 | } 42 | res = cur 43 | cur = [] 44 | } 45 | return res 46 | } 47 | -------------------------------------------------------------------------------- /easy/69.x-的平方根/69.x-的平方根.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=69 lang=python3 3 | # 4 | # [69] x 的平方根 5 | # 6 | # https://leetcode-cn.com/problems/sqrtx/description/ 7 | # 8 | # algorithms 9 | # Easy (34.28%) 10 | # Total Accepted: 22.7K 11 | # Total Submissions: 65.9K 12 | # Testcase Example: '4' 13 | # 14 | # 实现 int sqrt(int x) 函数。 15 | # 16 | # 计算并返回 x 的平方根,其中 x 是非负整数。 17 | # 18 | # 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: 4 23 | # 输出: 2 24 | # 25 | # 26 | # 示例 2: 27 | # 28 | # 输入: 8 29 | # 输出: 2 30 | # 说明: 8 的平方根是 2.82842..., 31 | # 由于返回类型是整数,小数部分将被舍去。 32 | # 33 | # 34 | # 35 | class Solution: 36 | def mySqrt(self, x: 'int') -> 'int': 37 | l = 0 38 | r = x 39 | while l <= x: 40 | mid = l + (r - l) // 2 41 | if mid ** 2 <= x and (mid + 1) ** 2 > x: 42 | return mid 43 | elif mid ** 2 > x: 44 | r = mid - 1 45 | else: 46 | l = mid + 1 47 | -------------------------------------------------------------------------------- /medium/24.两两交换链表中的节点/24.两两交换链表中的节点.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=24 lang=javascript 3 | * 4 | * [24] 两两交换链表中的节点 5 | * 6 | * https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/ 7 | * 8 | * algorithms 9 | * Medium (58.02%) 10 | * Total Accepted: 15.8K 11 | * Total Submissions: 27.2K 12 | * Testcase Example: '[1,2,3,4]' 13 | * 14 | * 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 15 | * 16 | * 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 17 | * 18 | * 19 | * 20 | * 示例: 21 | * 22 | * 给定 1->2->3->4, 你应该返回 2->1->4->3. 23 | * 24 | * 25 | */ 26 | /** 27 | * Definition for singly-linked list. 28 | * function ListNode(val) { 29 | * this.val = val; 30 | * this.next = null; 31 | * } 32 | */ 33 | /** 34 | * @param {ListNode} head 35 | * @return {ListNode} 36 | */ 37 | var swapPairs = function(head) { 38 | if (!head || !head.next) return head 39 | const { next } = head 40 | head.next = swapPairs(next.next) 41 | next.next = head 42 | return next 43 | } 44 | -------------------------------------------------------------------------------- /medium/55.跳跃游戏/55.跳跃游戏.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=55 lang=golang 3 | * 4 | * [55] 跳跃游戏 5 | * 6 | * https://leetcode-cn.com/problems/jump-game/description/ 7 | * 8 | * algorithms 9 | * Medium (34.48%) 10 | * Total Accepted: 15.5K 11 | * Total Submissions: 44.6K 12 | * Testcase Example: '[2,3,1,1,4]' 13 | * 14 | * 给定一个非负整数数组,你最初位于数组的第一个位置。 15 | * 16 | * 数组中的每个元素代表你在该位置可以跳跃的最大长度。 17 | * 18 | * 判断你是否能够到达最后一个位置。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [2,3,1,1,4] 23 | * 输出: true 24 | * 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 25 | * 26 | * 27 | * 示例 2: 28 | * 29 | * 输入: [3,2,1,0,4] 30 | * 输出: false 31 | * 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 32 | * 33 | * 34 | */ 35 | func canJump(nums []int) bool { 36 | n := 1 37 | for i := len(nums) - 2; i >= 0; i-- { 38 | if nums[i] >= n { 39 | n = 1 40 | } else { 41 | n++ 42 | } 43 | if i == 0 && n > 1 { 44 | return false 45 | } 46 | } 47 | return true 48 | } 49 | 50 | -------------------------------------------------------------------------------- /medium/79.单词搜索/79.单词搜索.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=79 lang=python3 3 | # 4 | # [79] 单词搜索 5 | # 6 | 7 | 8 | class Solution: 9 | def exist(self, board: List[List[str]], word: str) -> bool: 10 | for i in range(len(board)): 11 | for j in range(len(board[i])): 12 | if self.dfs(board, i, j, word, 0): 13 | return True 14 | return False 15 | 16 | def dfs(self, board, i, j, word, k): 17 | if k == len(word): 18 | return True 19 | if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or board[i][j] != word[k]: 20 | return False 21 | s = board[i][j] 22 | board[i][j] = '*' 23 | res = self.dfs(board, i+1, j, word, k+1) or self.dfs(board, i-1, j, word, k + 24 | 1)or self.dfs(board, i, j+1, word, k+1)or self.dfs(board, i, j-1, word, k+1) 25 | board[i][j] = s 26 | return res 27 | -------------------------------------------------------------------------------- /easy/83.删除排序链表中的重复元素/83.删除排序链表中的重复元素.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=83 lang=golang 3 | * 4 | * [83] 删除排序链表中的重复元素 5 | * 6 | * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/ 7 | * 8 | * algorithms 9 | * Easy (43.50%) 10 | * Total Accepted: 16.1K 11 | * Total Submissions: 37.1K 12 | * Testcase Example: '[1,1,2]' 13 | * 14 | * 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 1->1->2 19 | * 输出: 1->2 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 1->1->2->3->3 25 | * 输出: 1->2->3 26 | * 27 | */ 28 | /** 29 | * Definition for singly-linked list. 30 | * type ListNode struct { 31 | * Val int 32 | * Next *ListNode 33 | * } 34 | */ 35 | func deleteDuplicates(head *ListNode) *ListNode { 36 | node := head 37 | for head != nil && head.Next != nil { 38 | if head.Val == head.Next.Val { 39 | head.Next = head.Next.Next 40 | } else { 41 | head = head.Next 42 | } 43 | } 44 | return node 45 | } 46 | -------------------------------------------------------------------------------- /medium/81.搜索旋转排序数组-ii/81.搜索旋转排序数组-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=81 lang=javascript 3 | * 4 | * [81] 搜索旋转排序数组 II 5 | */ 6 | /** 7 | * @param {number[]} nums 8 | * @param {number} target 9 | * @return {boolean} 10 | */ 11 | var search = function(nums, target, i = 0, j = nums.length - 1) { 12 | if (j < 0) return false 13 | const mid = (i + j) >> 1 14 | if (nums[mid] === target || nums[i] === target || nums[j] === target) 15 | return true 16 | else if (mid === i) return false 17 | else if (nums[i] === nums[mid] && nums[mid] === nums[j]) 18 | return search(nums, target, i, mid - 1) || search(nums, target, mid, j) 19 | else if (nums[mid] >= nums[i]) 20 | return target > nums[i] && target < nums[mid] 21 | ? search(nums, target, i, mid - 1) 22 | : search(nums, target, mid + 1, j) 23 | else 24 | return target > nums[mid] && target < nums[i] 25 | ? search(nums, target, mid + 1, j) 26 | : search(nums, target, i, mid - 1) 27 | } 28 | -------------------------------------------------------------------------------- /easy/1.两数之和/1.两数之和.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=1 lang=javascript 3 | * 4 | * [1] 两数之和 5 | * 6 | * https://leetcode-cn.com/problems/two-sum/description/ 7 | * 8 | * algorithms 9 | * Easy (44.37%) 10 | * Total Accepted: 238.5K 11 | * Total Submissions: 537.5K 12 | * Testcase Example: '[2,7,11,15]\n9' 13 | * 14 | * 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 15 | * 16 | * 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 17 | * 18 | * 示例: 19 | * 20 | * 给定 nums = [2, 7, 11, 15], target = 9 21 | * 22 | * 因为 nums[0] + nums[1] = 2 + 7 = 9 23 | * 所以返回 [0, 1] 24 | * 25 | * 26 | */ 27 | /** 28 | * @param {number[]} nums 29 | * @param {number} target 30 | * @return {number[]} 31 | */ 32 | var twoSum = function(nums, target) { 33 | const map = {} 34 | for (let i = 0; i < nums.length; i++) { 35 | const n = nums[i] 36 | if (target - n in map) { 37 | return [map[target - n], i] 38 | } else { 39 | map[n] = i 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /easy/171.excel表列序号/171.excel表列序号.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=171 lang=golang 3 | * 4 | * [171] Excel表列序号 5 | * 6 | * https://leetcode-cn.com/problems/excel-sheet-column-number/description/ 7 | * 8 | * algorithms 9 | * Easy (63.07%) 10 | * Total Accepted: 9K 11 | * Total Submissions: 14.2K 12 | * Testcase Example: '"A"' 13 | * 14 | * 给定一个Excel表格中的列名称,返回其相应的列序号。 15 | * 16 | * 例如, 17 | * 18 | * ⁠ A -> 1 19 | * ⁠ B -> 2 20 | * ⁠ C -> 3 21 | * ⁠ ... 22 | * ⁠ Z -> 26 23 | * ⁠ AA -> 27 24 | * ⁠ AB -> 28 25 | * ⁠ ... 26 | * 27 | * 28 | * 示例 1: 29 | * 30 | * 输入: "A" 31 | * 输出: 1 32 | * 33 | * 34 | * 示例 2: 35 | * 36 | * 输入: "AB" 37 | * 输出: 28 38 | * 39 | * 40 | * 示例 3: 41 | * 42 | * 输入: "ZY" 43 | * 输出: 701 44 | * 45 | * 致谢: 46 | * 特别感谢 @ts 添加此问题并创建所有测试用例。 47 | * 48 | */ 49 | func titleToNumber(s string) int { 50 | res := 0 51 | for _, str := range s { 52 | res = res*26 + int(str) - 65 + 1 53 | } 54 | return res 55 | } 56 | 57 | -------------------------------------------------------------------------------- /easy/283.移动零/283.移动零.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=283 lang=javascript 3 | * 4 | * [283] 移动零 5 | * 6 | * https://leetcode-cn.com/problems/move-zeroes/description/ 7 | * 8 | * algorithms 9 | * Easy (52.53%) 10 | * Total Accepted: 34.1K 11 | * Total Submissions: 64.9K 12 | * Testcase Example: '[0,1,0,3,12]' 13 | * 14 | * 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 15 | * 16 | * 示例: 17 | * 18 | * 输入: [0,1,0,3,12] 19 | * 输出: [1,3,12,0,0] 20 | * 21 | * 说明: 22 | * 23 | * 24 | * 必须在原数组上操作,不能拷贝额外的数组。 25 | * 尽量减少操作次数。 26 | * 27 | * 28 | */ 29 | /** 30 | * @param {number[]} nums 31 | * @return {void} Do not return anything, modify nums in-place instead. 32 | */ 33 | var moveZeroes = function(nums) { 34 | let [slow, fast] = [0, 0] 35 | const len = nums.length 36 | for (fast = 0; fast < len; fast++) { 37 | if (nums[fast] !== 0) { 38 | nums[slow++] = nums[fast] 39 | } 40 | } 41 | while (slow < len) { 42 | nums[slow++] = 0 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /medium/63.不同路径-ii/63.不同路径-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=63 lang=python3 3 | # 4 | # [63] 不同路径 II 5 | # 6 | 7 | 8 | class Solution: 9 | def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: 10 | if len(obstacleGrid) == 0: 11 | return 0 12 | x = len(obstacleGrid) 13 | y = len(obstacleGrid[0]) 14 | dp = [[]] * x 15 | for i in range(x): 16 | dp[i] = [0] * y 17 | for j in range(y): 18 | if obstacleGrid[i][j] == 1: 19 | dp[i][j] = 0 20 | else: 21 | if i == 0 and j == 0: 22 | dp[i][j] = 1 23 | elif i == 0: 24 | dp[i][j] = dp[i][j - 1] 25 | elif j == 0: 26 | dp[i][j] = dp[i - 1][j] 27 | else: 28 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 29 | return dp[x - 1][y - 1] 30 | -------------------------------------------------------------------------------- /easy/203.移除链表元素/203.移除链表元素.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=203 lang=golang 3 | * 4 | * [203] 移除链表元素 5 | * 6 | * https://leetcode-cn.com/problems/remove-linked-list-elements/description/ 7 | * 8 | * algorithms 9 | * Easy (38.84%) 10 | * Total Accepted: 16.2K 11 | * Total Submissions: 41.5K 12 | * Testcase Example: '[1,2,6,3,4,5,6]\n6' 13 | * 14 | * 删除链表中等于给定值 val 的所有节点。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 1->2->6->3->4->5->6, val = 6 19 | * 输出: 1->2->3->4->5 20 | * 21 | * 22 | */ 23 | /** 24 | * Definition for singly-linked list. 25 | * type ListNode struct { 26 | * Val int 27 | * Next *ListNode 28 | * } 29 | */ 30 | func removeElements(head *ListNode, val int) *ListNode { 31 | header := new(ListNode) 32 | header.Val = -1 33 | header.Next = head 34 | node := header 35 | for node.Next != nil { 36 | if node.Next.Val == val { 37 | node.Next = node.Next.Next 38 | } else { 39 | node = node.Next 40 | } 41 | } 42 | return header.Next 43 | } 44 | 45 | -------------------------------------------------------------------------------- /easy/7.整数反转/7.整数反转.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=7 lang=golang 3 | * 4 | * [7] 整数反转 5 | * 6 | * https://leetcode-cn.com/problems/reverse-integer/description/ 7 | * 8 | * algorithms 9 | * Easy (31.41%) 10 | * Total Accepted: 79.4K 11 | * Total Submissions: 252.8K 12 | * Testcase Example: '123' 13 | * 14 | * 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 123 19 | * 输出: 321 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: -123 25 | * 输出: -321 26 | * 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 120 31 | * 输出: 21 32 | * 33 | * 34 | * 注意: 35 | * 36 | * 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。 37 | * 38 | */ 39 | import "math" 40 | 41 | func reverse(x int) int { 42 | res := 0 43 | for x != 0 { 44 | n := x % 10 45 | res = res*10 + n 46 | if float64(res) < math.Pow(-2, 31) || float64(res) > math.Pow(2, 31)-1 { 47 | res = 0 48 | break 49 | } 50 | x = (x - n) / 10 51 | } 52 | return res 53 | } 54 | -------------------------------------------------------------------------------- /medium/22.括号生成/22.括号生成.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=22 lang=python3 3 | # 4 | # [22] 括号生成 5 | # 6 | # https://leetcode-cn.com/problems/generate-parentheses/description/ 7 | # 8 | # algorithms 9 | # Medium (68.93%) 10 | # Total Accepted: 16.8K 11 | # Total Submissions: 24.3K 12 | # Testcase Example: '3' 13 | # 14 | # 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 15 | # 16 | # 例如,给出 n = 3,生成结果为: 17 | # 18 | # [ 19 | # ⁠ "((()))", 20 | # ⁠ "(()())", 21 | # ⁠ "(())()", 22 | # ⁠ "()(())", 23 | # ⁠ "()()()" 24 | # ] 25 | # 26 | # 27 | # 28 | 29 | 30 | class Solution: 31 | def generateParenthesis(self, n: int) -> List[str]: 32 | ans = [] 33 | 34 | def backtrack(s='', l=0, r=0): 35 | if len(s) == 2 * n: 36 | ans.append(s) 37 | return 38 | if l < n: 39 | backtrack(s + '(', l + 1, r) 40 | if r < l: 41 | backtrack(s + ')', l, r + 1) 42 | backtrack() 43 | return ans 44 | -------------------------------------------------------------------------------- /medium/22.括号生成/22.括号生成.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=22 lang=golang 3 | * 4 | * [22] 括号生成 5 | * 6 | * https://leetcode-cn.com/problems/generate-parentheses/description/ 7 | * 8 | * algorithms 9 | * Medium (68.93%) 10 | * Total Accepted: 16.8K 11 | * Total Submissions: 24.3K 12 | * Testcase Example: '3' 13 | * 14 | * 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 15 | * 16 | * 例如,给出 n = 3,生成结果为: 17 | * 18 | * [ 19 | * ⁠ "((()))", 20 | * ⁠ "(()())", 21 | * ⁠ "(())()", 22 | * ⁠ "()(())", 23 | * ⁠ "()()()" 24 | * ] 25 | * 26 | * 27 | */ 28 | type fp func(string, int, int) 29 | 30 | func generateParenthesis(n int) []string { 31 | ans := []string{} 32 | var backtrack fp 33 | backtrack = func(s string, l, r int) { 34 | if len(s) == 2*n { 35 | ans = append(ans, s) 36 | return 37 | } 38 | if l < n { 39 | backtrack(s+"(", l+1, r) 40 | } 41 | if r < l { 42 | backtrack(s+")", l, r+1) 43 | } 44 | } 45 | backtrack("", 0, 0) 46 | return ans 47 | } 48 | 49 | -------------------------------------------------------------------------------- /easy/367.有效的完全平方数/367.有效的完全平方数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=367 lang=javascript 3 | * 4 | * [367] 有效的完全平方数 5 | * 6 | * https://leetcode-cn.com/problems/valid-perfect-square/description/ 7 | * 8 | * algorithms 9 | * Easy (39.57%) 10 | * Total Accepted: 6.3K 11 | * Total Submissions: 15.8K 12 | * Testcase Example: '16' 13 | * 14 | * 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 15 | * 16 | * 说明:不要使用任何内置的库函数,如  sqrt。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入:16 21 | * 输出:True 22 | * 23 | * 示例 2: 24 | * 25 | * 输入:14 26 | * 输出:False 27 | * 28 | * 29 | */ 30 | /** 31 | * @param {number} num 32 | * @return {boolean} 33 | */ 34 | var isPerfectSquare = function(num) { 35 | let l = 0 36 | let r = num 37 | while (l <= r) { 38 | const mid = (l + (r - l) / 2) | 0 39 | if (mid ** 2 === num) { 40 | return true 41 | } else if (mid ** 2 < num) { 42 | l = mid + 1 43 | } else { 44 | r = mid - 1 45 | } 46 | } 47 | return false 48 | } 49 | -------------------------------------------------------------------------------- /easy/58.最后一个单词的长度/58.最后一个单词的长度.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=58 lang=javascript 3 | * 4 | * [58] 最后一个单词的长度 5 | * 6 | * https://leetcode-cn.com/problems/length-of-last-word/description/ 7 | * 8 | * algorithms 9 | * Easy (28.61%) 10 | * Total Accepted: 16.7K 11 | * Total Submissions: 58.5K 12 | * Testcase Example: '"Hello World"' 13 | * 14 | * 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。 15 | * 16 | * 如果不存在最后一个单词,请返回 0 。 17 | * 18 | * 说明:一个单词是指由字母组成,但不包含任何空格的字符串。 19 | * 20 | * 示例: 21 | * 22 | * 输入: "Hello World" 23 | * 输出: 5 24 | * 25 | * 26 | */ 27 | /** 28 | * @param {string} s 29 | * @return {number} 30 | */ 31 | var lengthOfLastWord = function(s) { 32 | let res = 0 33 | let flag = true 34 | for (let i = s.length - 1; i >= 0; i--) { 35 | if (s[i] === ' ') { 36 | if (flag) { 37 | s = s.slice(0, i) 38 | } else { 39 | return res 40 | } 41 | } else { 42 | flag = false 43 | res++ 44 | } 45 | } 46 | return res 47 | } 48 | -------------------------------------------------------------------------------- /easy/58.最后一个单词的长度/58.最后一个单词的长度.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=58 lang=python3 3 | # 4 | # [58] 最后一个单词的长度 5 | # 6 | # https://leetcode-cn.com/problems/length-of-last-word/description/ 7 | # 8 | # algorithms 9 | # Easy (28.61%) 10 | # Total Accepted: 16.7K 11 | # Total Submissions: 58.5K 12 | # Testcase Example: '"Hello World"' 13 | # 14 | # 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。 15 | # 16 | # 如果不存在最后一个单词,请返回 0 。 17 | # 18 | # 说明:一个单词是指由字母组成,但不包含任何空格的字符串。 19 | # 20 | # 示例: 21 | # 22 | # 输入: "Hello World" 23 | # 输出: 5 24 | # 25 | # 26 | # 27 | class Solution: 28 | def lengthOfLastWord(self, s: 'str') -> 'int': 29 | res = 0 30 | flag = True 31 | i = len(s) - 1 32 | while i >= 0: 33 | if s[i] == ' ': 34 | if flag: 35 | s = s[0:i] 36 | else: 37 | return res 38 | else: 39 | flag = False 40 | res += 1 41 | i -= 1 42 | return res 43 | -------------------------------------------------------------------------------- /easy/70.爬楼梯/70.爬楼梯.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=70 lang=javascript 3 | * 4 | * [70] 爬楼梯 5 | * 6 | * https://leetcode-cn.com/problems/climbing-stairs/description/ 7 | * 8 | * algorithms 9 | * Easy (43.84%) 10 | * Total Accepted: 29.6K 11 | * Total Submissions: 67.4K 12 | * Testcase Example: '2' 13 | * 14 | * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 15 | * 16 | * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 17 | * 18 | * 注意:给定 n 是一个正整数。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: 2 23 | * 输出: 2 24 | * 解释: 有两种方法可以爬到楼顶。 25 | * 1. 1 阶 + 1 阶 26 | * 2. 2 阶 27 | * 28 | * 示例 2: 29 | * 30 | * 输入: 3 31 | * 输出: 3 32 | * 解释: 有三种方法可以爬到楼顶。 33 | * 1. 1 阶 + 1 阶 + 1 阶 34 | * 2. 1 阶 + 2 阶 35 | * 3. 2 阶 + 1 阶 36 | * 37 | * 38 | */ 39 | /** 40 | * @param {number} n 41 | * @return {number} 42 | */ 43 | var climbStairs = function(n, cache = {}) { 44 | if (cache[n]) { 45 | return cache[n] 46 | } 47 | return (cache[n] = 48 | n < 2 ? 1 : climbStairs(n - 1, cache) + climbStairs(n - 2, cache)) 49 | } 50 | -------------------------------------------------------------------------------- /easy/219.存在重复元素-ii/219.存在重复元素-ii.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=219 lang=golang 3 | * 4 | * [219] 存在重复元素 II 5 | * 6 | * https://leetcode-cn.com/problems/contains-duplicate-ii/description/ 7 | * 8 | * algorithms 9 | * Easy (32.71%) 10 | * Total Accepted: 8.9K 11 | * Total Submissions: 27K 12 | * Testcase Example: '[1,2,3,1]\n3' 13 | * 14 | * 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 15 | * 的差的绝对值最大为 k。 16 | * 17 | * 示例 1: 18 | * 19 | * 输入: nums = [1,2,3,1], k = 3 20 | * 输出: true 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: nums = [1,0,1,1], k = 1 25 | * 输出: true 26 | * 27 | * 示例 3: 28 | * 29 | * 输入: nums = [1,2,3,1,2,3], k = 2 30 | * 输出: false 31 | * 32 | */ 33 | func containsNearbyDuplicate(nums []int, k int) bool { 34 | m := map[int]int{} 35 | for i, n := range nums { 36 | if _, ok := m[n]; ok { 37 | if i-m[n] <= k { 38 | return true 39 | } 40 | m[n] = i 41 | } else { 42 | m[n] = i 43 | } 44 | } 45 | return false 46 | } 47 | 48 | -------------------------------------------------------------------------------- /easy/119.杨辉三角-ii/119.杨辉三角-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=119 lang=python3 3 | # 4 | # [119] 杨辉三角 II 5 | # 6 | # https://leetcode-cn.com/problems/pascals-triangle-ii/description/ 7 | # 8 | # algorithms 9 | # Easy (53.57%) 10 | # Total Accepted: 9.7K 11 | # Total Submissions: 18K 12 | # Testcase Example: '3' 13 | # 14 | # 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 15 | # 16 | # 17 | # 18 | # 在杨辉三角中,每个数是它左上方和右上方的数的和。 19 | # 20 | # 示例: 21 | # 22 | # 输入: 3 23 | # 输出: [1,3,3,1] 24 | # 25 | # 26 | # 进阶: 27 | # 28 | # 你可以优化你的算法到 O(k) 空间复杂度吗? 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def getRow(self, rowIndex: 'int') -> 'List[int]': 35 | res = [1] 36 | while rowIndex > 0: 37 | cur = [] 38 | for i in range(len(res) + 1): 39 | if i == 0 or i == len(res): 40 | cur.append(1) 41 | else: 42 | cur.append(res[i] + res[i - 1]) 43 | res = cur 44 | rowIndex -= 1 45 | return res 46 | -------------------------------------------------------------------------------- /easy/182.查找重复的电子邮箱/182.查找重复的电子邮箱.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- @lc app=leetcode.cn id=182 lang=mysql 3 | -- 4 | -- [182] 查找重复的电子邮箱 5 | -- 6 | -- https://leetcode-cn.com/problems/duplicate-emails/description/ 7 | -- 8 | -- database 9 | -- Easy (71.23%) 10 | -- Total Accepted: 16.2K 11 | -- Total Submissions: 22.8K 12 | -- Testcase Example: '{"headers": {"Person": ["Id", "Email"]}, "rows": {"Person": [[1, "a@b.com"], [2, "c@d.com"], [3, "a@b.com"]]}}' 13 | -- 14 | -- 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。 15 | -- 16 | -- 示例: 17 | -- 18 | -- +----+---------+ 19 | -- | Id | Email | 20 | -- +----+---------+ 21 | -- | 1 | a@b.com | 22 | -- | 2 | c@d.com | 23 | -- | 3 | a@b.com | 24 | -- +----+---------+ 25 | -- 26 | -- 27 | -- 根据以上输入,你的查询应返回以下结果: 28 | -- 29 | -- +---------+ 30 | -- | Email | 31 | -- +---------+ 32 | -- | a@b.com | 33 | -- +---------+ 34 | -- 35 | -- 36 | -- 说明:所有电子邮箱都是小写字母。 37 | -- 38 | -- 39 | # Write your MySQL query statement below 40 | select Email from Person 41 | group by Email having count(Email) > 1 42 | 43 | -------------------------------------------------------------------------------- /easy/206.反转链表/206.反转链表.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=206 lang=javascript 3 | * 4 | * [206] 反转链表 5 | * 6 | * https://leetcode-cn.com/problems/reverse-linked-list/description/ 7 | * 8 | * algorithms 9 | * Easy (57.57%) 10 | * Total Accepted: 35.2K 11 | * Total Submissions: 60.6K 12 | * Testcase Example: '[1,2,3,4,5]' 13 | * 14 | * 反转一个单链表。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 1->2->3->4->5->NULL 19 | * 输出: 5->4->3->2->1->NULL 20 | * 21 | * 进阶: 22 | * 你可以迭代或递归地反转链表。你能否用两种方法解决这道题? 23 | * 24 | */ 25 | /** 26 | * Definition for singly-linked list. 27 | * function ListNode(val) { 28 | * this.val = val; 29 | * this.next = null; 30 | * } 31 | */ 32 | /** 33 | * @param {ListNode} head 34 | * @return {ListNode} 35 | */ 36 | var reverseList = function(head) { 37 | let node = head 38 | let temp = null 39 | while (head) { 40 | head = head.next 41 | node.next = temp 42 | temp = node 43 | if (!head) return node 44 | node = head 45 | } 46 | return null 47 | } 48 | -------------------------------------------------------------------------------- /easy/448.找到所有数组中消失的数字/448.找到所有数组中消失的数字.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=448 lang=python3 3 | # 4 | # [448] 找到所有数组中消失的数字 5 | # 6 | # https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/ 7 | # 8 | # algorithms 9 | # Easy (46.40%) 10 | # Total Accepted: 5.6K 11 | # Total Submissions: 12.1K 12 | # Testcase Example: '[4,3,2,7,8,2,3,1]' 13 | # 14 | # 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 15 | # 16 | # 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 17 | # 18 | # 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 19 | # 20 | # 示例: 21 | # 22 | # 23 | # 输入: 24 | # [4,3,2,7,8,2,3,1] 25 | # 26 | # 输出: 27 | # [5,6] 28 | # 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def findDisappearedNumbers(self, nums: List[int]) -> List[int]: 35 | ans = [] 36 | for n in nums: 37 | nums[abs(n) - 1] = -abs(nums[abs(n) - 1]) 38 | for i in range(len(nums)): 39 | if nums[i] > 0: 40 | ans.append(i + 1) 41 | return ans 42 | -------------------------------------------------------------------------------- /easy/7.整数反转/7.整数反转.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=7 lang=javascript 3 | * 4 | * [7] 整数反转 5 | * 6 | * https://leetcode-cn.com/problems/reverse-integer/description/ 7 | * 8 | * algorithms 9 | * Easy (31.41%) 10 | * Total Accepted: 79.4K 11 | * Total Submissions: 252.8K 12 | * Testcase Example: '123' 13 | * 14 | * 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 123 19 | * 输出: 321 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: -123 25 | * 输出: -321 26 | * 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 120 31 | * 输出: 21 32 | * 33 | * 34 | * 注意: 35 | * 36 | * 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。 37 | * 38 | */ 39 | /** 40 | * @param {number} x 41 | * @return {number} 42 | */ 43 | var reverse = function(x) { 44 | let res = 0 45 | while (x !== 0) { 46 | const n = x % 10 47 | res = res * 10 + n 48 | if (res < -(2 ** 31) || res > 2 ** 31 - 1) return 0 49 | x = (x - n) / 10 50 | } 51 | return res 52 | } 53 | -------------------------------------------------------------------------------- /easy/202.快乐数/202.快乐数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=202 lang=javascript 3 | * 4 | * [202] 快乐数 5 | * 6 | * https://leetcode-cn.com/problems/happy-number/description/ 7 | * 8 | * algorithms 9 | * Easy (51.51%) 10 | * Total Accepted: 11.4K 11 | * Total Submissions: 22K 12 | * Testcase Example: '19' 13 | * 14 | * 编写一个算法来判断一个数是不是“快乐数”。 15 | * 16 | * 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 17 | * 1。如果可以变为 1,那么这个数就是快乐数。 18 | * 19 | * 示例: 20 | * 21 | * 输入: 19 22 | * 输出: true 23 | * 解释: 24 | * 12 + 92 = 82 25 | * 82 + 22 = 68 26 | * 62 + 82 = 100 27 | * 12 + 02 + 02 = 1 28 | * 29 | * 30 | */ 31 | /** 32 | * @param {number} n 33 | * @return {boolean} 34 | */ 35 | var isHappy = function(n) { 36 | const map = {} 37 | while (true) { 38 | if (n === 1) return true 39 | if (map[n]) return false 40 | map[n] = 1 41 | let cur = 0 42 | while (n) { 43 | cur += (n % 10) ** 2 44 | n = (n / 10) | 0 45 | } 46 | n = cur 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /easy/206.反转链表/206.反转链表.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=206 lang=python3 3 | # 4 | # [206] 反转链表 5 | # 6 | # https://leetcode-cn.com/problems/reverse-linked-list/description/ 7 | # 8 | # algorithms 9 | # Easy (57.57%) 10 | # Total Accepted: 35.2K 11 | # Total Submissions: 60.6K 12 | # Testcase Example: '[1,2,3,4,5]' 13 | # 14 | # 反转一个单链表。 15 | # 16 | # 示例: 17 | # 18 | # 输入: 1->2->3->4->5->NULL 19 | # 输出: 5->4->3->2->1->NULL 20 | # 21 | # 进阶: 22 | # 你可以迭代或递归地反转链表。你能否用两种方法解决这道题? 23 | # 24 | # 25 | # Definition for singly-linked list. 26 | 27 | 28 | class ListNode: 29 | def __init__(self, x): 30 | self.val = x 31 | self.next = None 32 | 33 | 34 | class Solution: 35 | def reverseList(self, head: ListNode) -> ListNode: 36 | node = head 37 | temp = None 38 | while head: 39 | head = head.next 40 | node.next = temp 41 | temp = node 42 | if not head: 43 | return node 44 | node = head 45 | return None 46 | -------------------------------------------------------------------------------- /easy/476.数字的补数/476.数字的补数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=476 lang=javascript 3 | * 4 | * [476] 数字的补数 5 | * 6 | * https://leetcode-cn.com/problems/number-complement/description/ 7 | * 8 | * algorithms 9 | * Easy (67.07%) 10 | * Total Accepted: 7.5K 11 | * Total Submissions: 11.2K 12 | * Testcase Example: '5' 13 | * 14 | * 给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。 15 | * 16 | * 注意: 17 | * 18 | * 19 | * 给定的整数保证在32位带符号整数的范围内。 20 | * 你可以假定二进制数不包含前导零位。 21 | * 22 | * 23 | * 示例 1: 24 | * 25 | * 26 | * 输入: 5 27 | * 输出: 2 28 | * 解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2。 29 | * 30 | * 31 | * 示例 2: 32 | * 33 | * 34 | * 输入: 1 35 | * 输出: 0 36 | * 解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0。 37 | * 38 | * 39 | */ 40 | /** 41 | * @param {number} num 42 | * @return {number} 43 | */ 44 | var findComplement = function(num) { 45 | let ans = 0 46 | let temp = 0 47 | while (num) { 48 | const n = (num & 1) ^ 1 49 | num >>= 1 50 | ans = (n << temp) + ans 51 | temp++ 52 | } 53 | return ans 54 | } 55 | -------------------------------------------------------------------------------- /easy/234.回文链表/234.回文链表.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=234 lang=javascript 3 | * 4 | * [234] 回文链表 5 | * 6 | * https://leetcode-cn.com/problems/palindrome-linked-list/description/ 7 | * 8 | * algorithms 9 | * Easy (34.84%) 10 | * Total Accepted: 15.8K 11 | * Total Submissions: 45K 12 | * Testcase Example: '[1,2]' 13 | * 14 | * 请判断一个链表是否为回文链表。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 1->2 19 | * 输出: false 20 | * 21 | * 示例 2: 22 | * 23 | * 输入: 1->2->2->1 24 | * 输出: true 25 | * 26 | * 27 | * 进阶: 28 | * 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题? 29 | * 30 | */ 31 | /** 32 | * Definition for singly-linked list. 33 | * function ListNode(val) { 34 | * this.val = val; 35 | * this.next = null; 36 | * } 37 | */ 38 | /** 39 | * @param {ListNode} head 40 | * @return {boolean} 41 | */ 42 | var isPalindrome = function(head) { 43 | let [s1, s2] = ['', ''] 44 | while (head) { 45 | s1 = `${s1}${head.val}` 46 | s2 = `${head.val}${s2}` 47 | head = head.next 48 | } 49 | return s1 === s2 50 | } 51 | -------------------------------------------------------------------------------- /easy/69.x-的平方根/69.x-的平方根.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=69 lang=javascript 3 | * 4 | * [69] x 的平方根 5 | * 6 | * https://leetcode-cn.com/problems/sqrtx/description/ 7 | * 8 | * algorithms 9 | * Easy (34.28%) 10 | * Total Accepted: 22.7K 11 | * Total Submissions: 65.9K 12 | * Testcase Example: '4' 13 | * 14 | * 实现 int sqrt(int x) 函数。 15 | * 16 | * 计算并返回 x 的平方根,其中 x 是非负整数。 17 | * 18 | * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: 4 23 | * 输出: 2 24 | * 25 | * 26 | * 示例 2: 27 | * 28 | * 输入: 8 29 | * 输出: 2 30 | * 说明: 8 的平方根是 2.82842..., 31 | * 由于返回类型是整数,小数部分将被舍去。 32 | * 33 | * 34 | */ 35 | /** 36 | * @param {number} x 37 | * @return {number} 38 | */ 39 | var mySqrt = function(x) { 40 | let l = 0 41 | let r = x 42 | while (l <= r) { 43 | const mid = ((r + l) / 2) | 0 44 | if (mid ** 2 <= x && (mid + 1) ** 2 > x) { 45 | return mid 46 | } else if (mid ** 2 < x) { 47 | l = mid + 1 48 | } else { 49 | r = mid - 1 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /medium/152.乘积最大子序列/152.乘积最大子序列.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=152 lang=python3 3 | # 4 | # [152] 乘积最大子序列 5 | # 6 | # https://leetcode-cn.com/problems/maximum-product-subarray/description/ 7 | # 8 | # algorithms 9 | # Medium (34.18%) 10 | # Likes: 200 11 | # Dislikes: 0 12 | # Total Accepted: 11.5K 13 | # Total Submissions: 33.4K 14 | # Testcase Example: '[2,3,-2,4]' 15 | # 16 | # 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: [2,3,-2,4] 21 | # 输出: 6 22 | # 解释: 子数组 [2,3] 有最大乘积 6。 23 | # 24 | # 25 | # 示例 2: 26 | # 27 | # 输入: [-2,0,-1] 28 | # 输出: 0 29 | # 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。 30 | # 31 | # 32 | 33 | 34 | class Solution: 35 | def maxProduct(self, nums: List[int]) -> int: 36 | mi = ma = res = nums[0] 37 | for i in range(1, len(nums)): 38 | if nums[i] < 0: 39 | mi, ma = ma, mi 40 | ma = max(nums[i], ma * nums[i]) 41 | mi = min(nums[i], mi * nums[i]) 42 | res = max(res, ma) 43 | return res 44 | -------------------------------------------------------------------------------- /medium/49.字母异位词分组/49.字母异位词分组.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=49 lang=python 3 | # 4 | # [49] 字母异位词分组 5 | # 6 | # https://leetcode-cn.com/problems/group-anagrams/description/ 7 | # 8 | # algorithms 9 | # Medium (54.79%) 10 | # Total Accepted: 13.6K 11 | # Total Submissions: 24.7K 12 | # Testcase Example: '["eat","tea","tan","ate","nat","bat"]' 13 | # 14 | # 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 15 | # 16 | # 示例: 17 | # 18 | # 输入: ["eat", "tea", "tan", "ate", "nat", "bat"], 19 | # 输出: 20 | # [ 21 | # ⁠ ["ate","eat","tea"], 22 | # ⁠ ["nat","tan"], 23 | # ⁠ ["bat"] 24 | # ] 25 | # 26 | # 说明: 27 | # 28 | # 29 | # 所有输入均为小写字母。 30 | # 不考虑答案输出的顺序。 31 | # 32 | # 33 | # 34 | import collections 35 | 36 | 37 | class Solution: 38 | def groupAnagrams(self, strs): 39 | ans = collections.defaultdict(list) 40 | for s in strs: 41 | count = [0] * 26 42 | for c in s: 43 | count[ord(c) - ord('a')] += 1 44 | ans[tuple(count)].append(s) 45 | return ans.values() 46 | -------------------------------------------------------------------------------- /easy/202.快乐数/202.快乐数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=202 lang=python3 3 | # 4 | # [202] 快乐数 5 | # 6 | # https://leetcode-cn.com/problems/happy-number/description/ 7 | # 8 | # algorithms 9 | # Easy (51.51%) 10 | # Total Accepted: 11.4K 11 | # Total Submissions: 22K 12 | # Testcase Example: '19' 13 | # 14 | # 编写一个算法来判断一个数是不是“快乐数”。 15 | # 16 | # 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 17 | # 1。如果可以变为 1,那么这个数就是快乐数。 18 | # 19 | # 示例:  20 | # 21 | # 输入: 19 22 | # 输出: true 23 | # 解释: 24 | # 12 + 92 = 82 25 | # 82 + 22 = 68 26 | # 62 + 82 = 100 27 | # 12 + 02 + 02 = 1 28 | # 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def isHappy(self, n: int) -> bool: 35 | m = {} 36 | while True: 37 | if n in m: 38 | return False 39 | if n == 1: 40 | return True 41 | m[n] = 1 42 | cur = 0 43 | while n != 0: 44 | cur += (n % 10) ** 2 45 | n //= 10 46 | n = cur 47 | -------------------------------------------------------------------------------- /easy/66.加一/66.加一.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=66 lang=golang 3 | * 4 | * [66] 加一 5 | * 6 | * https://leetcode-cn.com/problems/plus-one/description/ 7 | * 8 | * algorithms 9 | * Easy (37.33%) 10 | * Total Accepted: 35.9K 11 | * Total Submissions: 96.2K 12 | * Testcase Example: '[1,2,3]' 13 | * 14 | * 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 15 | * 16 | * 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 17 | * 18 | * 你可以假设除了整数 0 之外,这个整数不会以零开头。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [1,2,3] 23 | * 输出: [1,2,4] 24 | * 解释: 输入数组表示数字 123。 25 | * 26 | * 27 | * 示例 2: 28 | * 29 | * 输入: [4,3,2,1] 30 | * 输出: [4,3,2,2] 31 | * 解释: 输入数组表示数字 4321。 32 | * 33 | * 34 | */ 35 | func plusOne(digits []int) []int { 36 | flag := true 37 | for n := len(digits) - 1; n >= 0; n-- { 38 | if flag { 39 | if digits[n]+1 < 10 { 40 | flag = false 41 | } 42 | digits[n] = (digits[n] + 1) % 10 43 | } else { 44 | return digits 45 | } 46 | } 47 | if flag { 48 | digits = append([]int{1}, digits...) 49 | } 50 | return digits 51 | } 52 | -------------------------------------------------------------------------------- /easy/83.删除排序链表中的重复元素/83.删除排序链表中的重复元素.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=83 lang=python3 3 | # 4 | # [83] 删除排序链表中的重复元素 5 | # 6 | # https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/ 7 | # 8 | # algorithms 9 | # Easy (43.50%) 10 | # Total Accepted: 16.1K 11 | # Total Submissions: 37.1K 12 | # Testcase Example: '[1,1,2]' 13 | # 14 | # 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 1->1->2 19 | # 输出: 1->2 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 1->1->2->3->3 25 | # 输出: 1->2->3 26 | # 27 | # 28 | # Definition for singly-linked list. 29 | # class ListNode: 30 | # def __init__(self, x): 31 | # self.val = x 32 | # self.next = None 33 | 34 | class Solution: 35 | def deleteDuplicates(self, head: 'ListNode') -> 'ListNode': 36 | node = head 37 | while head and head.next: 38 | if head.val == head.next.val: 39 | head.next = head.next.next 40 | else: 41 | head = head.next 42 | return node 43 | -------------------------------------------------------------------------------- /medium/187.重复的dna序列.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=187 lang=python3 3 | # 4 | # [187] 重复的DNA序列 5 | # 6 | # https://leetcode-cn.com/problems/repeated-dna-sequences/description/ 7 | # 8 | # algorithms 9 | # Medium (42.75%) 10 | # Likes: 30 11 | # Dislikes: 0 12 | # Total Accepted: 3.7K 13 | # Total Submissions: 8.7K 14 | # Testcase Example: '"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"' 15 | # 16 | # 所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究 DNA 时,识别 DNA 17 | # 中的重复序列有时会对研究非常有帮助。 18 | # 19 | # 编写一个函数来查找 DNA 分子中所有出现超多一次的10个字母长的序列(子串)。 20 | # 21 | # 示例: 22 | # 23 | # 输入: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 24 | # 25 | # 输出: ["AAAAACCCCC", "CCCCCAAAAA"] 26 | # 27 | # 28 | 29 | 30 | class Solution: 31 | def findRepeatedDnaSequences(self, s: str) -> List[str]: 32 | m = {} 33 | res = set() 34 | for i in range(len(s) - 9): 35 | str = s[i:i+10] 36 | if str in m: 37 | res.add(str) 38 | else: 39 | m[str] = 1 40 | return list(res) 41 | -------------------------------------------------------------------------------- /medium/22.括号生成/22.括号生成.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=22 lang=javascript 3 | * 4 | * [22] 括号生成 5 | * 6 | * https://leetcode-cn.com/problems/generate-parentheses/description/ 7 | * 8 | * algorithms 9 | * Medium (68.93%) 10 | * Total Accepted: 16.8K 11 | * Total Submissions: 24.3K 12 | * Testcase Example: '3' 13 | * 14 | * 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 15 | * 16 | * 例如,给出 n = 3,生成结果为: 17 | * 18 | * [ 19 | * ⁠ "((()))", 20 | * ⁠ "(()())", 21 | * ⁠ "(())()", 22 | * ⁠ "()(())", 23 | * ⁠ "()()()" 24 | * ] 25 | * 26 | * 27 | */ 28 | /** 29 | * @param {number} n 30 | * @return {string[]} 31 | */ 32 | var generateParenthesis = function(n) { 33 | const ans = [] 34 | const backtrack = (s = '', l = 0, r = 0) => { 35 | if (s.length === 2 * n) { 36 | ans.push(s) 37 | return 38 | } 39 | if (l < n) { 40 | backtrack(s + '(', l + 1, r) 41 | } 42 | if (r < l) { 43 | backtrack(s + ')', l, r + 1) 44 | } 45 | } 46 | backtrack() 47 | return ans 48 | } 49 | -------------------------------------------------------------------------------- /easy/242.有效的字母异位词/242.有效的字母异位词.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=242 lang=golang 3 | * 4 | * [242] 有效的字母异位词 5 | * 6 | * https://leetcode-cn.com/problems/valid-anagram/description/ 7 | * 8 | * algorithms 9 | * Easy (50.32%) 10 | * Total Accepted: 20.4K 11 | * Total Submissions: 40.5K 12 | * Testcase Example: '"anagram"\n"nagaram"' 13 | * 14 | * 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: s = "anagram", t = "nagaram" 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: s = "rat", t = "car" 25 | * 输出: false 26 | * 27 | * 说明: 28 | * 你可以假设字符串只包含小写字母。 29 | * 30 | * 进阶: 31 | * 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? 32 | * 33 | */ 34 | func isAnagram(s string, t string) bool { 35 | x, y := make([]int, 26), make([]int, 26) 36 | for _, s1 := range s { 37 | x[s1-'a']++ 38 | } 39 | for _, t1 := range t { 40 | y[t1-'a']++ 41 | } 42 | for i := 0; i < 26; i++ { 43 | if x[i] != y[i] { 44 | return false 45 | } 46 | } 47 | return true 48 | } 49 | 50 | -------------------------------------------------------------------------------- /easy/349.两个数组的交集/349.两个数组的交集.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=349 lang=python3 3 | # 4 | # [349] 两个数组的交集 5 | # 6 | # https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ 7 | # 8 | # algorithms 9 | # Easy (59.17%) 10 | # Total Accepted: 13.6K 11 | # Total Submissions: 22.8K 12 | # Testcase Example: '[1,2,2,1]\n[2,2]' 13 | # 14 | # 给定两个数组,编写一个函数来计算它们的交集。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: nums1 = [1,2,2,1], nums2 = [2,2] 19 | # 输出: [2] 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 25 | # 输出: [9,4] 26 | # 27 | # 说明: 28 | # 29 | # 30 | # 输出结果中的每个元素一定是唯一的。 31 | # 我们可以不考虑输出结果的顺序。 32 | # 33 | # 34 | # 35 | 36 | 37 | class Solution: 38 | def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: 39 | m = {} 40 | for n in nums1: 41 | if not n in m: 42 | m[n] = 1 43 | res = [] 44 | for n in nums2: 45 | if n in m: 46 | res.append(n) 47 | del m[n] 48 | return res 49 | -------------------------------------------------------------------------------- /easy/53.最大子序和/53.最大子序和.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=53 lang=javascript 3 | * 4 | * [53] 最大子序和 5 | * 6 | * https://leetcode-cn.com/problems/maximum-subarray/description/ 7 | * 8 | * algorithms 9 | * Easy (42.02%) 10 | * Total Accepted: 35.4K 11 | * Total Submissions: 84.1K 12 | * Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]' 13 | * 14 | * 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 15 | * 16 | * 示例: 17 | * 18 | * 输入: [-2,1,-3,4,-1,2,1,-5,4], 19 | * 输出: 6 20 | * 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 21 | * 22 | * 23 | * 进阶: 24 | * 25 | * 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 26 | * 27 | */ 28 | /** 29 | * @param {number[]} nums 30 | * @return {number} 31 | */ 32 | var maxSubArray = function(nums) { 33 | const result = nums.slice() 34 | let maxSum = nums[0] 35 | for (let i = 1; i < nums.length; i++) { 36 | if (result[i - 1] > 0) { 37 | result[i] = nums[i] + result[i - 1] 38 | } 39 | if (result[i] > maxSum) { 40 | maxSum = result[i] 41 | } 42 | } 43 | return maxSum 44 | } 45 | -------------------------------------------------------------------------------- /easy/70.爬楼梯/70.爬楼梯.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=70 lang=python3 3 | # 4 | # [70] 爬楼梯 5 | # 6 | # https://leetcode-cn.com/problems/climbing-stairs/description/ 7 | # 8 | # algorithms 9 | # Easy (43.84%) 10 | # Total Accepted: 29.6K 11 | # Total Submissions: 67.4K 12 | # Testcase Example: '2' 13 | # 14 | # 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 15 | # 16 | # 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 17 | # 18 | # 注意:给定 n 是一个正整数。 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: 2 23 | # 输出: 2 24 | # 解释: 有两种方法可以爬到楼顶。 25 | # 1. 1 阶 + 1 阶 26 | # 2. 2 阶 27 | # 28 | # 示例 2: 29 | # 30 | # 输入: 3 31 | # 输出: 3 32 | # 解释: 有三种方法可以爬到楼顶。 33 | # 1. 1 阶 + 1 阶 + 1 阶 34 | # 2. 1 阶 + 2 阶 35 | # 3. 2 阶 + 1 阶 36 | # 37 | # 38 | # 39 | class Solution: 40 | cache = {} 41 | def climbStairs(self, n: 'int') -> 'int': 42 | if n in self.cache: 43 | return self.cache[n] 44 | else: 45 | if n < 2: 46 | return 1 47 | self.cache[n] = self.climbStairs(n - 1) + self.climbStairs(n - 2) 48 | return self.cache[n] 49 | -------------------------------------------------------------------------------- /medium/55.跳跃游戏/55.跳跃游戏.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=55 lang=python3 3 | # 4 | # [55] 跳跃游戏 5 | # 6 | # https://leetcode-cn.com/problems/jump-game/description/ 7 | # 8 | # algorithms 9 | # Medium (34.12%) 10 | # Total Accepted: 14.4K 11 | # Total Submissions: 41.7K 12 | # Testcase Example: '[2,3,1,1,4]' 13 | # 14 | # 给定一个非负整数数组,你最初位于数组的第一个位置。 15 | # 16 | # 数组中的每个元素代表你在该位置可以跳跃的最大长度。 17 | # 18 | # 判断你是否能够到达最后一个位置。 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: [2,3,1,1,4] 23 | # 输出: true 24 | # 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 25 | # 26 | # 27 | # 示例 2: 28 | # 29 | # 输入: [3,2,1,0,4] 30 | # 输出: false 31 | # 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 32 | # 33 | # 34 | # 35 | 36 | 37 | class Solution: 38 | def canJump(self, nums: List[int]) -> bool: 39 | n = 1 40 | for i in range(len(nums)-2, -1, -1): 41 | if nums[i] >= n: 42 | n = 1 43 | else: 44 | n += 1 45 | if i == 0 and n > 1: 46 | return False 47 | return True 48 | -------------------------------------------------------------------------------- /easy/104.二叉树的最大深度/104.二叉树的最大深度.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=104 lang=golang 3 | * 4 | * [104] 二叉树的最大深度 5 | * 6 | * https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/ 7 | * 8 | * algorithms 9 | * Easy (66.76%) 10 | * Total Accepted: 29.3K 11 | * Total Submissions: 43.8K 12 | * Testcase Example: '[3,9,20,null,null,15,7]' 13 | * 14 | * 给定一个二叉树,找出其最大深度。 15 | * 16 | * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 17 | * 18 | * 说明: 叶子节点是指没有子节点的节点。 19 | * 20 | * 示例: 21 | * 给定二叉树 [3,9,20,null,null,15,7], 22 | * 23 | * ⁠ 3 24 | * ⁠ / \ 25 | * ⁠ 9 20 26 | * ⁠ / \ 27 | * ⁠ 15 7 28 | * 29 | * 返回它的最大深度 3 。 30 | * 31 | */ 32 | /** 33 | * Definition for a binary tree node. 34 | * type TreeNode struct { 35 | * Val int 36 | * Left *TreeNode 37 | * Right *TreeNode 38 | * } 39 | */ 40 | import "math" 41 | 42 | func maxDepth(root *TreeNode) int { 43 | if root == nil { 44 | return 0 45 | } 46 | return 1 + int(math.Max(float64(maxDepth(root.Left)), float64(maxDepth(root.Right)))) 47 | } 48 | -------------------------------------------------------------------------------- /easy/203.移除链表元素/203.移除链表元素.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=203 lang=python3 3 | # 4 | # [203] 移除链表元素 5 | # 6 | # https://leetcode-cn.com/problems/remove-linked-list-elements/description/ 7 | # 8 | # algorithms 9 | # Easy (38.84%) 10 | # Total Accepted: 16.2K 11 | # Total Submissions: 41.5K 12 | # Testcase Example: '[1,2,6,3,4,5,6]\n6' 13 | # 14 | # 删除链表中等于给定值 val 的所有节点。 15 | # 16 | # 示例: 17 | # 18 | # 输入: 1->2->6->3->4->5->6, val = 6 19 | # 输出: 1->2->3->4->5 20 | # 21 | # 22 | # 23 | # Definition for singly-linked list. 24 | # class ListNode: 25 | # def __init__(self, x): 26 | # self.val = x 27 | # self.next = None 28 | 29 | 30 | class Solution: 31 | def removeElements(self, head: ListNode, val: int) -> ListNode: 32 | header = ListNode(None) 33 | header.next = head 34 | node = header 35 | while node.next: 36 | if node.next.val == val: 37 | node.next = node.next.next 38 | else: 39 | node = node.next 40 | return header.next 41 | -------------------------------------------------------------------------------- /easy/263.丑数/263.丑数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=263 lang=golang 3 | * 4 | * [263] 丑数 5 | * 6 | * https://leetcode-cn.com/problems/ugly-number/description/ 7 | * 8 | * algorithms 9 | * Easy (44.55%) 10 | * Total Accepted: 6.3K 11 | * Total Submissions: 14.2K 12 | * Testcase Example: '6' 13 | * 14 | * 编写一个程序判断给定的数是否为丑数。 15 | * 16 | * 丑数就是只包含质因数 2, 3, 5 的正整数。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: 6 21 | * 输出: true 22 | * 解释: 6 = 2 × 3 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: 8 27 | * 输出: true 28 | * 解释: 8 = 2 × 2 × 2 29 | * 30 | * 31 | * 示例 3: 32 | * 33 | * 输入: 14 34 | * 输出: false 35 | * 解释: 14 不是丑数,因为它包含了另外一个质因数 7。 36 | * 37 | * 说明: 38 | * 39 | * 40 | * 1 是丑数。 41 | * 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。 42 | * 43 | * 44 | */ 45 | func isUgly(num int) bool { 46 | if num == 0 { 47 | return false 48 | } 49 | for num & 1 == 0 { 50 | num >>= 1 51 | } 52 | for num % 3 == 0 { 53 | num /= 3 54 | } 55 | for num % 5 == 0 { 56 | num /= 5 57 | } 58 | return num == 1 59 | } 60 | 61 | -------------------------------------------------------------------------------- /easy/387.字符串中的第一个唯一字符/387.字符串中的第一个唯一字符.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=387 lang=javascript 3 | * 4 | * [387] 字符串中的第一个唯一字符 5 | * 6 | * https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/ 7 | * 8 | * algorithms 9 | * Easy (35.49%) 10 | * Total Accepted: 21.3K 11 | * Total Submissions: 59.4K 12 | * Testcase Example: '"leetcode"' 13 | * 14 | * 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 15 | * 16 | * 案例: 17 | * 18 | * 19 | * s = "leetcode" 20 | * 返回 0. 21 | * 22 | * s = "loveleetcode", 23 | * 返回 2. 24 | * 25 | * 26 | * 27 | * 28 | * 注意事项:您可以假定该字符串只包含小写字母。 29 | * 30 | */ 31 | /** 32 | * @param {string} s 33 | * @return {number} 34 | */ 35 | var firstUniqChar = function(s) { 36 | const arr = Array(26).fill(0) 37 | for (let i = 0; i < s.length; i++) { 38 | arr[s[i].charCodeAt() - 'a'.charCodeAt()]++ 39 | } 40 | for (let i = 0; i < s.length; i++) { 41 | if (arr[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) { 42 | return i 43 | } 44 | } 45 | return -1 46 | } 47 | -------------------------------------------------------------------------------- /easy/349.两个数组的交集/349.两个数组的交集.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=349 lang=golang 3 | * 4 | * [349] 两个数组的交集 5 | * 6 | * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ 7 | * 8 | * algorithms 9 | * Easy (59.17%) 10 | * Total Accepted: 13.6K 11 | * Total Submissions: 22.8K 12 | * Testcase Example: '[1,2,2,1]\n[2,2]' 13 | * 14 | * 给定两个数组,编写一个函数来计算它们的交集。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: nums1 = [1,2,2,1], nums2 = [2,2] 19 | * 输出: [2] 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 25 | * 输出: [9,4] 26 | * 27 | * 说明: 28 | * 29 | * 30 | * 输出结果中的每个元素一定是唯一的。 31 | * 我们可以不考虑输出结果的顺序。 32 | * 33 | * 34 | */ 35 | func intersection(nums1 []int, nums2 []int) []int { 36 | m := map[int]int{} 37 | for _, n := range nums1 { 38 | if _, ok := m[n]; ok == false { 39 | m[n] = 1 40 | } 41 | } 42 | res := []int{} 43 | for _, n := range nums2 { 44 | if _, ok := m[n]; ok { 45 | res = append(res, n) 46 | delete(m, n) 47 | } 48 | } 49 | return res 50 | } 51 | 52 | -------------------------------------------------------------------------------- /easy/404.左叶子之和/404.左叶子之和.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=404 lang=golang 3 | * 4 | * [404] 左叶子之和 5 | * 6 | * https://leetcode-cn.com/problems/sum-of-left-leaves/description/ 7 | * 8 | * algorithms 9 | * Easy (48.25%) 10 | * Total Accepted: 4.5K 11 | * Total Submissions: 9.4K 12 | * Testcase Example: '[3,9,20,null,null,15,7]' 13 | * 14 | * 计算给定二叉树的所有左叶子之和。 15 | * 16 | * 示例: 17 | * 18 | * 19 | * ⁠ 3 20 | * ⁠ / \ 21 | * ⁠ 9 20 22 | * ⁠ / \ 23 | * ⁠ 15 7 24 | * 25 | * 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 26 | * 27 | * 28 | * 29 | */ 30 | /** 31 | * Definition for a binary tree node. 32 | * type TreeNode struct { 33 | * Val int 34 | * Left *TreeNode 35 | * Right *TreeNode 36 | * } 37 | */ 38 | func sumOfLeftLeaves(root *TreeNode) int { 39 | ans := 0 40 | if root == nil { 41 | return ans 42 | } 43 | if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { 44 | ans += root.Left.Val 45 | } 46 | return ans + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) 47 | } 48 | 49 | -------------------------------------------------------------------------------- /medium/50.pow-x-n/50.pow-x-n.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=50 lang=golang 3 | * 4 | * [50] Pow(x, n) 5 | * 6 | * https://leetcode-cn.com/problems/powx-n/description/ 7 | * 8 | * algorithms 9 | * Medium (31.99%) 10 | * Total Accepted: 15.9K 11 | * Total Submissions: 49.4K 12 | * Testcase Example: '2.00000\n10' 13 | * 14 | * 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 2.00000, 10 19 | * 输出: 1024.00000 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 2.10000, 3 25 | * 输出: 9.26100 26 | * 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 2.00000, -2 31 | * 输出: 0.25000 32 | * 解释: 2^-2 = 1/2^2 = 1/4 = 0.25 33 | * 34 | * 说明: 35 | * 36 | * 37 | * -100.0 < x < 100.0 38 | * n 是 32 位有符号整数,其数值范围是 [−2^31, 2^31 − 1] 。 39 | * 40 | * 41 | */ 42 | func myPow(x float64, n int) float64 { 43 | if n == 0 { 44 | return 1 45 | } 46 | if n < 0 { 47 | return 1 / myPow(x, -n) 48 | } 49 | if n & 1 == 1 { 50 | return x * myPow(x, n - 1) 51 | } 52 | return myPow(x * x, n / 2) 53 | } 54 | 55 | -------------------------------------------------------------------------------- /easy/344.反转字符串/344.反转字符串.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=344 lang=javascript 3 | * 4 | * [344] 反转字符串 5 | * 6 | * https://leetcode-cn.com/problems/reverse-string/description/ 7 | * 8 | * algorithms 9 | * Easy (65.08%) 10 | * Total Accepted: 35.9K 11 | * Total Submissions: 55.2K 12 | * Testcase Example: '["h","e","l","l","o"]' 13 | * 14 | * 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 15 | * 16 | * 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 17 | * 18 | * 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 19 | * 20 | * 21 | * 22 | * 示例 1: 23 | * 24 | * 输入:["h","e","l","l","o"] 25 | * 输出:["o","l","l","e","h"] 26 | * 27 | * 28 | * 示例 2: 29 | * 30 | * 输入:["H","a","n","n","a","h"] 31 | * 输出:["h","a","n","n","a","H"] 32 | * 33 | */ 34 | /** 35 | * @param {character[]} s 36 | * @return {void} Do not return anything, modify s in-place instead. 37 | */ 38 | var reverseString = function(s) { 39 | let l = 0 40 | let r = s.length - 1 41 | while (l <= r) { 42 | ;[s[l], s[r]] = [s[r], s[l]] 43 | l++ 44 | r-- 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /easy/349.两个数组的交集/349.两个数组的交集.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=349 lang=javascript 3 | * 4 | * [349] 两个数组的交集 5 | * 6 | * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ 7 | * 8 | * algorithms 9 | * Easy (59.17%) 10 | * Total Accepted: 13.6K 11 | * Total Submissions: 22.8K 12 | * Testcase Example: '[1,2,2,1]\n[2,2]' 13 | * 14 | * 给定两个数组,编写一个函数来计算它们的交集。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: nums1 = [1,2,2,1], nums2 = [2,2] 19 | * 输出: [2] 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 25 | * 输出: [9,4] 26 | * 27 | * 说明: 28 | * 29 | * 30 | * 输出结果中的每个元素一定是唯一的。 31 | * 我们可以不考虑输出结果的顺序。 32 | * 33 | * 34 | */ 35 | /** 36 | * @param {number[]} nums1 37 | * @param {number[]} nums2 38 | * @return {number[]} 39 | */ 40 | var intersection = function(nums1, nums2) { 41 | const set = new Set([...nums1]) 42 | const res = [] 43 | for (let n of nums2) { 44 | if (set.has(n)) { 45 | res.push(n) 46 | set.delete(n) 47 | } 48 | } 49 | return res 50 | } 51 | -------------------------------------------------------------------------------- /medium/46.全排列/46.全排列.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=46 lang=javascript 3 | * 4 | * [46] 全排列 5 | * 6 | * https://leetcode-cn.com/problems/permutations/description/ 7 | * 8 | * algorithms 9 | * Medium (67.08%) 10 | * Total Accepted: 18.7K 11 | * Total Submissions: 27.7K 12 | * Testcase Example: '[1,2,3]' 13 | * 14 | * 给定一个没有重复数字的序列,返回其所有可能的全排列。 15 | * 16 | * 示例: 17 | * 18 | * 输入: [1,2,3] 19 | * 输出: 20 | * [ 21 | * ⁠ [1,2,3], 22 | * ⁠ [1,3,2], 23 | * ⁠ [2,1,3], 24 | * ⁠ [2,3,1], 25 | * ⁠ [3,1,2], 26 | * ⁠ [3,2,1] 27 | * ] 28 | * 29 | */ 30 | /** 31 | * @param {number[]} nums 32 | * @return {number[][]} 33 | */ 34 | var permute = function(nums, start = 0, end = nums.length, result = []) { 35 | if (start === end) { 36 | result.push(nums) 37 | } else { 38 | for (let i = start; i < end; i++) { 39 | ;[nums[i], nums[start]] = [nums[start], nums[i]] 40 | permute([...nums], start + 1, end, result) 41 | ;[nums[i], nums[start]] = [nums[start], nums[i]] 42 | } 43 | } 44 | return result 45 | } 46 | -------------------------------------------------------------------------------- /medium/50.pow-x-n/50.pow-x-n.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=50 lang=python3 3 | # 4 | # [50] Pow(x, n) 5 | # 6 | # https://leetcode-cn.com/problems/powx-n/description/ 7 | # 8 | # algorithms 9 | # Medium (31.79%) 10 | # Total Accepted: 14.8K 11 | # Total Submissions: 46.1K 12 | # Testcase Example: '2.00000\n10' 13 | # 14 | # 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 2.00000, 10 19 | # 输出: 1024.00000 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: 2.10000, 3 25 | # 输出: 9.26100 26 | # 27 | # 28 | # 示例 3: 29 | # 30 | # 输入: 2.00000, -2 31 | # 输出: 0.25000 32 | # 解释: 2^-2 = 1/2^2 = 1/4 = 0.25 33 | # 34 | # 说明: 35 | # 36 | # 37 | # -100.0 < x < 100.0 38 | # n 是 32 位有符号整数,其数值范围是 [−2^31, 2^31 − 1] 。 39 | # 40 | # 41 | # 42 | 43 | 44 | class Solution: 45 | def myPow(self, x: float, n: int) -> float: 46 | if n == 0: 47 | return 1 48 | if n < 0: 49 | return 1 / self.myPow(x, -n) 50 | if n & 1: 51 | return x * self.myPow(x, n - 1) 52 | return self.myPow(x * x, n // 2) 53 | -------------------------------------------------------------------------------- /easy/167.两数之和-ii-输入有序数组/167.两数之和-ii-输入有序数组.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=167 lang=golang 3 | * 4 | * [167] 两数之和 II - 输入有序数组 5 | * 6 | * https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/description/ 7 | * 8 | * algorithms 9 | * Easy (46.83%) 10 | * Total Accepted: 15.8K 11 | * Total Submissions: 33.8K 12 | * Testcase Example: '[2,7,11,15]\n9' 13 | * 14 | * 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 15 | * 16 | * 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 17 | * 18 | * 说明: 19 | * 20 | * 21 | * 返回的下标值(index1 和 index2)不是从零开始的。 22 | * 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。 23 | * 24 | * 25 | * 示例: 26 | * 27 | * 输入: numbers = [2, 7, 11, 15], target = 9 28 | * 输出: [1,2] 29 | * 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。 30 | * 31 | */ 32 | func twoSum(numbers []int, target int) []int { 33 | l, r := 0, len(numbers)-1 34 | for numbers[l]+numbers[r] != target { 35 | if numbers[l]+numbers[r] > target { 36 | r-- 37 | } else { 38 | l++ 39 | } 40 | } 41 | return []int{l+1, r+1} 42 | } 43 | 44 | -------------------------------------------------------------------------------- /easy/344.反转字符串/344.反转字符串.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=344 lang=python3 3 | # 4 | # [344] 反转字符串 5 | # 6 | # https://leetcode-cn.com/problems/reverse-string/description/ 7 | # 8 | # algorithms 9 | # Easy (65.08%) 10 | # Total Accepted: 35.9K 11 | # Total Submissions: 55.2K 12 | # Testcase Example: '["h","e","l","l","o"]' 13 | # 14 | # 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 15 | # 16 | # 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 17 | # 18 | # 你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 19 | # 20 | # 21 | # 22 | # 示例 1: 23 | # 24 | # 输入:["h","e","l","l","o"] 25 | # 输出:["o","l","l","e","h"] 26 | # 27 | # 28 | # 示例 2: 29 | # 30 | # 输入:["H","a","n","n","a","h"] 31 | # 输出:["h","a","n","n","a","H"] 32 | # 33 | # 34 | 35 | 36 | class Solution: 37 | def reverseString(self, s: List[str]) -> None: 38 | """ 39 | Do not return anything, modify s in-place instead. 40 | """ 41 | l = 0 42 | r = len(s) - 1 43 | while l <= r: 44 | s[l], s[r] = s[r], s[l] 45 | l += 1 46 | r -= 1 47 | -------------------------------------------------------------------------------- /easy/400.第n个数字/400.第n个数字.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=400 lang=python3 3 | # 4 | # [400] 第N个数字 5 | # 6 | # https://leetcode-cn.com/problems/nth-digit/description/ 7 | # 8 | # algorithms 9 | # Easy (30.10%) 10 | # Total Accepted: 2.1K 11 | # Total Submissions: 7K 12 | # Testcase Example: '3' 13 | # 14 | # 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。 15 | # 16 | # 注意: 17 | # n 是正数且在32为整形范围内 ( n < 231)。 18 | # 19 | # 示例 1: 20 | # 21 | # 22 | # 输入: 23 | # 3 24 | # 25 | # 输出: 26 | # 3 27 | # 28 | # 29 | # 示例 2: 30 | # 31 | # 32 | # 输入: 33 | # 11 34 | # 35 | # 输出: 36 | # 0 37 | # 38 | # 说明: 39 | # 第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。 40 | # 41 | # 42 | # 43 | 44 | 45 | class Solution: 46 | def findNthDigit(self, n: 'int') -> 'int': 47 | digit = 1 48 | while True: 49 | first = 10**(digit-1) 50 | cnt = 9 * first * digit 51 | if cnt >= n: 52 | return int(str(first + (n-1)//digit)[(n-1) % digit]) 53 | n -= cnt 54 | digit += 1 55 | -------------------------------------------------------------------------------- /easy/9.回文数/9.回文数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=9 lang=javascript 3 | * 4 | * [9] 回文数 5 | * 6 | * https://leetcode-cn.com/problems/palindrome-number/description/ 7 | * 8 | * algorithms 9 | * Easy (55.96%) 10 | * Total Accepted: 68.4K 11 | * Total Submissions: 122.2K 12 | * Testcase Example: '121' 13 | * 14 | * 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 121 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: -121 25 | * 输出: false 26 | * 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 27 | * 28 | * 29 | * 示例 3: 30 | * 31 | * 输入: 10 32 | * 输出: false 33 | * 解释: 从右向左读, 为 01 。因此它不是一个回文数。 34 | * 35 | * 36 | * 进阶: 37 | * 38 | * 你能不将整数转为字符串来解决这个问题吗? 39 | * 40 | */ 41 | /** 42 | * @param {number} x 43 | * @return {boolean} 44 | */ 45 | var isPalindrome = function(x) { 46 | if (x < 0) return false 47 | let res = 0 48 | const d = x 49 | while (x !== 0) { 50 | const n = x % 10 51 | res = res * 10 + n 52 | x = (x - n) / 10 53 | } 54 | return res == d 55 | } 56 | -------------------------------------------------------------------------------- /easy/263.丑数/263.丑数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=263 lang=python3 3 | # 4 | # [263] 丑数 5 | # 6 | # https://leetcode-cn.com/problems/ugly-number/description/ 7 | # 8 | # algorithms 9 | # Easy (44.55%) 10 | # Total Accepted: 6.3K 11 | # Total Submissions: 14.2K 12 | # Testcase Example: '6' 13 | # 14 | # 编写一个程序判断给定的数是否为丑数。 15 | # 16 | # 丑数就是只包含质因数 2, 3, 5 的正整数。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: 6 21 | # 输出: true 22 | # 解释: 6 = 2 × 3 23 | # 24 | # 示例 2: 25 | # 26 | # 输入: 8 27 | # 输出: true 28 | # 解释: 8 = 2 × 2 × 2 29 | # 30 | # 31 | # 示例 3: 32 | # 33 | # 输入: 14 34 | # 输出: false 35 | # 解释: 14 不是丑数,因为它包含了另外一个质因数 7。 36 | # 37 | # 说明: 38 | # 39 | # 40 | # 1 是丑数。 41 | # 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。 42 | # 43 | # 44 | # 45 | 46 | 47 | class Solution: 48 | def isUgly(self, num: int) -> bool: 49 | if num == 0: 50 | return False 51 | while num & 1 == 0: 52 | num >>= 1 53 | while num % 3 == 0: 54 | num /= 3 55 | while num % 5 == 0: 56 | num /= 5 57 | return num == 1 58 | -------------------------------------------------------------------------------- /medium/31.下一个排列/31.下一个排列.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=31 lang=golang 3 | * 4 | * [31] 下一个排列 5 | * 6 | * https://leetcode-cn.com/problems/next-permutation/description/ 7 | * 8 | * algorithms 9 | * Medium (30.22%) 10 | * Total Accepted: 9.8K 11 | * Total Submissions: 32.4K 12 | * Testcase Example: '[1,2,3]' 13 | * 14 | * 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。 15 | * 16 | * 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。 17 | * 18 | * 必须原地修改,只允许使用额外常数空间。 19 | * 20 | * 以下是一些例子,输入位于左侧列,其相应输出位于右侧列。 21 | * 1,2,3 → 1,3,2 22 | * 3,2,1 → 1,2,3 23 | * 1,1,5 → 1,5,1 24 | * 25 | */ 26 | func nextPermutation(nums []int) { 27 | i := len(nums) - 2 28 | for i >= 0 && nums[i+1] <= nums[i] { 29 | i-- 30 | } 31 | if i >= 0 { 32 | j := len(nums) - 1 33 | for j >= 0 && nums[j] <= nums[i] { 34 | j-- 35 | } 36 | nums[i], nums[j] = nums[j], nums[i] 37 | } 38 | reverse(nums, i+1, len(nums)-1) 39 | } 40 | 41 | func reverse(arr []int, l int, r int) { 42 | for l <= r { 43 | arr[l], arr[r] = arr[r], arr[l] 44 | r-- 45 | l++ 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /medium/50.pow-x-n/50.pow-x-n.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=50 lang=javascript 3 | * 4 | * [50] Pow(x, n) 5 | * 6 | * https://leetcode-cn.com/problems/powx-n/description/ 7 | * 8 | * algorithms 9 | * Medium (31.79%) 10 | * Total Accepted: 14.8K 11 | * Total Submissions: 46.1K 12 | * Testcase Example: '2.00000\n10' 13 | * 14 | * 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 2.00000, 10 19 | * 输出: 1024.00000 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 2.10000, 3 25 | * 输出: 9.26100 26 | * 27 | * 28 | * 示例 3: 29 | * 30 | * 输入: 2.00000, -2 31 | * 输出: 0.25000 32 | * 解释: 2^-2 = 1/2^2 = 1/4 = 0.25 33 | * 34 | * 说明: 35 | * 36 | * 37 | * -100.0 < x < 100.0 38 | * n 是 32 位有符号整数,其数值范围是 [−2^31, 2^31 − 1] 。 39 | * 40 | * 41 | */ 42 | /** 43 | * @param {number} x 44 | * @param {number} n 45 | * @return {number} 46 | */ 47 | var myPow = function(x, n) { 48 | if (n === 0) return 1 49 | if (n < 0) return 1 / myPow(x, -n) 50 | if (n & 1) return x * myPow(x, n - 1) 51 | return myPow(x * x, n / 2) 52 | } 53 | -------------------------------------------------------------------------------- /medium/55.跳跃游戏/55.跳跃游戏.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=55 lang=javascript 3 | * 4 | * [55] 跳跃游戏 5 | * 6 | * https://leetcode-cn.com/problems/jump-game/description/ 7 | * 8 | * algorithms 9 | * Medium (34.12%) 10 | * Total Accepted: 14.4K 11 | * Total Submissions: 41.7K 12 | * Testcase Example: '[2,3,1,1,4]' 13 | * 14 | * 给定一个非负整数数组,你最初位于数组的第一个位置。 15 | * 16 | * 数组中的每个元素代表你在该位置可以跳跃的最大长度。 17 | * 18 | * 判断你是否能够到达最后一个位置。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [2,3,1,1,4] 23 | * 输出: true 24 | * 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 25 | * 26 | * 27 | * 示例 2: 28 | * 29 | * 输入: [3,2,1,0,4] 30 | * 输出: false 31 | * 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 32 | * 33 | * 34 | */ 35 | /** 36 | * @param {number[]} nums 37 | * @return {boolean} 38 | */ 39 | var canJump = function(nums) { 40 | let n = 1 41 | for (let i = nums.length - 2; i >= 0; i--) { 42 | if (nums[i] >= n) { 43 | n = 1 44 | } else { 45 | n++ 46 | } 47 | if (i === 0 && n > 1) return false 48 | } 49 | return true 50 | } 51 | -------------------------------------------------------------------------------- /easy/242.有效的字母异位词/242.有效的字母异位词.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=242 lang=python3 3 | # 4 | # [242] 有效的字母异位词 5 | # 6 | # https://leetcode-cn.com/problems/valid-anagram/description/ 7 | # 8 | # algorithms 9 | # Easy (49.88%) 10 | # Total Accepted: 20.3K 11 | # Total Submissions: 40.4K 12 | # Testcase Example: '"anagram"\n"nagaram"' 13 | # 14 | # 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: s = "anagram", t = "nagaram" 19 | # 输出: true 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: s = "rat", t = "car" 25 | # 输出: false 26 | # 27 | # 说明: 28 | # 你可以假设字符串只包含小写字母。 29 | # 30 | # 进阶: 31 | # 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? 32 | # 33 | # 34 | 35 | 36 | class Solution: 37 | def isAnagram(self, s: str, t: str) -> bool: 38 | x = [0] * 26 39 | y = [0] * 26 40 | for s1 in s: 41 | x[ord(s1) - ord('a')] += 1 42 | for t1 in t: 43 | y[ord(t1) - ord('a')] += 1 44 | for i in range(26): 45 | if x[i] != y[i]: 46 | return False 47 | return True 48 | -------------------------------------------------------------------------------- /easy/404.左叶子之和/404.左叶子之和.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=404 lang=javascript 3 | * 4 | * [404] 左叶子之和 5 | * 6 | * https://leetcode-cn.com/problems/sum-of-left-leaves/description/ 7 | * 8 | * algorithms 9 | * Easy (48.25%) 10 | * Total Accepted: 4.5K 11 | * Total Submissions: 9.4K 12 | * Testcase Example: '[3,9,20,null,null,15,7]' 13 | * 14 | * 计算给定二叉树的所有左叶子之和。 15 | * 16 | * 示例: 17 | * 18 | * 19 | * ⁠ 3 20 | * ⁠ / \ 21 | * ⁠ 9 20 22 | * ⁠ / \ 23 | * ⁠ 15 7 24 | * 25 | * 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 26 | * 27 | * 28 | * 29 | */ 30 | /** 31 | * Definition for a binary tree node. 32 | * function TreeNode(val) { 33 | * this.val = val; 34 | * this.left = this.right = null; 35 | * } 36 | */ 37 | /** 38 | * @param {TreeNode} root 39 | * @return {number} 40 | */ 41 | var sumOfLeftLeaves = function(root) { 42 | let sum = 0 43 | if (root && root.left && !root.left.left && !root.left.right) { 44 | sum += root.left.val 45 | } 46 | return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right) 47 | } 48 | -------------------------------------------------------------------------------- /easy/1.两数之和/1.两数之和.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=1 lang=rust 3 | * 4 | * [1] 两数之和 5 | * 6 | * https://leetcode-cn.com/problems/two-sum/description/ 7 | * 8 | * algorithms 9 | * Easy (44.37%) 10 | * Total Accepted: 238.5K 11 | * Total Submissions: 537.5K 12 | * Testcase Example: '[2,7,11,15]\n9' 13 | * 14 | * 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 15 | * 16 | * 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 17 | * 18 | * 示例: 19 | * 20 | * 给定 nums = [2, 7, 11, 15], target = 9 21 | * 22 | * 因为 nums[0] + nums[1] = 2 + 7 = 9 23 | * 所以返回 [0, 1] 24 | * 25 | * 26 | */ 27 | use std::collections::HashMap; 28 | impl Solution { 29 | fn two_sum(nums: Vec, target: i32) -> Vec { 30 | let mut map: HashMap = HashMap::new(); 31 | let mut res: Vec = vec![]; 32 | for (i, &n) in nums.iter().enumerate() { 33 | let t = target - n; 34 | if map.get(&t).is_some() { 35 | return vec![*map.get(&t).unwrap(), i as i32]; 36 | } else { 37 | map.insert(n, i as i32); 38 | } 39 | } 40 | vec![] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /easy/118.杨辉三角/118.杨辉三角.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=118 lang=golang 3 | * 4 | * [118] 杨辉三角 5 | * 6 | * https://leetcode-cn.com/problems/pascals-triangle/description/ 7 | * 8 | * algorithms 9 | * Easy (59.45%) 10 | * Total Accepted: 15.9K 11 | * Total Submissions: 26.6K 12 | * Testcase Example: '5' 13 | * 14 | * 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 15 | * 16 | * 17 | * 18 | * 在杨辉三角中,每个数是它左上方和右上方的数的和。 19 | * 20 | * 示例: 21 | * 22 | * 输入: 5 23 | * 输出: 24 | * [ 25 | * ⁠ [1], 26 | * ⁠ [1,1], 27 | * ⁠ [1,2,1], 28 | * ⁠ [1,3,3,1], 29 | * ⁠[1,4,6,4,1] 30 | * ] 31 | * 32 | */ 33 | func generate(numRows int) [][]int { 34 | if numRows == 0 { 35 | return [][]int{} 36 | } 37 | res := [][]int{[]int{1}} 38 | arr := []int{1} 39 | for numRows > 1 { 40 | cur := []int{} 41 | for i := 0; i <= len(arr); i++ { 42 | if i == 0 || i == len(arr) { 43 | cur = append(cur, 1) 44 | } else { 45 | cur = append(cur, arr[i]+arr[i-1]) 46 | } 47 | } 48 | res = append(res, cur) 49 | arr = cur 50 | numRows-- 51 | } 52 | return res 53 | } 54 | -------------------------------------------------------------------------------- /easy/14.最长公共前缀/14.最长公共前缀.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=14 lang=python3 3 | # 4 | # [14] 最长公共前缀 5 | # 6 | # https://leetcode-cn.com/problems/longest-common-prefix/description/ 7 | # 8 | # algorithms 9 | # Easy (31.66%) 10 | # Total Accepted: 49.2K 11 | # Total Submissions: 155.2K 12 | # Testcase Example: '["flower","flow","flight"]' 13 | # 14 | # 编写一个函数来查找字符串数组中的最长公共前缀。 15 | # 16 | # 如果不存在公共前缀,返回空字符串 ""。 17 | # 18 | # 示例 1: 19 | # 20 | # 输入: ["flower","flow","flight"] 21 | # 输出: "fl" 22 | # 23 | # 24 | # 示例 2: 25 | # 26 | # 输入: ["dog","racecar","car"] 27 | # 输出: "" 28 | # 解释: 输入不存在公共前缀。 29 | # 30 | # 31 | # 说明: 32 | # 33 | # 所有输入只包含小写字母 a-z 。 34 | # 35 | # 36 | class Solution: 37 | def longestCommonPrefix(self, strs: 'List[str]') -> 'str': 38 | if not len(strs): 39 | return '' 40 | prefix = strs[0] 41 | for i in range(len(strs)): 42 | while strs[i].find(prefix) != 0: 43 | prefix = prefix[0: len(prefix) - 1] 44 | if prefix == '': 45 | return '' 46 | return prefix 47 | -------------------------------------------------------------------------------- /easy/35.搜索插入位置/35.search-insert-position.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=35 lang=golang 3 | * 4 | * [35] 搜索插入位置 5 | * 6 | * https://leetcode-cn.com/problems/search-insert-position/description/ 7 | * 8 | * algorithms 9 | * Easy (42.49%) 10 | * Total Accepted: 28.4K 11 | * Total Submissions: 66.7K 12 | * Testcase Example: '[1,3,5,6]\n5' 13 | * 14 | * 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 15 | * 16 | * 你可以假设数组中无重复元素。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: [1,3,5,6], 5 21 | * 输出: 2 22 | * 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: [1,3,5,6], 2 27 | * 输出: 1 28 | * 29 | * 30 | * 示例 3: 31 | * 32 | * 输入: [1,3,5,6], 7 33 | * 输出: 4 34 | * 35 | * 36 | * 示例 4: 37 | * 38 | * 输入: [1,3,5,6], 0 39 | * 输出: 0 40 | * 41 | * 42 | */ 43 | func searchInsert(nums []int, target int) int { 44 | l := 0 45 | r := len(nums) 46 | for l < r { 47 | mid := l + (r-l)/2 48 | if nums[mid] > target { 49 | r = mid 50 | } else if nums[mid] < target { 51 | l = mid + 1 52 | } else { 53 | return mid 54 | } 55 | } 56 | return l 57 | } 58 | -------------------------------------------------------------------------------- /easy/70.爬楼梯/70.爬楼梯.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=70 lang=golang 3 | * 4 | * [70] 爬楼梯 5 | * 6 | * https://leetcode-cn.com/problems/climbing-stairs/description/ 7 | * 8 | * algorithms 9 | * Easy (43.84%) 10 | * Total Accepted: 29.6K 11 | * Total Submissions: 67.4K 12 | * Testcase Example: '2' 13 | * 14 | * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 15 | * 16 | * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 17 | * 18 | * 注意:给定 n 是一个正整数。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: 2 23 | * 输出: 2 24 | * 解释: 有两种方法可以爬到楼顶。 25 | * 1. 1 阶 + 1 阶 26 | * 2. 2 阶 27 | * 28 | * 示例 2: 29 | * 30 | * 输入: 3 31 | * 输出: 3 32 | * 解释: 有三种方法可以爬到楼顶。 33 | * 1. 1 阶 + 1 阶 + 1 阶 34 | * 2. 1 阶 + 2 阶 35 | * 3. 2 阶 + 1 阶 36 | * 37 | * 38 | */ 39 | func climbStairs(n int) int { 40 | cache := map[int]int{} 41 | return climb(n, cache) 42 | } 43 | 44 | func climb(n int, cache map[int]int) int { 45 | if _, ok := cache[n]; ok { 46 | return cache[n] 47 | } 48 | if n < 2 { 49 | cache[n] = 1 50 | } else { 51 | cache[n] = climb(n-1, cache) + climb(n-2, cache) 52 | } 53 | return cache[n] 54 | } 55 | -------------------------------------------------------------------------------- /easy/83.删除排序链表中的重复元素/83.删除排序链表中的重复元素.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=83 lang=javascript 3 | * 4 | * [83] 删除排序链表中的重复元素 5 | * 6 | * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/ 7 | * 8 | * algorithms 9 | * Easy (43.50%) 10 | * Total Accepted: 16.1K 11 | * Total Submissions: 37.1K 12 | * Testcase Example: '[1,1,2]' 13 | * 14 | * 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 1->1->2 19 | * 输出: 1->2 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: 1->1->2->3->3 25 | * 输出: 1->2->3 26 | * 27 | */ 28 | /** 29 | * Definition for singly-linked list. 30 | * function ListNode(val) { 31 | * this.val = val; 32 | * this.next = null; 33 | * } 34 | */ 35 | /** 36 | * @param {ListNode} head 37 | * @return {ListNode} 38 | */ 39 | var deleteDuplicates = function(head) { 40 | const node = head 41 | while (head && head.next) { 42 | if (head.val === head.next.val) { 43 | head.next = head.next.next 44 | } else { 45 | head = head.next 46 | } 47 | } 48 | return node 49 | } 50 | -------------------------------------------------------------------------------- /easy/9.回文数/9.回文数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=9 lang=python3 3 | # 4 | # [9] 回文数 5 | # 6 | # https://leetcode-cn.com/problems/palindrome-number/description/ 7 | # 8 | # algorithms 9 | # Easy (55.96%) 10 | # Total Accepted: 68.4K 11 | # Total Submissions: 122.2K 12 | # Testcase Example: '121' 13 | # 14 | # 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 15 | # 16 | # 示例 1: 17 | # 18 | # 输入: 121 19 | # 输出: true 20 | # 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: -121 25 | # 输出: false 26 | # 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 27 | # 28 | # 29 | # 示例 3: 30 | # 31 | # 输入: 10 32 | # 输出: false 33 | # 解释: 从右向左读, 为 01 。因此它不是一个回文数。 34 | # 35 | # 36 | # 进阶: 37 | # 38 | # 你能不将整数转为字符串来解决这个问题吗? 39 | # 40 | # 41 | class Solution: 42 | def isPalindrome(self, x: 'int') -> 'bool': 43 | if x < 0: 44 | return False 45 | return self.reverse(x) == x 46 | 47 | def reverse(self, x: 'int') -> 'int': 48 | res = 0 49 | while x != 0: 50 | n = x % 10 51 | res = res * 10 + n 52 | x //= 10 53 | return res 54 | -------------------------------------------------------------------------------- /easy/14.最长公共前缀/14.最长公共前缀.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=14 lang=golang 3 | * 4 | * [14] 最长公共前缀 5 | * 6 | * https://leetcode-cn.com/problems/longest-common-prefix/description/ 7 | * 8 | * algorithms 9 | * Easy (31.66%) 10 | * Total Accepted: 49.2K 11 | * Total Submissions: 155.2K 12 | * Testcase Example: '["flower","flow","flight"]' 13 | * 14 | * 编写一个函数来查找字符串数组中的最长公共前缀。 15 | * 16 | * 如果不存在公共前缀,返回空字符串 ""。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: ["flower","flow","flight"] 21 | * 输出: "fl" 22 | * 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: ["dog","racecar","car"] 27 | * 输出: "" 28 | * 解释: 输入不存在公共前缀。 29 | * 30 | * 31 | * 说明: 32 | * 33 | * 所有输入只包含小写字母 a-z 。 34 | * 35 | */ 36 | import "strings" 37 | 38 | func longestCommonPrefix(strs []string) string { 39 | if len(strs) == 0 { 40 | return "" 41 | } 42 | prefix := strs[0] 43 | for _, str := range strs { 44 | for !strings.HasPrefix(str, prefix) { 45 | prefix = prefix[:len(prefix)-1] 46 | if len(prefix) == 0 { 47 | prefix = "" 48 | break 49 | } 50 | } 51 | } 52 | return prefix 53 | } 54 | -------------------------------------------------------------------------------- /medium/11.盛最多水的容器/11.盛最多水的容器.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=11 lang=golang 3 | * 4 | * [11] 盛最多水的容器 5 | * 6 | * https://leetcode-cn.com/problems/container-with-most-water/description/ 7 | * 8 | * algorithms 9 | * Medium (53.70%) 10 | * Total Accepted: 32.6K 11 | * Total Submissions: 60.6K 12 | * Testcase Example: '[1,8,6,2,5,4,8,3,7]' 13 | * 14 | * 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 15 | * (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 16 | * 17 | * 说明:你不能倾斜容器,且 n 的值至少为 2。 18 | * 19 | * 20 | * 21 | * 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 22 | * 23 | * 24 | * 25 | * 示例: 26 | * 27 | * 输入: [1,8,6,2,5,4,8,3,7] 28 | * 输出: 49 29 | * 30 | */ 31 | import "math" 32 | 33 | func maxArea(height []int) int { 34 | l, r := 0, len(height)-1 35 | var ans float64 = 0 36 | for l < r { 37 | ans = math.Max(ans, math.Min(float64(height[r]), float64(height[l]))*float64(r-l)) 38 | if height[l] < height[r] { 39 | l++ 40 | } else { 41 | r-- 42 | } 43 | } 44 | return int(ans) 45 | } 46 | 47 | -------------------------------------------------------------------------------- /easy/121.买卖股票的最佳时机/121.买卖股票的最佳时机.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=121 lang=python3 3 | # 4 | # [121] 买卖股票的最佳时机 5 | # 6 | # https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ 7 | # 8 | # algorithms 9 | # Easy (47.72%) 10 | # Total Accepted: 28.5K 11 | # Total Submissions: 59.5K 12 | # Testcase Example: '[7,1,5,3,6,4]' 13 | # 14 | # 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 15 | # 16 | # 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。 17 | # 18 | # 注意你不能在买入股票前卖出股票。 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: [7,1,5,3,6,4] 23 | # 输出: 5 24 | # 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 25 | # ⁠ 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。 26 | # 27 | # 28 | # 示例 2: 29 | # 30 | # 输入: [7,6,4,3,1] 31 | # 输出: 0 32 | # 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 33 | # 34 | # 35 | # 36 | 37 | 38 | class Solution: 39 | def maxProfit(self, prices: 'List[int]') -> 'int': 40 | cur = 0 41 | res = 0 42 | for i in range(len(prices) - 1): 43 | cur = max(cur + prices[i + 1] - prices[i], 0) 44 | res = max(cur, res) 45 | return res 46 | -------------------------------------------------------------------------------- /easy/448.找到所有数组中消失的数字/448.找到所有数组中消失的数字.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=448 lang=javascript 3 | * 4 | * [448] 找到所有数组中消失的数字 5 | * 6 | * https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/ 7 | * 8 | * algorithms 9 | * Easy (46.40%) 10 | * Total Accepted: 5.6K 11 | * Total Submissions: 12.1K 12 | * Testcase Example: '[4,3,2,7,8,2,3,1]' 13 | * 14 | * 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 15 | * 16 | * 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 17 | * 18 | * 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 19 | * 20 | * 示例: 21 | * 22 | * 23 | * 输入: 24 | * [4,3,2,7,8,2,3,1] 25 | * 26 | * 输出: 27 | * [5,6] 28 | * 29 | * 30 | */ 31 | /** 32 | * @param {number[]} nums 33 | * @return {number[]} 34 | */ 35 | var findDisappearedNumbers = function(nums) { 36 | for (let n of nums) { 37 | nums[Math.abs(n) - 1] = -Math.abs(nums[Math.abs(n) - 1]) 38 | } 39 | let ans = [] 40 | for (const [i, n] of nums.entries()) { 41 | if (n > 0) { 42 | ans.push(i + 1) 43 | } 44 | } 45 | return ans 46 | } 47 | -------------------------------------------------------------------------------- /medium/11.盛最多水的容器/11.盛最多水的容器.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=11 lang=python3 3 | # 4 | # [11] 盛最多水的容器 5 | # 6 | # https://leetcode-cn.com/problems/container-with-most-water/description/ 7 | # 8 | # algorithms 9 | # Medium (53.70%) 10 | # Total Accepted: 32.6K 11 | # Total Submissions: 60.6K 12 | # Testcase Example: '[1,8,6,2,5,4,8,3,7]' 13 | # 14 | # 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, 15 | # ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 16 | # 17 | # 说明:你不能倾斜容器,且 n 的值至少为 2。 18 | # 19 | # 20 | # 21 | # 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 22 | # 23 | # 24 | # 25 | # 示例: 26 | # 27 | # 输入: [1,8,6,2,5,4,8,3,7] 28 | # 输出: 49 29 | # 30 | # 31 | 32 | 33 | class Solution: 34 | def maxArea(self, height: List[int]) -> int: 35 | ans = 0 36 | l, r = 0, len(height) - 1 37 | while l < r: 38 | ans = max(ans, min(height[l], height[r]) * (r - l)) 39 | if height[l] < height[r]: 40 | l += 1 41 | else: 42 | r -= 1 43 | return ans 44 | -------------------------------------------------------------------------------- /easy/219.存在重复元素-ii/219.存在重复元素-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=219 lang=python3 3 | # 4 | # [219] 存在重复元素 II 5 | # 6 | # https://leetcode-cn.com/problems/contains-duplicate-ii/description/ 7 | # 8 | # algorithms 9 | # Easy (32.71%) 10 | # Total Accepted: 8.9K 11 | # Total Submissions: 27K 12 | # Testcase Example: '[1,2,3,1]\n3' 13 | # 14 | # 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 15 | # 的差的绝对值最大为 k。 16 | # 17 | # 示例 1: 18 | # 19 | # 输入: nums = [1,2,3,1], k = 3 20 | # 输出: true 21 | # 22 | # 示例 2: 23 | # 24 | # 输入: nums = [1,0,1,1], k = 1 25 | # 输出: true 26 | # 27 | # 示例 3: 28 | # 29 | # 输入: nums = [1,2,3,1,2,3], k = 2 30 | # 输出: false 31 | # 32 | # 33 | 34 | 35 | class Solution: 36 | def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: 37 | m = {} 38 | for i in range(len(nums)): 39 | n = nums[i] 40 | if n in m: 41 | if abs(i - m[n]) <= k: 42 | return True 43 | m[n] = i 44 | else: 45 | m[n] = i 46 | return False 47 | -------------------------------------------------------------------------------- /easy/389.找不同/389.找不同.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=389 lang=golang 3 | * 4 | * [389] 找不同 5 | * 6 | * https://leetcode-cn.com/problems/find-the-difference/description/ 7 | * 8 | * algorithms 9 | * Easy (54.04%) 10 | * Total Accepted: 6.5K 11 | * Total Submissions: 12K 12 | * Testcase Example: '"abcd"\n"abcde"' 13 | * 14 | * 给定两个字符串 s 和 t,它们只包含小写字母。 15 | * 16 | * 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 17 | * 18 | * 请找出在 t 中被添加的字母。 19 | * 20 | * 21 | * 22 | * 示例: 23 | * 24 | * 输入: 25 | * s = "abcd" 26 | * t = "abcde" 27 | * 28 | * 输出: 29 | * e 30 | * 31 | * 解释: 32 | * 'e' 是那个被添加的字母。 33 | * 34 | * 35 | */ 36 | 37 | func findTheDifference(s string, t string) byte { 38 | m := map[byte]int{} 39 | var res byte 40 | for i := 0; i < len(s); i++ { 41 | if _, ok := m[s[i]]; ok { 42 | m[s[i]]++ 43 | } else { 44 | m[s[i]] = 1 45 | } 46 | } 47 | for i := 0; i < len(t); i++ { 48 | if _, ok := m[t[i]]; !ok { 49 | res = t[i] 50 | } else { 51 | m[t[i]]-- 52 | if m[t[i]] < 0 { 53 | res = t[i] 54 | } 55 | } 56 | } 57 | return res 58 | } 59 | 60 | -------------------------------------------------------------------------------- /easy/404.左叶子之和/404.左叶子之和.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=404 lang=python3 3 | # 4 | # [404] 左叶子之和 5 | # 6 | # https://leetcode-cn.com/problems/sum-of-left-leaves/description/ 7 | # 8 | # algorithms 9 | # Easy (48.25%) 10 | # Total Accepted: 4.5K 11 | # Total Submissions: 9.4K 12 | # Testcase Example: '[3,9,20,null,null,15,7]' 13 | # 14 | # 计算给定二叉树的所有左叶子之和。 15 | # 16 | # 示例: 17 | # 18 | # 19 | # ⁠ 3 20 | # ⁠ / \ 21 | # ⁠ 9 20 22 | # ⁠ / \ 23 | # ⁠ 15 7 24 | # 25 | # 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 26 | # 27 | # 28 | # 29 | # 30 | # Definition for a binary tree node. 31 | # class TreeNode: 32 | # def __init__(self, x): 33 | # self.val = x 34 | # self.left = None 35 | # self.right = None 36 | 37 | 38 | class Solution: 39 | def sumOfLeftLeaves(self, root: TreeNode) -> int: 40 | if not root: 41 | return 0 42 | ans = 0 43 | if root and root.left and not root.left.left and not root.left.right: 44 | ans += root.left.val 45 | return ans + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right) 46 | -------------------------------------------------------------------------------- /easy/9.回文数/9.回文数.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=9 lang=golang 3 | * 4 | * [9] 回文数 5 | * 6 | * https://leetcode-cn.com/problems/palindrome-number/description/ 7 | * 8 | * algorithms 9 | * Easy (55.96%) 10 | * Total Accepted: 68.4K 11 | * Total Submissions: 122.2K 12 | * Testcase Example: '121' 13 | * 14 | * 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 15 | * 16 | * 示例 1: 17 | * 18 | * 输入: 121 19 | * 输出: true 20 | * 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: -121 25 | * 输出: false 26 | * 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 27 | * 28 | * 29 | * 示例 3: 30 | * 31 | * 输入: 10 32 | * 输出: false 33 | * 解释: 从右向左读, 为 01 。因此它不是一个回文数。 34 | * 35 | * 36 | * 进阶: 37 | * 38 | * 你能不将整数转为字符串来解决这个问题吗? 39 | * 40 | */ 41 | func isPalindrome(x int) bool { 42 | var res bool 43 | if x < 0 || (x%10 == 0 && x != 0) { 44 | res = false 45 | } else { 46 | revertedNumber := 0 47 | for x > revertedNumber { 48 | revertedNumber = revertedNumber*10 + x%10 49 | x /= 10 50 | } 51 | 52 | res = x == revertedNumber || x == revertedNumber/10 53 | } 54 | 55 | return res 56 | } -------------------------------------------------------------------------------- /medium/187.重复的dna序列.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=187 lang=javascript 3 | * 4 | * [187] 重复的DNA序列 5 | * 6 | * https://leetcode-cn.com/problems/repeated-dna-sequences/description/ 7 | * 8 | * algorithms 9 | * Medium (42.75%) 10 | * Likes: 30 11 | * Dislikes: 0 12 | * Total Accepted: 3.7K 13 | * Total Submissions: 8.7K 14 | * Testcase Example: '"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"' 15 | * 16 | * 所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究 DNA 时,识别 DNA 17 | * 中的重复序列有时会对研究非常有帮助。 18 | * 19 | * 编写一个函数来查找 DNA 分子中所有出现超多一次的10个字母长的序列(子串)。 20 | * 21 | * 示例: 22 | * 23 | * 输入: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 24 | * 25 | * 输出: ["AAAAACCCCC", "CCCCCAAAAA"] 26 | * 27 | */ 28 | /** 29 | * @param {string} s 30 | * @return {string[]} 31 | */ 32 | var findRepeatedDnaSequences = function(s) { 33 | const map = {} 34 | const res = new Set() 35 | for (let i = 0; i <= s.length - 10; i++) { 36 | const str = s.slice(i, i + 10) 37 | if (str in map) { 38 | res.add(str) 39 | } else { 40 | map[str] = 1 41 | } 42 | } 43 | return [...res] 44 | } 45 | -------------------------------------------------------------------------------- /medium/94.二叉树的中序遍历/94.二叉树的中序遍历.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=94 lang=javascript 3 | * 4 | * [94] 二叉树的中序遍历 5 | * 6 | * https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/ 7 | * 8 | * algorithms 9 | * Medium (64.87%) 10 | * Likes: 189 11 | * Dislikes: 0 12 | * Total Accepted: 31K 13 | * Total Submissions: 47.4K 14 | * Testcase Example: '[1,null,2,3]' 15 | * 16 | * 给定一个二叉树,返回它的中序 遍历。 17 | * 18 | * 示例: 19 | * 20 | * 输入: [1,null,2,3] 21 | * ⁠ 1 22 | * ⁠ \ 23 | * ⁠ 2 24 | * ⁠ / 25 | * ⁠ 3 26 | * 27 | * 输出: [1,3,2] 28 | * 29 | * 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 30 | * 31 | */ 32 | /** 33 | * Definition for a binary tree node. 34 | * function TreeNode(val) { 35 | * this.val = val; 36 | * this.left = this.right = null; 37 | * } 38 | */ 39 | /** 40 | * @param {TreeNode} root 41 | * @return {number[]} 42 | */ 43 | var inorderTraversal = function(root, res = []) { 44 | if (root) { 45 | inorderTraversal(root.left, res) 46 | res.push(root.val) 47 | inorderTraversal(root.right, res) 48 | } 49 | return res 50 | } 51 | -------------------------------------------------------------------------------- /easy/415.字符串相加/415.字符串相加.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=415 lang=python3 3 | # 4 | # [415] 字符串相加 5 | # 6 | # https://leetcode-cn.com/problems/add-strings/description/ 7 | # 8 | # algorithms 9 | # Easy (42.83%) 10 | # Total Accepted: 4.9K 11 | # Total Submissions: 11.3K 12 | # Testcase Example: '"0"\n"0"' 13 | # 14 | # 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。 15 | # 16 | # 注意: 17 | # 18 | # 19 | # num1 和num2 的长度都小于 5100. 20 | # num1 和num2 都只包含数字 0-9. 21 | # num1 和num2 都不包含任何前导零。 22 | # 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。 23 | # 24 | # 25 | # 26 | 27 | 28 | class Solution: 29 | def addStrings(self, num1: str, num2: str) -> str: 30 | l1, l2 = len(num1) - 1, len(num2) - 1 31 | carry, ans = 0, '' 32 | while l1 >= 0 or l2 >= 0: 33 | n1 = int(num1[l1]) if l1 >= 0 else 0 34 | n2 = int(num2[l2]) if l2 >= 0 else 0 35 | val = carry + n1 + n2 36 | carry = val // 10 37 | val %= 10 38 | ans = str(val) + ans 39 | l1 -= 1 40 | l2 -= 1 41 | return str(carry) + ans if carry else ans 42 | -------------------------------------------------------------------------------- /easy/118.杨辉三角/118.杨辉三角.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=118 lang=javascript 3 | * 4 | * [118] 杨辉三角 5 | * 6 | * https://leetcode-cn.com/problems/pascals-triangle/description/ 7 | * 8 | * algorithms 9 | * Easy (59.45%) 10 | * Total Accepted: 15.9K 11 | * Total Submissions: 26.6K 12 | * Testcase Example: '5' 13 | * 14 | * 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 15 | * 16 | * 17 | * 18 | * 在杨辉三角中,每个数是它左上方和右上方的数的和。 19 | * 20 | * 示例: 21 | * 22 | * 输入: 5 23 | * 输出: 24 | * [ 25 | * ⁠ [1], 26 | * ⁠ [1,1], 27 | * ⁠ [1,2,1], 28 | * ⁠ [1,3,3,1], 29 | * ⁠[1,4,6,4,1] 30 | * ] 31 | * 32 | */ 33 | /** 34 | * @param {number} numRows 35 | * @return {number[][]} 36 | */ 37 | var generate = function(numRows) { 38 | if (!numRows) return [] 39 | let n = 1 40 | const result = [[1]] 41 | while (n < numRows) { 42 | const cur = result[result.length - 1] 43 | const arr = [] 44 | for (let i = 0; i <= cur.length; i++) { 45 | arr[i] = (cur[i] ? cur[i] : 0) + (cur[i - 1] ? cur[i - 1] : 0) 46 | } 47 | result.push(arr) 48 | n++ 49 | } 50 | return result 51 | } 52 | -------------------------------------------------------------------------------- /easy/121.买卖股票的最佳时机/121.买卖股票的最佳时机.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=121 lang=golang 3 | * 4 | * [121] 买卖股票的最佳时机 5 | * 6 | * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ 7 | * 8 | * algorithms 9 | * Easy (47.72%) 10 | * Total Accepted: 28.5K 11 | * Total Submissions: 59.5K 12 | * Testcase Example: '[7,1,5,3,6,4]' 13 | * 14 | * 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 15 | * 16 | * 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。 17 | * 18 | * 注意你不能在买入股票前卖出股票。 19 | * 20 | * 示例 1: 21 | * 22 | * 输入: [7,1,5,3,6,4] 23 | * 输出: 5 24 | * 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 25 | * ⁠ 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。 26 | * 27 | * 28 | * 示例 2: 29 | * 30 | * 输入: [7,6,4,3,1] 31 | * 输出: 0 32 | * 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 33 | * 34 | * 35 | */ 36 | import "math" 37 | 38 | func maxProfit(prices []int) int { 39 | var cur float64 = 0 40 | var res float64 = 0 41 | for i := 0; i < len(prices)-1; i++ { 42 | cur = math.Max(cur+float64(prices[i+1]-prices[i]), 0) 43 | res = math.Max(cur, res) 44 | } 45 | return int(res) 46 | } 47 | -------------------------------------------------------------------------------- /easy/203.移除链表元素/203.移除链表元素.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=203 lang=javascript 3 | * 4 | * [203] 移除链表元素 5 | * 6 | * https://leetcode-cn.com/problems/remove-linked-list-elements/description/ 7 | * 8 | * algorithms 9 | * Easy (38.84%) 10 | * Total Accepted: 16.2K 11 | * Total Submissions: 41.5K 12 | * Testcase Example: '[1,2,6,3,4,5,6]\n6' 13 | * 14 | * 删除链表中等于给定值 val 的所有节点。 15 | * 16 | * 示例: 17 | * 18 | * 输入: 1->2->6->3->4->5->6, val = 6 19 | * 输出: 1->2->3->4->5 20 | * 21 | * 22 | */ 23 | /** 24 | * Definition for singly-linked list. 25 | * function ListNode(val) { 26 | * this.val = val; 27 | * this.next = null; 28 | * } 29 | */ 30 | /** 31 | * @param {ListNode} head 32 | * @param {number} val 33 | * @return {ListNode} 34 | */ 35 | var removeElements = function(head, val) { 36 | const header = new ListNode(-Infinity) 37 | header.next = head 38 | let node = header 39 | while (node.next) { 40 | if (node.next.val === val) { 41 | node.next = node.next.next 42 | } else { 43 | node = node.next 44 | } 45 | } 46 | return header.next 47 | } 48 | -------------------------------------------------------------------------------- /easy/263.丑数/263.丑数.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=263 lang=javascript 3 | * 4 | * [263] 丑数 5 | * 6 | * https://leetcode-cn.com/problems/ugly-number/description/ 7 | * 8 | * algorithms 9 | * Easy (44.39%) 10 | * Total Accepted: 6.3K 11 | * Total Submissions: 14.2K 12 | * Testcase Example: '6' 13 | * 14 | * 编写一个程序判断给定的数是否为丑数。 15 | * 16 | * 丑数就是只包含质因数 2, 3, 5 的正整数。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: 6 21 | * 输出: true 22 | * 解释: 6 = 2 × 3 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: 8 27 | * 输出: true 28 | * 解释: 8 = 2 × 2 × 2 29 | * 30 | * 31 | * 示例 3: 32 | * 33 | * 输入: 14 34 | * 输出: false 35 | * 解释: 14 不是丑数,因为它包含了另外一个质因数 7。 36 | * 37 | * 说明: 38 | * 39 | * 40 | * 1 是丑数。 41 | * 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。 42 | * 43 | * 44 | */ 45 | /** 46 | * @param {number} num 47 | * @return {boolean} 48 | */ 49 | var isUgly = function(num) { 50 | if (!num) return false 51 | while (num % 2 === 0) { 52 | num /= 2 53 | } 54 | while (num % 3 === 0) { 55 | num /= 3 56 | } 57 | while (num % 5 === 0) { 58 | num /= 5 59 | } 60 | return num === 1 61 | } 62 | -------------------------------------------------------------------------------- /easy/14.最长公共前缀/14.最长公共前缀.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=14 lang=javascript 3 | * 4 | * [14] 最长公共前缀 5 | * 6 | * https://leetcode-cn.com/problems/longest-common-prefix/description/ 7 | * 8 | * algorithms 9 | * Easy (31.66%) 10 | * Total Accepted: 49.2K 11 | * Total Submissions: 155.2K 12 | * Testcase Example: '["flower","flow","flight"]' 13 | * 14 | * 编写一个函数来查找字符串数组中的最长公共前缀。 15 | * 16 | * 如果不存在公共前缀,返回空字符串 ""。 17 | * 18 | * 示例 1: 19 | * 20 | * 输入: ["flower","flow","flight"] 21 | * 输出: "fl" 22 | * 23 | * 24 | * 示例 2: 25 | * 26 | * 输入: ["dog","racecar","car"] 27 | * 输出: "" 28 | * 解释: 输入不存在公共前缀。 29 | * 30 | * 31 | * 说明: 32 | * 33 | * 所有输入只包含小写字母 a-z 。 34 | * 35 | */ 36 | /** 37 | * @param {string[]} strs 38 | * @return {string} 39 | */ 40 | var longestCommonPrefix = function(strs) { 41 | if (!strs.length) return '' 42 | let prefix = strs[0] 43 | for (let i = 0; i < strs.length; i++) { 44 | while (strs[i].indexOf(prefix) !== 0) { 45 | prefix = prefix.substring(0, prefix.length - 1) 46 | if (!prefix) return '' 47 | } 48 | } 49 | return prefix 50 | } 51 | -------------------------------------------------------------------------------- /easy/171.excel表列序号/171.excel表列序号.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=171 lang=javascript 3 | * 4 | * [171] Excel表列序号 5 | * 6 | * https://leetcode-cn.com/problems/excel-sheet-column-number/description/ 7 | * 8 | * algorithms 9 | * Easy (63.07%) 10 | * Total Accepted: 9K 11 | * Total Submissions: 14.2K 12 | * Testcase Example: '"A"' 13 | * 14 | * 给定一个Excel表格中的列名称,返回其相应的列序号。 15 | * 16 | * 例如, 17 | * 18 | * ⁠ A -> 1 19 | * ⁠ B -> 2 20 | * ⁠ C -> 3 21 | * ⁠ ... 22 | * ⁠ Z -> 26 23 | * ⁠ AA -> 27 24 | * ⁠ AB -> 28 25 | * ⁠ ... 26 | * 27 | * 28 | * 示例 1: 29 | * 30 | * 输入: "A" 31 | * 输出: 1 32 | * 33 | * 34 | * 示例 2: 35 | * 36 | * 输入: "AB" 37 | * 输出: 28 38 | * 39 | * 40 | * 示例 3: 41 | * 42 | * 输入: "ZY" 43 | * 输出: 701 44 | * 45 | * 致谢: 46 | * 特别感谢 @ts 添加此问题并创建所有测试用例。 47 | * 48 | */ 49 | /** 50 | * @param {string} s 51 | * @return {number} 52 | */ 53 | var titleToNumber = function(s) { 54 | let res = 0 55 | for (let i = 0; i < s.length; i++) { 56 | const str = s[i] 57 | res = 26 * res + str.charCodeAt() - 65 + 1 58 | } 59 | return res 60 | } 61 | -------------------------------------------------------------------------------- /easy/409.最长回文串/409.最长回文串.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=409 lang=golang 3 | * 4 | * [409] 最长回文串 5 | * 6 | * https://leetcode-cn.com/problems/longest-palindrome/description/ 7 | * 8 | * algorithms 9 | * Easy (45.63%) 10 | * Total Accepted: 3.6K 11 | * Total Submissions: 7.8K 12 | * Testcase Example: '"abccccdd"' 13 | * 14 | * 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 15 | * 16 | * 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。 17 | * 18 | * 注意: 19 | * 假设字符串的长度不会超过 1010。 20 | * 21 | * 示例 1: 22 | * 23 | * 24 | * 输入: 25 | * "abccccdd" 26 | * 27 | * 输出: 28 | * 7 29 | * 30 | * 解释: 31 | * 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。 32 | * 33 | * 34 | */ 35 | func longestPalindrome(s string) int { 36 | ans := 0 37 | m := map[rune]int{} 38 | flag := false 39 | for _, str := range s { 40 | if _, ok := m[str]; ok { 41 | m[str]++ 42 | } else { 43 | m[str] = 1 44 | } 45 | } 46 | for _, val := range m { 47 | if val%2 == 0 { 48 | ans += val 49 | } else { 50 | flag = true 51 | ans += val - 1 52 | } 53 | } 54 | if flag { 55 | return ans + 1 56 | } 57 | return ans 58 | } 59 | 60 | -------------------------------------------------------------------------------- /medium/144.二叉树的前序遍历/144.二叉树的前序遍历.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=144 lang=javascript 3 | * 4 | * [144] 二叉树的前序遍历 5 | * 6 | * https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/ 7 | * 8 | * algorithms 9 | * Medium (59.91%) 10 | * Likes: 116 11 | * Dislikes: 0 12 | * Total Accepted: 24.9K 13 | * Total Submissions: 41.5K 14 | * Testcase Example: '[1,null,2,3]' 15 | * 16 | * 给定一个二叉树,返回它的 前序 遍历。 17 | * 18 | * 示例: 19 | * 20 | * 输入: [1,null,2,3] 21 | * ⁠ 1 22 | * ⁠ \ 23 | * ⁠ 2 24 | * ⁠ / 25 | * ⁠ 3 26 | * 27 | * 输出: [1,2,3] 28 | * 29 | * 30 | * 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 31 | * 32 | */ 33 | /** 34 | * Definition for a binary tree node. 35 | * function TreeNode(val) { 36 | * this.val = val; 37 | * this.left = this.right = null; 38 | * } 39 | */ 40 | /** 41 | * @param {TreeNode} root 42 | * @return {number[]} 43 | */ 44 | var preorderTraversal = function(root, res = []) { 45 | if (root) { 46 | res.push(root.val) 47 | preorderTraversal(root.left, res) 48 | preorderTraversal(root.right, res) 49 | } 50 | return res 51 | } 52 | -------------------------------------------------------------------------------- /easy/168.excel表列名称/168.excel表列名称.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=168 lang=golang 3 | * 4 | * [168] Excel表列名称 5 | * 6 | * https://leetcode-cn.com/problems/excel-sheet-column-title/description/ 7 | * 8 | * algorithms 9 | * Easy (29.78%) 10 | * Total Accepted: 5.1K 11 | * Total Submissions: 17K 12 | * Testcase Example: '1' 13 | * 14 | * 给定一个正整数,返回它在 Excel 表中相对应的列名称。 15 | * 16 | * 例如, 17 | * 18 | * ⁠ 1 -> A 19 | * ⁠ 2 -> B 20 | * ⁠ 3 -> C 21 | * ⁠ ... 22 | * ⁠ 26 -> Z 23 | * ⁠ 27 -> AA 24 | * ⁠ 28 -> AB 25 | * ⁠ ... 26 | * 27 | * 28 | * 示例 1: 29 | * 30 | * 输入: 1 31 | * 输出: "A" 32 | * 33 | * 34 | * 示例 2: 35 | * 36 | * 输入: 28 37 | * 输出: "AB" 38 | * 39 | * 40 | * 示例 3: 41 | * 42 | * 输入: 701 43 | * 输出: "ZY" 44 | * 45 | * 46 | */ 47 | func convertToTitle(n int) string { 48 | arr := []rune{'Z'} 49 | for i := 65; i <= 90; i++ { 50 | arr = append(arr, rune(i)) 51 | } 52 | res := "" 53 | for n != 0 { 54 | num := n % 26 55 | res = string(arr[num]) + res 56 | n = (n - num) / 26 57 | if num == 0 { 58 | n-- 59 | } 60 | } 61 | return res 62 | } 63 | 64 | -------------------------------------------------------------------------------- /easy/88.合并两个有序数组/88.合并两个有序数组.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=88 lang=golang 3 | * 4 | * [88] 合并两个有序数组 5 | * 6 | * https://leetcode-cn.com/problems/merge-sorted-array/description/ 7 | * 8 | * algorithms 9 | * Easy (42.33%) 10 | * Total Accepted: 25.8K 11 | * Total Submissions: 60.8K 12 | * Testcase Example: '[1,2,3,0,0,0]\n3\n[2,5,6]\n3' 13 | * 14 | * 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。 15 | * 16 | * 说明: 17 | * 18 | * 19 | * 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。 20 | * 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。 21 | * 22 | * 23 | * 示例: 24 | * 25 | * 输入: 26 | * nums1 = [1,2,3,0,0,0], m = 3 27 | * nums2 = [2,5,6], n = 3 28 | * 29 | * 输出: [1,2,2,3,5,6] 30 | * 31 | */ 32 | func merge(nums1 []int, m int, nums2 []int, n int) { 33 | pos := m + n - 1 34 | m-- 35 | n-- 36 | for m >= 0 && n >= 0 { 37 | if nums1[m] > nums2[n] { 38 | nums1[pos] = nums1[m] 39 | pos-- 40 | m-- 41 | } else { 42 | nums1[pos] = nums2[n] 43 | pos-- 44 | n-- 45 | } 46 | } 47 | for n >= 0 { 48 | nums1[pos] = nums2[n] 49 | pos-- 50 | n-- 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /medium/94.二叉树的中序遍历/94.二叉树的中序遍历.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=94 lang=golang 3 | * 4 | * [94] 二叉树的中序遍历 5 | * 6 | * https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/ 7 | * 8 | * algorithms 9 | * Medium (64.80%) 10 | * Likes: 189 11 | * Dislikes: 0 12 | * Total Accepted: 31.5K 13 | * Total Submissions: 48.1K 14 | * Testcase Example: '[1,null,2,3]' 15 | * 16 | * 给定一个二叉树,返回它的中序 遍历。 17 | * 18 | * 示例: 19 | * 20 | * 输入: [1,null,2,3] 21 | * ⁠ 1 22 | * ⁠ \ 23 | * ⁠ 2 24 | * ⁠ / 25 | * ⁠ 3 26 | * 27 | * 输出: [1,3,2] 28 | * 29 | * 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 30 | * 31 | */ 32 | /** 33 | * Definition for a binary tree node. 34 | * type TreeNode struct { 35 | * Val int 36 | * Left *TreeNode 37 | * Right *TreeNode 38 | * } 39 | */ 40 | func inorderTraversal(root *TreeNode) []int { 41 | var res []int 42 | inorder(root, &res) 43 | return res 44 | } 45 | 46 | func inorder(root *TreeNode, res *[]int) { 47 | if root != nil { 48 | inorder(root.Left, res) 49 | *res = append(*res, root.Val) 50 | inorder(root.Right, res) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /easy/219.存在重复元素-ii/219.存在重复元素-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=219 lang=javascript 3 | * 4 | * [219] 存在重复元素 II 5 | * 6 | * https://leetcode-cn.com/problems/contains-duplicate-ii/description/ 7 | * 8 | * algorithms 9 | * Easy (32.71%) 10 | * Total Accepted: 8.9K 11 | * Total Submissions: 27K 12 | * Testcase Example: '[1,2,3,1]\n3' 13 | * 14 | * 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 15 | * 的差的绝对值最大为 k。 16 | * 17 | * 示例 1: 18 | * 19 | * 输入: nums = [1,2,3,1], k = 3 20 | * 输出: true 21 | * 22 | * 示例 2: 23 | * 24 | * 输入: nums = [1,0,1,1], k = 1 25 | * 输出: true 26 | * 27 | * 示例 3: 28 | * 29 | * 输入: nums = [1,2,3,1,2,3], k = 2 30 | * 输出: false 31 | * 32 | */ 33 | /** 34 | * @param {number[]} nums 35 | * @param {number} k 36 | * @return {boolean} 37 | */ 38 | var containsNearbyDuplicate = function(nums, k) { 39 | const map = {} 40 | for (let [i, n] of nums.entries()) { 41 | if (map[n] !== void 0 && Math.abs(i - map[n]) <= k && map[n] !== i) { 42 | return true 43 | } else { 44 | map[n] = i 45 | } 46 | } 47 | return false 48 | } 49 | -------------------------------------------------------------------------------- /easy/405.数字转换为十六进制数/405.数字转换为十六进制数.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=405 lang=python3 3 | # 4 | # [405] 数字转换为十六进制数 5 | # 6 | # https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/ 7 | # 8 | # algorithms 9 | # Easy (43.77%) 10 | # Total Accepted: 2.3K 11 | # Total Submissions: 5.3K 12 | # Testcase Example: '26' 13 | # 14 | # 给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。 15 | # 16 | # 注意: 17 | # 18 | # 19 | # 十六进制中所有字母(a-f)都必须是小写。 20 | # 十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。  21 | # 给定的数确保在32位有符号整数范围内。 22 | # 不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。 23 | # 24 | # 25 | # 示例 1: 26 | # 27 | # 28 | # 输入: 29 | # 26 30 | # 31 | # 输出: 32 | # "1a" 33 | # 34 | # 35 | # 示例 2: 36 | # 37 | # 38 | # 输入: 39 | # -1 40 | # 41 | # 输出: 42 | # "ffffffff" 43 | # 44 | # 45 | # 46 | 47 | 48 | class Solution: 49 | def toHex(self, num: int) -> str: 50 | if not num: 51 | return '0' 52 | s = '0123456789abcdef' 53 | ans = '' 54 | while num and len(ans) < 8: 55 | ans = s[num & 0xf] + ans 56 | num >>= 4 57 | return ans 58 | -------------------------------------------------------------------------------- /easy/448.找到所有数组中消失的数字/448.找到所有数组中消失的数字.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=448 lang=golang 3 | * 4 | * [448] 找到所有数组中消失的数字 5 | * 6 | * https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/ 7 | * 8 | * algorithms 9 | * Easy (46.40%) 10 | * Total Accepted: 5.6K 11 | * Total Submissions: 12.1K 12 | * Testcase Example: '[4,3,2,7,8,2,3,1]' 13 | * 14 | * 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 15 | * 16 | * 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 17 | * 18 | * 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 19 | * 20 | * 示例: 21 | * 22 | * 23 | * 输入: 24 | * [4,3,2,7,8,2,3,1] 25 | * 26 | * 输出: 27 | * [5,6] 28 | * 29 | * 30 | */ 31 | 32 | func findDisappearedNumbers(nums []int) []int { 33 | ans := []int{} 34 | for _, n := range nums { 35 | if n >= 0 { 36 | if nums[n-1] > 0 { 37 | nums[n-1] = -nums[n-1] 38 | } 39 | } else { 40 | if nums[-n-1] > 0 { 41 | nums[-n-1] = -nums[-n-1] 42 | } 43 | } 44 | } 45 | for i, n := range nums { 46 | if n > 0 { 47 | ans = append(ans, i+1) 48 | } 49 | } 50 | return ans 51 | } 52 | 53 | -------------------------------------------------------------------------------- /easy/66.加一/66.加一.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=66 lang=python3 3 | # 4 | # [66] 加一 5 | # 6 | # https://leetcode-cn.com/problems/plus-one/description/ 7 | # 8 | # algorithms 9 | # Easy (37.33%) 10 | # Total Accepted: 35.9K 11 | # Total Submissions: 96.2K 12 | # Testcase Example: '[1,2,3]' 13 | # 14 | # 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 15 | # 16 | # 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 17 | # 18 | # 你可以假设除了整数 0 之外,这个整数不会以零开头。 19 | # 20 | # 示例 1: 21 | # 22 | # 输入: [1,2,3] 23 | # 输出: [1,2,4] 24 | # 解释: 输入数组表示数字 123。 25 | # 26 | # 27 | # 示例 2: 28 | # 29 | # 输入: [4,3,2,1] 30 | # 输出: [4,3,2,2] 31 | # 解释: 输入数组表示数字 4321。 32 | # 33 | # 34 | # 35 | class Solution: 36 | def plusOne(self, digits: 'List[int]') -> 'List[int]': 37 | flag = True 38 | l = len(digits) - 1 39 | while l >= 0: 40 | if flag: 41 | if digits[l] + 1 < 10: 42 | flag = False 43 | digits[l] = (digits[l] + 1) % 10 44 | else: 45 | return digits 46 | l -= 1 47 | if flag: 48 | digits.insert(0, 1) 49 | return digits 50 | -------------------------------------------------------------------------------- /medium/47.全排列-ii/47.全排列-ii.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=47 lang=javascript 3 | * 4 | * [47] 全排列 II 5 | * 6 | * https://leetcode-cn.com/problems/permutations-ii/description/ 7 | * 8 | * algorithms 9 | * Medium (51.10%) 10 | * Total Accepted: 8.8K 11 | * Total Submissions: 17.2K 12 | * Testcase Example: '[1,1,2]' 13 | * 14 | * 给定一个可包含重复数字的序列,返回所有不重复的全排列。 15 | * 16 | * 示例: 17 | * 18 | * 输入: [1,1,2] 19 | * 输出: 20 | * [ 21 | * ⁠ [1,1,2], 22 | * ⁠ [1,2,1], 23 | * ⁠ [2,1,1] 24 | * ] 25 | * 26 | */ 27 | /** 28 | * @param {number[]} nums 29 | * @return {number[][]} 30 | */ 31 | var permuteUnique = function(nums) { 32 | nums = nums.sort((a, b) => a - b) 33 | const ans = [] 34 | perm(nums, 0, nums.length - 1, ans) 35 | return ans 36 | } 37 | 38 | function perm(nums, left, right, ans) { 39 | if (left === right) { 40 | ans.push(nums) 41 | } else { 42 | for (let i = left; i <= right; i++) { 43 | if (i !== left && nums[left] === nums[i]) continue 44 | ;[nums[left], nums[i]] = [nums[i], nums[left]] 45 | perm([...nums], left + 1, right, ans) 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /easy/176.第二高的薪水/176.第二高的薪水.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- @lc app=leetcode.cn id=176 lang=mysql 3 | -- 4 | -- [176] 第二高的薪水 5 | -- 6 | -- https://leetcode-cn.com/problems/second-highest-salary/description/ 7 | -- 8 | -- database 9 | -- Easy (27.97%) 10 | -- Total Accepted: 19.8K 11 | -- Total Submissions: 70.6K 12 | -- Testcase Example: '{"headers": {"Employee": ["Id", "Salary"]}, "rows": {"Employee": [[1, 100], [2, 200], [3, 300]]}}' 13 | -- 14 | -- 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。 15 | -- 16 | -- +----+--------+ 17 | -- | Id | Salary | 18 | -- +----+--------+ 19 | -- | 1 | 100 | 20 | -- | 2 | 200 | 21 | -- | 3 | 300 | 22 | -- +----+--------+ 23 | -- 24 | -- 25 | -- 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。 26 | -- 27 | -- +---------------------+ 28 | -- | SecondHighestSalary | 29 | -- +---------------------+ 30 | -- | 200 | 31 | -- +---------------------+ 32 | -- 33 | -- 34 | -- 35 | # Write your MySQL query statement below 36 | select ifnull( 37 | (select distinct Salary from Employee 38 | order by Salary desc 39 | limit 1 offset 1), null 40 | ) as SecondHighestSalary 41 | 42 | -------------------------------------------------------------------------------- /medium/47.全排列-ii/47.全排列-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode.cn id=47 lang=python3 3 | # 4 | # [47] 全排列 II 5 | # 6 | # https://leetcode-cn.com/problems/permutations-ii/description/ 7 | # 8 | # algorithms 9 | # Medium (51.19%) 10 | # Total Accepted: 9.4K 11 | # Total Submissions: 18.4K 12 | # Testcase Example: '[1,1,2]' 13 | # 14 | # 给定一个可包含重复数字的序列,返回所有不重复的全排列。 15 | # 16 | # 示例: 17 | # 18 | # 输入: [1,1,2] 19 | # 输出: 20 | # [ 21 | # ⁠ [1,1,2], 22 | # ⁠ [1,2,1], 23 | # ⁠ [2,1,1] 24 | # ] 25 | # 26 | # 27 | class Solution: 28 | def permuteUnique(self, nums: List[int]) -> List[List[int]]: 29 | nums = sorted(nums) 30 | ans = [] 31 | self.perm(nums, 0, len(nums) - 1, ans) 32 | return ans 33 | 34 | def perm(self, nums, left, right, ans): 35 | if left == right: 36 | ans.append(nums) 37 | else: 38 | for i in range(left, right + 1): 39 | if i != left and nums[left] == nums[i]: 40 | continue 41 | nums[left], nums[i] = nums[i], nums[left] 42 | self.perm(nums[:], left + 1, right, ans) 43 | 44 | -------------------------------------------------------------------------------- /easy/400.第n个数字/400.第n个数字.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=400 lang=javascript 3 | * 4 | * [400] 第N个数字 5 | * 6 | * https://leetcode-cn.com/problems/nth-digit/description/ 7 | * 8 | * algorithms 9 | * Easy (30.10%) 10 | * Total Accepted: 2.1K 11 | * Total Submissions: 7K 12 | * Testcase Example: '3' 13 | * 14 | * 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。 15 | * 16 | * 注意: 17 | * n 是正数且在32为整形范围内 ( n < 231)。 18 | * 19 | * 示例 1: 20 | * 21 | * 22 | * 输入: 23 | * 3 24 | * 25 | * 输出: 26 | * 3 27 | * 28 | * 29 | * 示例 2: 30 | * 31 | * 32 | * 输入: 33 | * 11 34 | * 35 | * 输出: 36 | * 0 37 | * 38 | * 说明: 39 | * 第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。 40 | * 41 | * 42 | */ 43 | /** 44 | * @param {number} n 45 | * @return {number} 46 | */ 47 | var findNthDigit = function(n) { 48 | let digit = 1 49 | while (true) { 50 | let first = 10 ** (digit - 1) 51 | let cnt = 9 * first * digit 52 | if (cnt >= n) { 53 | return Number(((first + (n - 1) / digit) | 0).toString()[(n - 1) % digit]) 54 | } 55 | n -= cnt 56 | digit++ 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /medium/144.二叉树的前序遍历/144.二叉树的前序遍历.go: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode.cn id=144 lang=golang 3 | * 4 | * [144] 二叉树的前序遍历 5 | * 6 | * https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/ 7 | * 8 | * algorithms 9 | * Medium (59.91%) 10 | * Likes: 116 11 | * Dislikes: 0 12 | * Total Accepted: 24.9K 13 | * Total Submissions: 41.5K 14 | * Testcase Example: '[1,null,2,3]' 15 | * 16 | * 给定一个二叉树,返回它的 前序 遍历。 17 | * 18 | * 示例: 19 | * 20 | * 输入: [1,null,2,3] 21 | * ⁠ 1 22 | * ⁠ \ 23 | * ⁠ 2 24 | * ⁠ / 25 | * ⁠ 3 26 | * 27 | * 输出: [1,2,3] 28 | * 29 | * 30 | * 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 31 | * 32 | */ 33 | /** 34 | * Definition for a binary tree node. 35 | * type TreeNode struct { 36 | * Val int 37 | * Left *TreeNode 38 | * Right *TreeNode 39 | * } 40 | */ 41 | func preorderTraversal(root *TreeNode) []int { 42 | var res []int 43 | traversal(root, &res) 44 | return res 45 | } 46 | 47 | func traversal(root *TreeNode, res *[]int) { 48 | if root != nil { 49 | *res = append(*res, root.Val) 50 | traversal(root.Left, res) 51 | traversal(root.Right, res) 52 | } 53 | } 54 | --------------------------------------------------------------------------------