python
Neekity
刷出一片天
展开
-
python leetcode 400. Nth Digit
按着题目意思编程即可class Solution(object): def findNthDigit(self, n): """ :type n: int :rtype: int """ count=1 res=0 while (res+count*9*10**(count-1...原创 2018-11-29 08:55:02 · 608 阅读 · 0 评论 -
python leetcode 402. Remove K Digits
dfs或者用栈比大小 估计用dfs会超时 最后注意要去除字符串开头的0class Solution(object): def removeKdigits(self, num, k): """ :type num: str :type k: int :rtype: str """ stack=[...原创 2018-11-29 09:31:45 · 615 阅读 · 0 评论 -
python leetcode 403. Frog Jump
与一般DP不同的是 青蛙能跳k-1,k,k+1步三种不同的情况 所以用字典存储跳到这个石头上所用的步数class Solution(object): def canCross(self, stones): """ :type stones: List[int] :rtype: bool """ #maxJum...原创 2018-11-29 10:46:43 · 924 阅读 · 0 评论 -
python leetcode 363. Max Sum of Rectangle No Larger Than K
python leetcode 363. Max Sum of Rectangle No Larger Than K自己码了代码TLE 然后研究了别人的代码成功地将时间复杂度由O(mn)^2降到了nnm*log(m)这里m是行长列长中大的那个假如是列较长 那就扫描列 利用bisect进行二分查找,插入(slist)sums是行叠加后的大小(x到y行 第z列的和) num存储的是x到y行 ...原创 2018-11-26 14:06:10 · 687 阅读 · 0 评论 -
python leetcode 440. K-th Smallest in Lexicographical Order
如果按照Lexicographical Numbers这道题进行遍历取值显然会超时(n最大可能为10^9 也就是说必须要用30位bit存 30*10^9 数据量是非常大的)先来看看n=100 1,10,100,11,12,13,14,15,16,17,18,19,2… 1下面对应10,11,…,19, 10下面对100这样就对应于一个10叉树 类似先序遍历顺序即为序号cur先为1 要快速定...原创 2018-12-02 11:43:28 · 843 阅读 · 0 评论 -
python leetcode 404. Sum of Left Leaves
二叉树就用递归 关键是如何判断左叶class Solution: def sumOfLeftLeaves(self, root): """ :type root: TreeNode :rtype: int """ res=[0] def help(now,parent): ...原创 2018-11-29 13:01:18 · 505 阅读 · 0 评论 -
python leetcode 405. Convert a Number to Hexadecimal
涉及到进制和加减乘除 很多时候考察的是位运算注意的是 要先把负数转为正数class Solution: def toHex(self, num): """ :type num: int :rtype: str """ sh='0123456789abcdef' res='' ...原创 2018-11-29 13:34:01 · 554 阅读 · 0 评论 -
python leetcode 442. Find All Duplicates in an Array
一开始的想法是用异或来做 但看到了Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array)这个条件意味着我们可以在原来的数组上操作设置标志位(因为所给的整数不会超出数组长度即最坏的情况都能一一对应)class Solution: def findDuplicates(self, nums): """ ...原创 2018-12-02 12:29:40 · 519 阅读 · 0 评论 -
python leetcode 406. Queue Reconstruction by Height
要尽量保证大的h在后面 所以进行如下people = sorted(people, key=lambda people:(-people[0],people[1])) h降序 k降序再遍历进行插入操作,插入位置为people[i][1]能保证符合题意 举例说明people=[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]people = sorted...原创 2018-11-29 13:48:52 · 625 阅读 · 0 评论 -
python leetcode 42. Trapping Rain Water
首先最左边和最右边肯定无法蓄水考虑能蓄水的情况:左右两边的高度都大于height[i]所以得左右遍历找到heigh[i]时的左右最大值再遍历height 当min(maxL[i],maxR[i])>height[i]即能蓄水class Solution: def trap(self, height): """ :type height: Li...原创 2018-11-29 14:11:02 · 837 阅读 · 0 评论 -
python leectcode 368. Largest Divisible Subset
动态规划求最大长度 开辟一个数组保存父节点class Solution: def largestDivisibleSubset(self, nums): """ :type nums: List[int] :rtype: List[int] """ if nums==[]:原创 2018-11-26 15:41:20 · 257 阅读 · 0 评论 -
python LeetCode Find K Pairs with Smallest Sums
居然没超时应该是要用堆来做class Solution: def kSmallestPairs(self, nums1, nums2, k): """ :type nums1: List[int] :type nums2: List[int] :type k: int :rtype: List[List[i...原创 2018-11-26 16:18:12 · 489 阅读 · 0 评论 -
python leetcde 445. Add Two Numbers II
利用Python的特性 整数相加无上界class Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ str1='' ...原创 2018-12-02 13:57:23 · 554 阅读 · 0 评论 -
python leetcode Trapping Rain Water II
刚开始以为和Trapping Rain Water做法一样 就是设置四个方向的最大值 但出错了 错误原因:一维的话蓄水只能往左右扩展,但二维可以往四周扩展(可能是第一次是往右扩展第二次往下了第三次又往右了 即无法保持在同一纬度上扩展)所以无法简单地求出当前的最大值 见代码二用BFS 先从最边上开始,利用queue进行更新if peakMap[nx][ny] > limit: #当[nx,...原创 2018-11-29 15:38:55 · 518 阅读 · 0 评论 -
python leetcode 446. Arithmetic Slices II - Subsequence
由于求的是有多少种 很容易想到用DP做 难点在于如何设置及更新DP假设dp[i][j]是在A[:i+1]差值为j的所有长度为2的数组数量譬如[2,4,6,8]i=3时 dp[:3]=[{},{2: 1},{4: 1, 2: 2}]j=2时 d=A[i]-A[j]=2 d在dp[j]中那么更新dp[i][d]+=dp[j][d] (2,([2,4],[4,6]))=3 ([2,4],[4,...原创 2018-12-02 14:48:49 · 566 阅读 · 0 评论 -
python leetcode Guess Number Higher or Lower II
动态规划+记忆化先举例以更好地理解[start,end]2 无需判断2,3,4 或者2,3 只需判断一次3即可(end-1)2,3,4,5 或者2,3,4,5,6 前者要2,4(end-3,end-1) 后者3,5(end-3,end-1)2,3,4,5,6,7 或者2,3,4,5,6,7,8 前者判断max((4,6),(2,4)) 后者max((3,5),(5,7))此时失去一般...原创 2018-11-26 18:32:59 · 452 阅读 · 0 评论 -
python leetcode 410. Split Array Largest Sum
代码一:DP难点在于如何设置dp数组dp[i][j] 前j个数字分成i组所能得到的最小的各个子数组中最大值代码二:二分查找变种class Solution(object): def splitArray(self, nums, m): """ :type nums: List[int] :type m: int :rt...原创 2018-11-29 19:34:38 · 656 阅读 · 0 评论 -
python leetcode 413. Arithmetic Slices
class Solution(object): def numberOfArithmeticSlices(self, A): """ :type A: List[int] :rtype: int """ res=0 if len(A)<3:return 0 pre=A[1]...原创 2018-11-29 20:00:48 · 488 阅读 · 0 评论 -
376. Wiggle Subsequence
p为上升的个数 q为下降的个数如果现在的数大于之前的数那么就是升序 摇摆序列长度为q+1 反之亦然class Solution: def wiggleMaxLength(self, nums): """ :type nums: List[int] :rtype: int """ if not n原创 2018-11-26 20:33:16 · 524 阅读 · 0 评论 -
python LeetCode 377. Combination Sum IV
求多少种一般是动态规划 要具体写出各种的情况一般是dfs本题类似于换硬币class Solution: def combinationSum4(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int ""&q原创 2018-11-26 20:47:36 · 555 阅读 · 0 评论 -
python leetcode 447. Number of Boomerangs
直接循环遍历O(n^3) 想办法转为O(n^2)根据点到点间的距离相同 可以建立一个字典{两点间距离:出现次数}两层循环第一层找i,第二层找j,kclass Solution: def numberOfBoomerangs(self, points): """ :type points: List[List[int]] :rtype...原创 2018-12-02 15:52:31 · 494 阅读 · 0 评论 -
python leetcode 448. Find All Numbers Disappeared in an Array
考察对数组的操作 与Add Two Numbers II 有异曲同工之妙class Solution: def findDisappearedNumbers(self, nums): &quot;&quot;&quot; :type nums: List[int] :rtype: List[int] &quot;&quot;&q原创 2018-12-02 15:55:22 · 558 阅读 · 1 评论 -
python leetcode 467. Unique Substrings in Wraparound String
看到有多少种应当想象是否用DP做,文章提示了开辟一个长度为26的数组存储的是以字母为结尾的字串,例如以b结尾,那么如果有b,ab,zab,yzab即dp[ord(‘b’)-97]=4class Solution: def findSubstringInWraproundString(self, p): """ :type p: str :...原创 2018-12-04 14:55:39 · 541 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix
利用help函数找到mid位于矩阵中的位置 loc如果loc小于k low=mid+1 反之high=mid-1注意help函数里求得是&lt;=num的loc 所以在主函数中如果loc=k 那么一定是包括在小的那部分 所以high=mid-1例如 matrix = [[11, 13],[13, 15]]k=3 结果应该是13mid=(11+15)//2=13 那么help函数求...原创 2018-11-26 21:22:18 · 548 阅读 · 0 评论 -
python leetcode 416. Partition Equal Subset Sum
每个数都可以取或不取,用字典存储已经尝试过的和class Solution: def canPartition(self, nums): """ :type nums: List[int] :rtype: bool """ sumn=sum(nums) if sumn&1==1:ret...原创 2018-11-30 10:52:19 · 541 阅读 · 0 评论 -
python leetcode 417. Pacific Atlantic Water Flow
考察DFS, 难点在于要分别判断是否能到达两大洋及如何设置搜索起始位置class Solution: def pacificAtlantic(self, matrix): """ :type matrix: List[List[int]] :rtype: List[List[int]] """ m=原创 2018-11-30 11:46:14 · 718 阅读 · 0 评论 -
python leetcode 419. Battleships in a Board
无难度 按照题意解题即可按照遍历顺序只需判断左边和上边是否相连class Solution: def countBattleships(self, board): """ :type board: List[List[str]] :rtype: int """ m=len(board) if...原创 2018-11-30 12:10:53 · 541 阅读 · 0 评论 -
python leetcode 450. Delete Node in a BST
用递归做 代码看起来简洁点以下是用运行时间的增加换取了代码的整洁如果key的右节点存在那么key的值和右节点的最最最最…左节点的值交换 继续递归直到再一次找到key值也就是说会操作两次 但还是能满足了O(h)的运行时间class Solution: def deleteNode(self, root, key): """ :type root: Tr...原创 2018-12-02 18:20:31 · 582 阅读 · 0 评论 -
380. Insert Delete GetRandom O(1)
O(1)的复杂度 很显然要用字典随机取值 利用randomclass RandomizedSet(object): import random def __init__(self): """ Initialize your data structure here. """ self.dict1={}原创 2018-11-27 14:23:26 · 449 阅读 · 0 评论 -
python leetcode 2. Add Two Numbers
感谢python的整数相加无上界class Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ link1 = '' ...原创 2018-12-04 21:15:34 · 482 阅读 · 0 评论 -
python leetcode 3. Longest Substring Without Repeating Characters
子串无重复,遍历字符串。设立一个judge子串来判断:如果judge中含有当前字符,更新最大值,更新juge否则judge加上当前字符class Solution: def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ ...原创 2018-12-04 21:22:02 · 444 阅读 · 0 评论 -
python leetcode 4. Median of Two Sorted Arrays
并不仅限于找到中位数** 这里的重点是getKth算法 在两个排序好的数组中找到第k小的数 保证A的数组长度小于等于B的长度 先二分,即A中取k/2个数,B中取k/2个数 如果A[k/2-1] < B[k/2-1] 是不是意味着第k个数是在B中,已经判断过了A中前k/2个数所以getKth(A[k/2:],B,k/2) 这样还有一个问题就是如果A的长度小于k/2 所以要做个比较**cla...原创 2018-12-04 21:47:32 · 457 阅读 · 0 评论 -
python leetcode 5. Longest Palindromic Substring
代码一:Manacger算法class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ def manacherString(s): res='#' for...原创 2018-12-04 21:52:48 · 443 阅读 · 0 评论 -
python leetcode 6. ZigZag Conversion
对字符串进行操作有几行就先初始化几个tmp=’’然后遍历字符串 找到每个字符应该存到第几行顺序是从第一行到最后一行再到第一行到最后一行…如此反复class Solution: def convert(self, s, numRows): """ :type s: str :type numRows: int :rtyp...原创 2018-12-04 21:58:21 · 511 阅读 · 0 评论 -
python leetcode 8. String to Integer (atoi)
题目并不难 但是你必须考虑到所有的异常情况class Solution: def myAtoi(self, str): """ :type str: str :rtype: int """ if str=="": return 0 f = 0 for ...原创 2018-12-04 22:00:54 · 492 阅读 · 0 评论 -
python leetcode 451. Sort Characters By Frequency
字典存储 字典排序 遍历排序后的listclass Solution: def frequencySort(self, s): """ :type s: str :rtype: str """ dict1={} for c in s: if c in dict1:...原创 2018-12-02 19:22:13 · 590 阅读 · 0 评论 -
python leetcode 452. Minimum Number of Arrows to Burst Balloons
能重叠的区域能一起射爆所以先排序再取重合的区域看一共有几个这样的区域class Solution: def findMinArrowShots(self, points): """ :type points: List[List[int]] :rtype: int """ if not points: ...原创 2018-12-02 19:40:57 · 636 阅读 · 0 评论 -
python leetcode 453. Minimum Moves to Equal Array Elements
理会了题目的意思就是一行代码的事情假设最终数组中全为某一个数f,一共加了n次,数组的长度为l,数组中最小的数字为m,数组和为sf*l=s+(l-1)*n f最小的情况是什么 一定是数组中的最小值加了n 即f=n+m代入得 nl+ml=s+ln-n n=s-mlclass Solution: def minMoves(self, nums): ""...原创 2018-12-02 20:00:21 · 609 阅读 · 0 评论 -
python leetcode 454. 4Sum II
这类题目基本是用字典 以空间换时间class Solution(object): def fourSumCount(self, A, B, C, D): &quot;&quot;&quot; :type A: List[int] :type B: List[int] :type C: List[int] :type D: List[int...原创 2018-12-03 09:18:16 · 561 阅读 · 0 评论 -
python leetcode 421. Maximum XOR of Two Numbers in an Array
通过mask来判断最终结果在哪些bit上能取到1 核心公式a^b=c <---->a^c=b以下是自己的理解nums=[3, 10, 5, 25, 2, 8,26] 这里因为最大数才25所以bit直接从4开始3 ‘00011’10 ‘01010’5 ‘00101’25 ‘11001’2 ‘00010’8 ‘01000’26 ‘11010’bit=4mas...原创 2018-11-30 15:53:00 · 472 阅读 · 0 评论