python
**华
作为一个资深技术打杂的主要任务是,善于从无聊和平庸中,发现乐趣和卓越。把看似无理的脑洞,用代码落实成实实在在的应用。
展开
-
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 · 238 阅读 · 1 评论 -
算法题:对相交的圆进行分组(python)
# -*- coding: utf-8 -*-# 相交的圆'''问题2:假定二维空间平面内有n个圆,现在我们需要将存在`交集`的圆进行分组,输出结果为n个组,每个组包含i个圆. 交集算法: 两圆心的距离 小于 两圆半径之和. 假设圆数据为: (用数组表示圆) [ { X: 0, Y: 0, Radius: 1 }, { X: 3, Y: 2, Radius: ...原创 2018-10-10 23:04:19 · 1078 阅读 · 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 · 84 阅读 · 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 · 89 阅读 · 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 · 239 阅读 · 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 · 259 阅读 · 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 · 1259 阅读 · 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 · 220 阅读 · 0 评论 -
[python]使用bits表示weekdays
在做表设计的时候,可能会遇到需要表示Weekdays的场景: {id:0, text:"Sun"}, {id:1, text:"Mon"}, {id:2, text:"Tue"}, {id:3, text:"Wed"}, {id:4, text:"Thu"}, {id:5, text:"Fri"}, ...原创 2019-06-17 16:24:59 · 444 阅读 · 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 · 944 阅读 · 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 · 283 阅读 · 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 · 198 阅读 · 0 评论 -
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 · 226 阅读 · 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 · 222 阅读 · 0 评论 -
Leetcode 61. 旋转链表 Python
我的版本:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def rotateRight(self,...原创 2018-09-20 15:13:33 · 408 阅读 · 0 评论 -
Python转换字典成为对象,可以用"."方式访问对象属性
database = [ { "name": "18D_Block", "xcc":{ "component": { "core":[], "platform":[] }, }, "uef原创 2018-09-19 14:31:11 · 9208 阅读 · 0 评论 -
sublime text 3-Virtualenv-Anaconda配
[projectname].sublime-project:{ "settings": { "python_interpreter": "~/workspace/env/bin/python", "test_command": "./manage.py test --settings原创 2015-08-12 22:35:28 · 2060 阅读 · 0 评论 -
python闭包实现,匿名函数的实现
#!/usr/bin/env python#coding=utf-8# python实现闭包def wrapper(counter={'cnt':0}): def _closure(): counter.update({ 'cnt':counter.get('cnt') + 1 }) return counter['cnt'] return _closure# 匿名原创 2015-09-01 14:20:04 · 385 阅读 · 0 评论 -
在一个list列表中的多个dict字典按照键值对key-value来进行排序
# -*- coding: utf-8 -*-#主题:在一个list列表中的多个dict字典按照键值对key-value来进行排序items = [ {'name':'李四','age':40}, {'name':'张三','age':30}, {'name':'王五','age':50},]items0 = items[:]# list.sort()或者sort原创 2015-09-02 23:15:03 · 7962 阅读 · 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 · 904 阅读 · 0 评论 -
测试python装饰器decorator
# coding=utf-8# 主题:测试python装饰器的一些特性def outter(f): print 'it is outter.' def wrapper(): return f return wrapper()def end(f): print 'it is end' print f('call it') def原创 2015-09-13 11:43:25 · 698 阅读 · 0 评论 -
python笔试题之找出一个列表里出现频次最高的元素(most common elements in a list)
def most_common(seq): d = {} for i in seq: d[i] = d.get(i, 0) + 1 ret = [] for j in sorted(d.items(), reverse=True, key=lambda x:x[1]): if len(ret) == 0: ret原创 2016-09-24 18:55:20 · 6520 阅读 · 0 评论 -
Leetcode 80 删除排序数组中的重复项 II Python
class Solution: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ rm=0 dup=0 L = len(nums) lo = 0 ...原创 2018-09-17 13:44:07 · 393 阅读 · 0 评论