![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
**华
作为一个资深技术打杂的主要任务是,善于从无聊和平庸中,发现乐趣和卓越。把看似无理的脑洞,用代码落实成实实在在的应用。
展开
-
Leetcode 5098. Tree Diameter
class Solution: def treeDiameter(self, edges: List[List[int]]) -> int: G=collections.defaultdict(list) for u,v in edges: G[u].append(v) G[v].append(u) ...原创 2019-11-03 10:24:50 · 246 阅读 · 1 评论 -
LCP3机器人大冒险
class Solution: def robot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: u,r=0,0 for m in command: if m=="U": u+=1 ...原创 2019-10-07 20:57:32 · 229 阅读 · 0 评论 -
Leetcode5091建造街区的最短时间
5091.建造街区的最短时间你是个城市规划工作者,手里负责管辖一系列的街区。在这个街区列表中blocks[i] = t意味着第 i个街区需要t个单位的时间来建造。由于一个街区只能由一个工人来完成建造。所以,一个工人要么需要再召唤一个工人(工人数增加 1);要么建造完一个街区后回家。这两个决定都需要花费一定的时间。一个工人再召唤一个工人所花费的时间由整数split给...原创 2019-09-22 08:34:07 · 471 阅读 · 0 评论 -
Leetcode636.函数的独占时间
# 2# ["0:start:0","1:start:2","1:end:5","0:end:6"]# 1# ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]# 2# ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]...原创 2019-09-16 14:23:29 · 206 阅读 · 0 评论 -
Leetcode 1191/5191 K 次串联后最大子数组之和
class Solution: def kConcatenationMaxSum(self, arr, k): """ :type arr: List[int] :type k: int :rtype: int """ res=0 most=0 n = len(...原创 2019-09-15 12:41:58 · 303 阅读 · 0 评论 -
Leetcode[1151]最少交换次数来组合所有的 1
"""1151. 最少交换次数来组合所有的 1 题目难度 Medium给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的 1 组合到一起,并返回所有可能中所需 最少的交换次数。示例 1:输入:[1,0,1,0,1]输出:1解释: 有三种可能的方法可以把所有的 1 组合在一起:[1,1,1,0,0],交换 1 次;[0,1,1,1,0],交换 2 次...原创 2019-08-11 09:45:28 · 965 阅读 · 0 评论 -
Leetcode 994. 腐烂的橘子
func orangesRotting(grid [][]int) int { /* 这题我学到了: 广度优先搜索的意义, 即矩阵中所有腐烂的橘子同时向四周扩张 */ R, C := len(grid), len(grid[0]) rotting := [][]int{} res := 0 for r := 0; r &l...原创 2019-06-03 17:57:55 · 424 阅读 · 0 评论 -
Leetcode 955. 删列造序 II
func minDeletionSize(A []string) int { skip := map[int]bool{} m, n := len(A[0]), len(A) D := 0 for j := 0; j < m; j++ { cur := 0 index := []int{}...原创 2019-05-17 23:00:42 · 169 阅读 · 0 评论 -
Leetcode 926. 将字符串翻转到单调递增
func minFlipsMonoIncr(S string) int { lo, hi := 0, len(S) - 1 zero, one := 0, 0 for _, c := range S { if c == '0' { zero++ }else{ one++ }...原创 2019-05-18 23:08:50 · 226 阅读 · 0 评论 -
Leetcode[849]到最近的人的最大距离
class Solution(object): def maxDistToClosest(self, seats): """ :type seats: List[int] :rtype: int """ N = len(seats) j, k = -1, -1 dp = [0]...原创 2019-03-28 12:56:42 · 224 阅读 · 0 评论 -
Leetcode[15]三数之和
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums = sorted(nums) N = len(nums) i...原创 2019-03-27 21:59:58 · 1264 阅读 · 0 评论 -
Leetcode 219. 存在重复元素 II (3种解法:哈希表、排序、滑动窗口)
class Solution(object): def containsNearbyDuplicate(self, nums, k): # 哈希表 d = {} for i in range(len(nums)): if nums[i] in d: if -k <= i - d[...原创 2019-03-31 17:14:23 · 261 阅读 · 0 评论 -
Leetcode[149]直线上最多的点数
class Solution: def maxPoints(self, points): dup = collections.Counter([(p.x, p.y) for p in points]) pt = dup.keys() N = len(pt) res = 0 for i in range(N):...原创 2019-03-31 16:51:58 · 245 阅读 · 0 评论 -
Leetcode[39/40/216] combinationSum 三部曲
# coding:utf-8class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] ...原创 2019-03-23 15:24:32 · 91 阅读 · 0 评论 -
Leetcode[442] 数组中重复的数据
'''442. 数组中重复的数据给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。找到所有出现两次的元素。你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?示例:输入:[4,3,2,7,8,2,3,1]输出:[2,3]'''def findDuplicates(nums): res...原创 2019-03-22 14:34:14 · 91 阅读 · 0 评论 -
Leetcode 915. 分割数组
即使自己做了几遍,还是没有真正的开窍,领会精髓。第一眼看上去,这是一道数组分割的题目。 给定一个数组 A,将其划分为两个不相交(没有公共元素)的连续子数组 left 和 right, 使得:left 中的每个元素都小于或等于 right 中的每个元素。 left 和 right 都是非空的。 left 要尽可能小思路一:那么是按索引位置先分割,然后用暴力法比较?很容易...原创 2019-01-09 12:39:20 · 701 阅读 · 0 评论 -
Python面试题 之 Uniquify a list 不使用set去除一个列表中的重复项,并且保持原有的排列顺序
def uniquify_a_list(ls): _ = lambda e, ls=[] : None if e in ls else ls.append(e) or ls return map(_, ls)[0] def uniquify_a_sorted_list(ls): # two pointers solution (learn from Leet...原创 2015-09-10 11:32:14 · 909 阅读 · 0 评论 -
LeetCode 794 Valid Tic-Tac-Toe State (Python)
class Solution(object): def validTicTacToe(self, board): """ :type board: List[str] :rtype: bool """ # count_x - count_o in [0,1] # only one reach...原创 2018-10-09 17:03:24 · 230 阅读 · 0 评论