刷题
vicky428
这个作者很懒,什么都没留下…
展开
-
lintcode 617 Maximum Average Subarray
617 Maximum Average Subarray 长度大于等于k的连续元素的最大平均值,比leet643再多加一个局部最大ll, l变化起点, ll变化终点。 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Feb 14 15:41:08 2018@author: vick...原创 2019-07-14 12:34:35 · 166 阅读 · 0 评论 -
leetcode 1 Two Sum
1 Two Sum 37.00% 查找数组中相加等于给定数字的两个元素 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sun Dec 24 13:56:44 2017@author: vicky"""#1. Two Sum class Solution: def tw...原创 2018-03-04 22:36:34 · 145 阅读 · 0 评论 -
lintcode 3 Digit Counts
3 Digit Counts 统计前n个元素中包含k的个数,k<9,n可以大于10,直接用str.join(list)融合数组, str.count(str(k))统计个数 join(): ''.join(), 无间隔的融合list为string count(): str.count(k), 计算string中k的个数 用[]构造list: [str(i) ...原创 2018-03-04 22:37:57 · 196 阅读 · 0 评论 -
leetcode 7 Reverse Integer
7 Reverse Integer 24.40% 反转数字,注意首位为0 32bit:数字大小小于2^31 string反转方法:[::-1] #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sun Dec 24 20:13:51 2017@author: vicky...原创 2018-03-04 22:39:04 · 89 阅读 · 0 评论 -
leetcode 9 Palindrome Number
9 Palindrome Number 35.70% 检查是不是回文数字,先反转之后判断再判断是否相等 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Jan 3 18:52:55 2018@author: vicky"""class Solution: ...原创 2018-03-04 22:40:20 · 83 阅读 · 0 评论 -
leetcode 13 Roman to Integer
13 Roman to Integer 47.40% 罗马数字转换为拉丁数字,用字典 字典dict{keys:valuse,…,},d.keys(),d.values() #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Jan 3 21:49:18 2018@a...原创 2018-03-04 22:41:25 · 101 阅读 · 0 评论 -
leetcode 21 Merge Two Sorted Lists
21 Merge Two Sorted Lists 40.10% 结合两个listnode,按照从小到大依次对比,先接小的数字 __init__()调用不用写函数名直接写类名; while x: =while x is not None: =while x!=None: while not x: =while x is None: =while x...原创 2018-03-04 22:45:26 · 155 阅读 · 0 评论 -
leetcode 26 Remove Duplicates from Sorted Array
26 Remove Duplicates from Sorted Array 35.90% 删除数组中的重复数字只留下一个,返回非重复的元素个数j,数组本身的前j个元素改变 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Jan 18 22:22:48 2018@au...原创 2018-03-04 22:46:17 · 101 阅读 · 0 评论 -
leetcode 27 Remove Element
27 Remove Element 40.30% 删除数组中的指定数字,返回非重复的元素个数j,数组本身的前j个元素改变 while True: 如果出现错误的话,可以继续循环。语句中一定要有break,否则会一直循环下去。 try:except:语句 (Python 异常处理): try: 正常...原创 2018-03-04 22:47:31 · 95 阅读 · 0 评论 -
leetcode 28 Implement strStr()
28 Implement strStr() 28.80% 查找是否有子串 python的elif = c++里面的else if python里是if..elif..else, c++里是if.. else if… else. Index(): 返回b在a中的位置:a.index(b),找不到返回ValueError异常 #!/usr/bin/...原创 2018-03-04 22:48:25 · 131 阅读 · 0 评论 -
leetcode 35 Search Insert Position
35 Search Insert Position 40.00% 查找目标数字在数组中的排序,输出排序位置,先加再排序再查找 注意list是可变变量,用list.append()合并和list.sort()排序时,list自身已经改变了 用[]构造list:[x for x in nums if x<target]:返回x中小于target的所有元素...原创 2018-03-04 22:49:10 · 120 阅读 · 0 评论 -
leetcode 38 Count and Say
38 Count and Say 36.20% 外观序列,后一个数字等于前一个数字的元素重复个数+元素本身 正则表达式pattern re模块函数: re.sub()替换,re.match()匹配,re.search()查找,re.group()分组, re.split()切分 lambda(): 创建匿名函数,返回一个lambda对象。lam...原创 2018-03-04 22:49:55 · 154 阅读 · 0 评论 -
leedcode 67 Add Binary
67 Add Binary 33.70% 2进制下的求和 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Jan 31 21:13:07 2018@author: vicky"""class Solution: def addBinary(self, a...原创 2018-03-28 11:23:52 · 145 阅读 · 0 评论 -
leetcode 66 Plus One
66 Plus One 39.40% list数组加1,如果当前元素为9,则加1后当前位置变为0,往前一个位置加1 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sat Jan 27 19:47:54 2018@author: vicky"""class Solutio...原创 2018-03-28 11:23:02 · 127 阅读 · 0 评论 -
leetcode 686 Repeated String Match
686 Repeated String Match 33.90% string b要重复多少次才包含string a,循环最多重复b/a+1次(li718) #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Mon Feb 12 21:45:08 2018@author: vi...原创 2018-03-28 11:28:08 · 162 阅读 · 0 评论 -
leetcode 643 Maximum Average Subarray I
643 Maximum Average Subarray I 37.70% 长度等于k的连续元素的最大平均值,动态规划,一个局部最大和l,一个全局最大和g,类似53题 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Mon Feb 12 23:46:54 2018@autho...原创 2018-03-28 11:27:30 · 107 阅读 · 0 评论 -
leetcode 374 Guess Number Higher or Lower
374 Guess Number Higher or Lower 36.30% 比较两个数的高低,二分法,lint662 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Fri Mar 2 14:51:41 2018@author: vicky"""# The gue...原创 2018-03-28 11:26:48 · 137 阅读 · 0 评论 -
leedcode 83 Remove Duplicates from Sorted List
83 Remove Duplicates from Sorted List 40.20% 删去listnode中的重复元素,a.next=a.next.next(单链表的删除) #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Feb 8 17:59:50 2018@...原创 2018-03-28 11:25:53 · 102 阅读 · 0 评论 -
leedcode 70 Climbing Stairs
70 Climbing Stairs 40.90% 爬楼梯,斐波那契数列 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Feb 7 22:43:23 2018@author: vicky"""#思路:斐波那契数列问题:# n<=2,此时只有一种或...原创 2018-03-28 11:25:09 · 105 阅读 · 0 评论 -
leedcode 69 Sqrt(x)
69 Sqrt(x) 28.60% 开方,用二分法缩小尝试范围 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Feb 7 16:06:06 2018@author: vicky"""##import math##class Solution:# d...原创 2018-03-28 11:24:30 · 106 阅读 · 0 评论 -
leetcode 58 Length of Last Word
58 Length of Last Word 32.00% 最后一个连续子串的长度 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sat Jan 27 17:40:21 2018@author: vicky"""split():分割字符串,返回分割后的字符串列表。http://...原创 2018-03-28 11:22:19 · 101 阅读 · 0 评论 -
lintcode 30 Insert Interval
30 Insert Interval 合并区间,没交叉时加本身,有交叉时左比右,右比左 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Feb 8 23:58:16 2018@author: vicky"""#思路:首先要找到newInterval在已有数组中的位置。有三种可能:##...原创 2018-03-28 11:20:42 · 127 阅读 · 0 评论 -
leetcode 53 Maximum Subarray
53 Maximum Subarray 40.10% 子数组和的最大值 动态规划法Dynamic Programming:时间复杂度为o(n),试想从头遍历这个数组。对于数组中的其中一个元素,它只有两个选择: 1. 要么加入之前的数组加和之中(跟别人一组) 2. 要么自己单立一个数组(自己单开一组)。 ...原创 2018-03-04 22:50:37 · 99 阅读 · 0 评论 -
leetcode 20 Valid Parentheses
20 Valid Parentheses 33.90% 检查正反括号是否配对完整 python中in的用法:for char in s replace():str.replace(old, new[, max]) #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Mon J...原创 2018-03-04 22:43:36 · 114 阅读 · 0 评论 -
leetcode 14 Longest Common Prefix
14 Longest Common Prefix 31.60% list中各string的最长重复string前缀 string是“”,char是‘’ reduce():跟r里面的apply类似 #!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sat Jan 6 16:11:...原创 2018-03-04 22:42:23 · 79 阅读 · 0 评论 -
twobignumbersum.py-20180414
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Sat Apr 14 09:22:54 2018@author: vicky"""#两个大数相加#大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求。可以使用字符串来表示大数,模拟大数相加的过程。#思路:#1.对齐两个字...原创 2019-07-22 19:46:08 · 68 阅读 · 0 评论