自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(35)
  • 资源 (7)
  • 收藏
  • 关注

原创 python leetcode 424. Longest Repeating Character Replacement

知道题目考察的是什么非常重要 统计连续子串中最大重复字符(允许修改k次)应该能想到滑动窗口class Solution: def characterReplacement(self, s, k): """ :type s: str :type k: int :rtype: int """ st...

2018-11-30 16:51:03 613

原创 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 485

原创 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 568

原创 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 755

原创 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 563

原创 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 500

原创 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 695

原创 python leetcode Trapping Rain Water II

刚开始以为和Trapping Rain Water做法一样 就是设置四个方向的最大值 但出错了 错误原因:一维的话蓄水只能往左右扩展,但二维可以往四周扩展(可能是第一次是往右扩展第二次往下了第三次又往右了 即无法保持在同一纬度上扩展)所以无法简单地求出当前的最大值 见代码二用BFS 先从最边上开始,利用queue进行更新if peakMap[nx][ny] > limit: #当[nx,...

2018-11-29 15:38:55 553

原创 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 905

原创 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 667

原创 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 570

原创 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 526

原创 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 944

原创 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 632

原创 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 631

原创 python leetcode 398. Random Pick Index

奇怪的是蓄水池抽样算法无法AC 代码2是蓄水池抽样class Solution(object): import random def __init__(self, nums): """ :type nums: List[int] """ self.n=nums def pick(self, target...

2018-11-28 20:13:35 591

原创 python leetcode 397. Integer Replacement

关键是要找到奇数时n+1的情况:n+1是4的倍数例如23n-1的情况:23->22->11->10->5->4->2->1n-1的情况:23->24->12->6->3->2->1class Solution(object): def integerReplacement(self, n): ...

2018-11-28 19:29:45 557

原创 python leetcode 396. Rotate Function

两个循环O(N^2)超时了A = [4, 3, 2, 6] 长度n=4F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16F(2) = (0 * 2) + (1 * 6) + (2 ...

2018-11-28 16:49:47 575

原创 395. Longest Substring with At Least K Repeating Characters

以次数不满k的字符分割字符串 因为此类字符可能会有多个 所以递归调用自身注意为了避免重复判定分割字符 所以在循环中返回比如s = “abcabbdaababbcaabd”, k = 3 判定的是以下字符串abcabbaababbcaabbcbbaababbaabbclass Solution(object): def longestSubstring(self, s,...

2018-11-28 15:58:57 465

原创 python leetcode 394. Decode String

考察代码的编写能力 栈和字符串的操作注意的地方是数字可能是111这样的大于10的数字字母要考虑大小写左右括号要对应class Solution: def decodeString(self, s): """ :type s: str :rtype: str """ base = []

2018-11-28 15:39:29 646

原创 python leetcode 392. Is Subsequence

没啥难度 还以为最大上升子序列这种类型class Solution: def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ ls=len(s) if ls==0:

2018-11-28 15:31:19 526

原创 python leetcode 390. Elimination Game

第n次删:从左删就是肯定是删奇数 从右删 如果n为奇数 删的是奇数 n为偶数删的是偶数n=n/2第n+1次删:此时如果第一次删的是奇数即留下的是偶数即n为奇数 那么n=n//2 继续按照第一次这么删 最后乘2如果留下的奇数即n为偶数 那么n=n//2 按照第一次这么删但最后结果要先乘2-1距离说明:例如1234 此时右删 删去2,4 下一次输入n为4//2=2 真实的数字为1,3 即21...

2018-11-28 15:04:44 617

原创 python leetcode 389. Find the Difference

class Solution: def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ dp=[0]*26 for i in range(len(s)):

2018-11-28 14:24:07 533

原创 python leetcode 387. First Unique Character in a String

利用find函数能极大地减少运行时间class Solution: def firstUniqChar(self, s): """ :type s: str :rtype: int """ c='qwertyuiopasdfghjklzxcvbnm' res=2**31-1

2018-11-28 14:09:02 615

原创 python leetcode 386. Lexicographical Numbers

先了解什么是字典顺序 比如201[1,10,100,101,102,103,104,105,106,107,108,109,11,110,…198,199,2,20,200,201,21,…]可以用sorted函数 但是时间复杂度会增加观察整理规律可得cur为当前数字1.若cur10<=n 则下个数为cur10 否则2.如果cur数字末尾不为9并且cur+1不大于n 则cur+=...

2018-11-28 13:52:08 635

原创 python leetcode 382. Linked List Random Node

不知道长度 蓄水池抽样原理? 假设三个节点1,2,3,4第一次循环 res=1第二次循环 res有1/2的概率=2 另外1/2=1第三次循环 res有1/3的概率=3 另外2/3的概率等于其他值 根据第二循环可得 1/2*(2/3)=1/3的概率等于21/2*(2/3)=1/3的概率等于1第四次循环 res有1/4的概率=4 另外3/4的概率等于其他值 根据第三循环可得 1/3*(...

2018-11-27 21:33:50 559

原创 python leetcode Insert Delete GetRandom O(1) - Duplicates allowed

代码一:在remove中涉及排序操作 时间复杂做不到O(1) 108ms代码二:继续用空间换时间 字典中存储字典 127ms 运行时间还增加了不知道集合删除操作是不是O(1)?望告知class RandomizedCollection(object): import random def __init__(self): """ Ini...

2018-11-27 20:54:22 631

原创 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 461

原创 378. Kth Smallest Element in a Sorted Matrix

利用help函数找到mid位于矩阵中的位置 loc如果loc小于k low=mid+1 反之high=mid-1注意help函数里求得是<=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 568

原创 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 594

原创 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 539

原创 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 472

原创 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 507

原创 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 271

原创 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 704

七夕表白8个网页源代码合集.zip

一共8个网页,超大合集近60M。 七夕表白8个网页源代码合集.zip

2019-08-07

Modern PHP++++++.pdf.zip

Modern+PHP(中文版).rar Modern+PHP的中文翻译版。属于进阶书籍

2019-08-06

laravel框架.ppt

此PPT讲述了Laravel框架的设计概念并对其他的框架做了一个简单的对比。 一共33页PPT

2019-08-06

七夕表白html+css合集.zip

五款七夕表白html+css合集,大声说出你的故事。专属于程序员的浪漫

2019-08-06

NLP汉语自然语言处理原理与实践_郑捷(著)_.pdf

NLP汉语自然语言处理原理与实践_郑捷(著) 非常不错的学习资料

2019-04-21

PYTHON QT GUI快速编程 PYQT编程指南

PYTHON QT GUI快速编程 PYQT编程指南 适合入门python简单的界面设计

2018-12-06

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除