自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(48)
  • 收藏
  • 关注

原创 leetcode之Isomorphic Strings

这题跟word pattern有点像,不过要是用word pattern的替换法来做的话肯定会超时的,这题的替换仅仅是单个字符对单个字符。所以用别的方法来看。代码如下:class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str

2015-10-31 16:03:27 284

原创 leetcode之Word Pattern

这道题是要求把字母换成一个字符串之后,比较2者是否相等。注意在替换的过程中直接在原字符串上替换有可能导致前面替换的对后面产生影响,且2个不同的字母的替换不能相同。代码如下:class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str

2015-10-31 14:04:26 245

原创 leetcode之Bulls and Cows

猜数字游戏。报出来每次有几个数字是在正确的位置的,多少是不正确的。先求前者,再求后者。代码如下:class Solution(object): def getHint(self, secret, guess): """ :type secret: str :type guess: str :rtype: str

2015-10-31 12:22:47 285

原创 leetcode之Search Insert Position

这题是有这个数就取出位置,没有就看插入到哪去,简单点想就是插进去,排个序,看看位置。代码如下:class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int

2015-10-25 00:43:07 230

原创 leetcode之Binary Tree Postorder Traversal

这个是二叉树的后序遍历啦。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Sol

2015-10-25 00:19:53 249

原创 leetcode之Binary Tree Preorder Traversal

这题就是二叉树的前序遍历。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Sol

2015-10-25 00:07:44 285

原创 leetcode之Validate Binary Search Tree

这道题判断一个二叉树是不是BST。用中序遍历成为一个数组,看是不是每个值都比后面小就好了。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None#

2015-10-24 23:06:41 254

原创 leetcode之Binary Tree Inorder Traversal

这个就是写一下中序遍历。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Soluti

2015-10-24 22:25:22 276

原创 leetcode之Kth Smallest Element in a BST

这道题是在BST上找到第k个最小的数,BST本身便是一个从左到右依次排序的树,问题的难点在于你如何排序,排序好了之后,第k个就一目了然了。代码如下:a = [] def sort(root): if root == None: return 0 if root.left != None:

2015-10-24 21:41:18 255

原创 leetcode之Set Matrix Zeroes

这道题要的尽量少用空间的使带有0的行和列的数值都变为0。因为要尽量少用空间,就没有用额外空间的把0替换掉,然后再根据替换的来解决。代码如下:class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: void

2015-10-24 15:33:09 453

原创 leetcode之Invert Binary Tree

这个题目就是反转左右节点。很简单啊。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclas

2015-10-24 02:37:14 289

原创 leetcode之Same Tree

这题就是来看每个节点的值想不想等。看一下左树的值,再看下右树的值来判断的。代码如下:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self

2015-10-24 01:12:58 267

原创 leetcode之Sliding Window Maximum

这题就是相当于一直求一个在变化的list的最大值组成的集合。代码如下:class Solution(object): def maxSlidingWindow(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] "

2015-10-23 00:30:51 277

原创 leetcode之Kth Largest Element in an Array

这题是求出来排序好的数的第k个大的数。用了.sort()之后就非常简单了。不用自己排序的表示真是简单啊。然而真讲排序我还基本不会。。。代码如下:class Solution(object): def findKthLargest(self, nums, k): """ :type nums: List[int] :type k: int

2015-10-23 00:10:20 283

原创 leetcode之Maximum Gap

这道题要求是求出来连续2个数的最大间隔。要求是线性的空间和时间。感觉很简单啊,不知道为啥是个hard。难道是因为python自带了list.sort()函数?代码如下:class Solution(object): def maximumGap(self, nums): """ :type nums: List[int] :rtype:

2015-10-23 00:05:13 290

原创 leetcode之Climbing Stairs

这题其实跟斐波那契数列很像啦,都是num(n) = num(n - 1) + num(n -2),不过python并没有对尾递归做出优化,所以还是用的for循环来写的。代码如下;class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int

2015-10-22 02:37:17 409

原创 leetcode之Search in Rotated Sorted Array

这道题的目的是找出来target在列表中的位置。不知道为啥分类在hard。代码如下:class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int

2015-10-21 00:31:07 251

原创 leetcode之Search a 2D Matrix II

这个题目II和I对于python来说,都是一样的,代码都不用换。代码如下:class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: b

2015-10-21 00:20:37 241

原创 leetcode之Search a 2D Matrix

这个题目是来寻找list[list]里是否含有指定的数。对于python来讲就太简单了。把所有的list的数放到一个list里,检测一下就好了。代码如下:class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]]

2015-10-21 00:15:43 228

原创 leetcode之Median of Two Sorted Arrays

这道题是求2个有序序列的中位数,就是中间数or2个数的平均值。用python是很简单的。代码如下:class Solution(object): def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]

2015-10-20 19:08:52 229

原创 leetcode之Remove Duplicates from Sorted Array

从排好序的列表中删除重复数。这个有Counter之后就可以反过来先算出来哪些重复,该删多少个,然后删就是了。代码如下:class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """

2015-10-20 18:50:22 240

原创 leetcode之LRU Cache

这次的功能是编写一个缓存,能取出来特定的值,以及缓存满了的时候能够自动清除最老的数据,将最新的数据更新进去。代码如下:class LRUCache(object): def __init__(self, capacity): """ :type capacity: int """ self.items = {}

2015-10-20 14:20:45 236

原创 leetcode之Divide Two Integers

一开始以为这道题很简单了,不让用乘除余数,可以循环用加法,但是超时了之后才注意到限制。就得模拟人进行除法的运算。代码如下:class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int :type divisor: int

2015-10-19 23:26:22 307

原创 leetcode之Excel Sheet Column Title

这个跟上一篇正好是反过来的,这个是反26进制,给一个数来推导出字符串来。需要注意处理n % 26 == 0的情况。代码如下:class Solution(object): def convertToTitle(self, n): """ :type n: int :rtype: str """ dic =

2015-10-19 21:43:13 318

原创 leetcode之Excel Sheet Column Number

这个excel sheet column number就是一个26进制数。代码如下:class Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ dic = {'A': 1, 'B': 2, 'C':

2015-10-19 21:14:26 255

原创 leetcode之Majority Element II

majority element 2比1的不同在于1:不再是一定存在元素;2:由n/2变为n/3。1的可以排序直接查中间的元素来做,2就不行了。祭出来Counter就十分轻松啦。代码如下:class Solution(object): def majorityElement(self, nums): """ :type nums: List[int]

2015-10-18 16:34:01 304

原创 leetcode之Product of Array Except Self

product of array except self,这道题本身看上去不难,只是加了条件是要求O(n),这就要求只能遍历一次,不能每次都乘一遍。我的做法是遍历一次,把非零的都乘起来,将0 的单独处理。代码如下:class Solution(object): def productExceptSelf(self, nums): """ :type nu

2015-10-18 10:41:03 213

原创 leetcode之Letter Combinations of a Phone Number

这个没什么好说的,就是每个数字代表的字母的排列组合。代码如下:class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ phonenumber = [['

2015-10-17 16:50:45 341

原创 leetcode之Subsets II

接上次的Subsets,这次的改动就是先不去除重复元素,等到了最后的时候再去除,大体代码结果差不多。前面把set(nums)去除重复给去掉了。后面不可以用list(set())的方法去去除重复,因为类型是list[list],不可hash。代码如下:class Solution(object): def subsetsWithDup(self, nums): """

2015-10-17 14:58:53 230

原创 leetcode之Subsets

这次的题目目的是给出一个list下的所有子集,可以看到一个递归规律就是新加一个数的新增量都是在原有的数的每一位都加上了新增量。也就是知道了前面的结果,就知道了新增的部分。代码如下:class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtyp

2015-10-17 14:44:11 290

原创 latched之First Missing Positive

这道题一方面是没有排序过,一方面是有可能有重复的。本次采用了跟上个missing number不一样的判断。代码如下:class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """

2015-10-17 11:48:47 387

原创 leetcode之Missing Number

这次的leetcode题目是missing number。找到缺失的一个数字。唯一的陷阱在于有可能啥都不缺,那么就得返回最后的数+ 1了。代码如下:class Solution(object): def missingNumber(self, nums): """ :type nums: List[int] :rtype: int

2015-10-17 11:32:45 265

原创 latched之Single Number III

这次的III仅仅比2多了一个要计算的东西,返回的也是列表,有Counter在也很简单。代码如下:class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: List[int] """ list

2015-10-17 11:25:36 245

原创 leetode之Single Number II

这次的Single Number II是找粗来唯一一个出现次数不是3的数,有神器Counter在手,很简单的。代码如下:class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """

2015-10-17 11:20:20 380

原创 leetcode之Reverse Interger

这次是leetcode的Reverse Interger。翻转可以有很多方法,但是需要翻转时去掉了尾数0,所以最先想到的就是strip函数啦。再注意下正负,就ok啦。代码如下:class Solution(object): def reverse(self, x): """ :type x: int :rtype: int

2015-10-17 11:00:46 273

原创 leetcode之Two Sum

Two Sum的意思是求出2个和为指定值的2个数的index + 1。因为python是从0开始的,所以最后要加上1.鉴于原题已经假设有且仅有一个解,因此就没有考虑特殊情况啦。需要注意的是如果是2个想等的数的和为target,需要使用index的时候注意一下,包括后面的index都会跟着改变。代码如下:class Solution(object): def twoSum(self, n

2015-10-16 16:22:21 280

原创 leetcode之Contains Duplicate II

接上次的Contains Duplicate,这次的ii的难度变大了。要求看是否2个一样的数的index之差小于指定的k。还是用的Counter方法,先求出了所有的重复值。然后针对每个重复值来返回去求index,最后收录在一个list里。最后求相互之间的差值。代码如下:class Solution(object): def containsNearbyDuplicate(self, n

2015-10-15 23:37:04 269

原创 leetcode之Contains Duplicate

继续写leetcode啦,既上次发现了Counter函数之后,又发现了用武之地啦。这次的题目简直就是撞枪口了。检查是否有重复数。太简单啦。代码如下:class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: boo

2015-10-15 23:02:32 265

原创 leetcode之Implement Stack using Queues

跟上一篇的Implement Queue using Stacks差不多。唯一一个不同的就是一个是在list后面加一个元素,这个是在头部插一个元素。代码如下:class Stack(object): def __init__(self): """ initialize your data structure here. """

2015-10-12 22:36:07 399

原创 leetcode之Implement Queue using Stacks

这道题就是实现栈的操作。用list来实现,很简单的。代码如下:class Queue(object): def __init__(self): """ initialize your data structure here. """ self.items = [] def push(self,

2015-10-12 22:00:22 401

空空如也

空空如也

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

TA关注的人

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