
Leetcode
qdbszsj
游荡ing
我的邮箱:qdbszsj@163.com
展开
-
LeetCode-1-Two-Sum HashMap
给一个int数组,给一个目标值,让你给出数组里的两个数的下标,which和是目标值C++:目测他们内置的g++编译器不支持STL的hashMap,用了本地能编译过,交上去就是CE,然后暴力一发过了,第二层for里的j都没从i+1开始都可以过。class Solution {public: vector twoSum(vector& nums, int target) {原创 2017-08-08 15:42:31 · 298 阅读 · 0 评论 -
LeetCode-2-Add-Two-Numbers 链表
题意是给俩非空链表,每个结点是0-9的数字,输出这俩链表的每一位加起来组成的一个新链表,要考虑进位的问题,链表左边是最低位,而且链表长度可以不一样。例如:3-4-5加4-6-2得7-0-85加5得0-10-8加0-2-1得0-0-2C++:/** * Definition for singly-linked list. * struct ListNode {原创 2017-08-08 20:39:11 · 236 阅读 · 0 评论 -
LeetCode-5-Longest Palindromic Substring 最长回文子串DP
C++:class Solution {public: int dp[1009][1009]; string longestPalindrome(string s) { int ansl=0,ansr=0,ans=1; int L=s.length(); memset(dp,-1,sizeof(dp)); for原创 2017-09-03 16:35:40 · 246 阅读 · 0 评论 -
LeetCode-6-ZigZag-Conversion 找规律水题
ZigZag就是折线式打印,走N形,一开始都没读懂题意。找规律,发现2*numRows-2的周期,然后输出的时候注意一下第一行和最后一行,还要注意一下只有一行的情况,因为这个我还WA了一发,开头加个特判。我觉得我代码已经不能再简单了,都是On的算法,但是只打败了6%的选手,不知道哪里比较耗时。C++和java用腻了,不写了,Python大法好:class Solution(obje原创 2017-09-04 20:55:06 · 242 阅读 · 0 评论 -
LeetCode-7-Reverse-Integer 水题
这个题比较烦的地方在于他说反转之后的数如果超过int范围要返回0。可以先把int转为字符串,然后用一个[::-1]实现倒序输出,-1应该是步长,前面两个参数要保证前面的大于后面的,比如[3:1:-1]这样子的,默认应该就是所有的了。class Solution(object): def reverse(self, x): """ :t原创 2017-09-04 21:57:06 · 179 阅读 · 0 评论 -
LeetCode-8-String-to-Integer (atoi) 细节题
很无聊的题目,注意一下要求的各种边界条件,仔细读题就好,不读题免不了WA几发class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ if(str==""):return 0 L原创 2017-09-04 22:37:15 · 199 阅读 · 0 评论 -
LeetCode-9-Palindrome-Number 水题
给你个整数问是不是回文数,负数因为有-不算回文,要求用O1的空间,Python大法好,不用考虑溢出的问题,直接把数倒着复制一遍然后比较是否相等就行了class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """原创 2017-09-04 22:51:51 · 216 阅读 · 0 评论 -
LeetCode-10-Regular-Expression-Matching 递归模拟正则表达式
没什么好说的,注意一下为空的情况就行。把p分类讨论了,然后写就行了。class Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ if p=="":原创 2017-09-05 13:18:02 · 284 阅读 · 0 评论 -
LeetCode-11-Container-With-Most-Water 贪心
在x轴上放一堆板子,问哪两个板子和x轴组成的矩形面积最大。贪心解决,初始LR在两头,向中心挪动低的那一块板子。O(n)解决class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int ""原创 2017-09-05 14:25:55 · 313 阅读 · 0 评论 -
LeetCode-12-Integer-to-Roman 无聊模拟题
模拟阿拉伯数字转罗马数字,我tm就知道罗马数字12以下的怎么写,现去查了百度才知道原来罗马数字有7个字母,到了3999就不能再大了,不然还得用新字母,而且罗马数字直观来看并不能看出来是多少,MDZZ,也就能用在钟表里装个逼了。全暴力模拟写的太麻烦,我比较懒,写了个函数优化了一下。class Solution(object): def intToRoman(self, num原创 2017-09-05 15:23:27 · 231 阅读 · 0 评论 -
LeetCode-13-Roman-to-Integer 无聊模拟,递归
跟上个题差不多,特判一下IX和IV这种情况就行了class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ romanMap={} romanMap['I']=1原创 2017-09-05 19:58:54 · 232 阅读 · 0 评论 -
LeetCode-14-Longest-Common-Prefix 求若干字符串的最大公共前缀
不知道这个题意义何在。。。class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ ans="" if strs.__len__()==原创 2017-09-05 20:42:28 · 215 阅读 · 0 评论 -
LeetCode-37-Sudoku Solver, list转字符串join,回溯
class Solution(object): count=0 ans=[[0 for x in range(9)]for y in range(9)] def solveSudoku(self, board): """ :type board: List[List[str]] :rtype: void Do not retu原创 2017-09-14 01:35:32 · 258 阅读 · 1 评论 -
LeetCode-15-3Sum 贪心
求一个数组里,有且仅有3个数和为0的组合。原本我想排序后,枚举两个数,然后再二分找第三个数,这样子时间复杂度就是O(n2logn)了,但是有n2的做法:枚举每个数,然后用贪心,O(n)的时间找到另外两个数,可以找左右两头,向中间移动,和小于0就移动L,大于0就移动R。这里枚举第i个数的时候,就从i+1找到最后就行,避免重复。Python:class Solution(o原创 2017-09-06 15:09:59 · 270 阅读 · 0 评论 -
LeetCode-16-3Sum-Closest 贪心
Similar to the last problemclass Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int ""原创 2017-09-06 15:33:44 · 187 阅读 · 0 评论 -
LeetCode-17-Letter-Combinations-of-a-Phone-Number 递归+模拟
class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ ans=[] if digits=="":return ans ma原创 2017-09-06 16:19:00 · 409 阅读 · 0 评论 -
LeetCode-38-Count and Say Python的int_to_string
题意真的很难理解。。。class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ if n==1:return "1" s="1" for i in range(n-1)原创 2017-09-14 14:05:45 · 256 阅读 · 0 评论 -
LeetCode-39-Combination Sum, 回溯dfs,Python的list.append()覆盖前面,tuple,set
写dfs的时候遇到了一些问题,直接把curAns给加到list里会出问题,需要先赋值一下再添加,赋值可以:newAns=curAns[:]然后再添加就不会出现重复的现象,如果不这么做,添加的实际上是引用,共享curAns的内存,然后curAns变了,list里所有的相关元素都会变,新开一个空间就没事了。这里set里只能存tuple,只有如此才能hash。class原创 2017-09-14 17:02:41 · 436 阅读 · 0 评论 -
LeetCode-40-Combination Sum II ,同39
跟上一道题比就改了一个地方,dfs的for循环从下一个开始,上一道是从自己开始dfsclass Solution(object): ansSet=set() def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type ta原创 2017-09-14 17:05:42 · 230 阅读 · 0 评论 -
LeetCode-41-First Missing Positive 递归水题
class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ Len=len(nums) for i in range(Len):原创 2017-09-14 17:51:00 · 229 阅读 · 0 评论 -
LeetCode-42-Trapping Rain Water 贪心或单调栈
这个题一开始想麻烦了,我用单调栈实现了一下,A了,但是只打败了0.4%的人,后来看了一眼别人的博客,发现没那么麻烦,找到最高点,从左右贪心就行了。。。下面是两份不同做法的代码,时间复杂度都是O(n)class Solution(object): def trap(self, height): """ :type height: List[in原创 2017-09-15 17:48:27 · 883 阅读 · 0 评论 -
LeetCode-43-Multiply Strings 模拟
真无聊的题目,1行代码搞定,Python大法好class Solution(object): def multiply(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ return str(int(原创 2017-09-15 19:38:18 · 177 阅读 · 0 评论 -
LeetCode-44-Wildcard Matching DP
dp[i][j]指s的前i个和p的前j个是否匹配,dp[0][0]指s和p为空时,初始化为True,其他全为F把p开头的所有的*都找出来,初始化dp[0][j]为T,因为后面dp的时候就从1开始了,不更新0了然后就是根据p[j]分类讨论就行DP:"""public boolean isMatch_2d_method(String s, String p) { int m原创 2017-09-15 22:39:44 · 307 阅读 · 0 评论 -
LeetCode-45-Jump Game II DP
这个题是典型的DP了,LeetCode有一点很不好,他不告诉你数据量大小,我就直接写了个很暴力的n方的dp然后挂在一组25000长度的数据上,然后我就在原来的基础上进行了剪枝,加了一个step,记录一下每个step最远能走到哪,就优化成On的了然后就过了。找了一下题解,发现有更简单的,空间复杂度O1的方法。。。"""public int jump(int[] A) { if(A=原创 2017-09-16 16:26:58 · 313 阅读 · 1 评论 -
LeetCode-46-Permutations 暴力递归
class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ Len=len(nums) if Len==0:return [] if原创 2017-09-16 17:23:43 · 260 阅读 · 0 评论 -
LeetCode-47-Permutations II 递归+dict
跟上个题差不多,只不过每次我都用set进行了一次Unique操作,然后TLE了。然后我就加了个dict优化一下,头上数字相同的情况下就不进行递归了,然后就A了class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :r原创 2017-09-16 17:57:13 · 222 阅读 · 0 评论 -
LeetCode-18-4Sum 预处理+暴力+字典灵活用法
这个题应该也是可以像前面那样,固定住两个点,然后左右两头向中心贪心,时间复杂度O(n3)。但是还能做一些优化:可以先把两两之和预处理存到Map里,key值是两数之和,内容是一个list,每个list的元素是一个pair,表示两数和为key的下标组合。然后就暴力的从左到右确保下标的递增,将所有的结果都存起来,为了保证结果的唯一性,用一个set存结果,因为下标是递增的唯一的,数据也是递增的原创 2017-09-10 15:27:25 · 314 阅读 · 0 评论 -
LeetCode-19-Remove-Nth-Node-From-End-of-List 链表水题
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def removeNthFromEnd(self, he原创 2017-09-10 16:50:35 · 269 阅读 · 0 评论 -
LeetCode-20-Valid-Parentheses 栈 水题 Python 列表删除操作
判断括号合法性,用个栈就行了Python里List 的pop,remove,del用法:a.pop()直接pop最后一个,x=a.pop(3)是pop下标为3的数,同时返回这个数xa.remove(3)是去掉第一个值为3的nodedel a[3]就是删除下标为3的数,这里也可以删除一个区间a[1,3]class Solution(object): def isV原创 2017-09-10 17:26:43 · 244 阅读 · 0 评论 -
LeetCode-21-Merge-Two-Sorted-Lists 递归水题
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def mergeTwoLists(self, l1, l原创 2017-09-10 17:38:15 · 374 阅读 · 0 评论 -
LeetCode-22-Generate-Parentheses 记忆化搜索,Python类变量,set强转list
用递归的记忆化搜索枚举出n组括号的情况class Solution(object): dp=[] def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ while(n>=len(self.dp)):s原创 2017-09-10 21:59:05 · 488 阅读 · 0 评论 -
LeetCode-48-Rotate Image 矩阵旋转90
一直搞不明白在Python里怎么快速复制一个矩阵class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place in原创 2017-09-19 13:44:36 · 408 阅读 · 0 评论 -
LeetCode-78-Subsets 水题暴力
class Solution(object): ans=[] cur=[] def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.ans=[] self.ans.ap原创 2017-09-27 14:42:31 · 250 阅读 · 0 评论 -
LeetCode-79-Word Search 爆搜
class Solution(object): d=[[1,0],[0,1],[-1,0],[0,-1]] used=[] def exist(self, board, word): """ :type board: List[List[str]] :type word: str :rtype: bool原创 2017-09-27 15:31:10 · 247 阅读 · 0 评论 -
LeetCode-80-Remove Duplicates from Sorted Array II 水题
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if nums==[]:return 0 Len=len(nums) for i原创 2017-09-27 15:50:50 · 775 阅读 · 0 评论 -
LeetCode-23-Merge-k-Sorted-Lists Python倒循环
这个题本来应该用类似归并排序的思想的,然而我直接暴力了一发就过了。。。不明觉厉# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(o原创 2017-09-11 13:50:53 · 809 阅读 · 2 评论 -
LeetCode-24-Swap-Nodes-in-Pairs 链表递归水题
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def swapPairs(self, head):原创 2017-09-11 14:06:47 · 236 阅读 · 0 评论 -
LeetCode-25-Reverse-Nodes-in-k-Group 链表递归水题
链表,递归,无聊的题目# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def reverseKGrou原创 2017-09-11 14:46:54 · 197 阅读 · 0 评论 -
LeetCode-26-Remove Duplicates from Sorted Array
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if nums==[]:return 0 nums.sort() ans=1原创 2017-09-11 15:02:09 · 244 阅读 · 0 评论 -
LeetCode-27-Remove Element
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ Len=len(nums) for i in原创 2017-09-11 15:39:48 · 177 阅读 · 0 评论