Leetcode刷题
Andrew9tech
这个作者很懒,什么都没留下…
展开
-
Leetcode 29 Divide Two Integers
class Solution: # @param {integer} dividend # @param {integer} divisor # @return {integer} def divide(self, dividend, divisor): if(dividend 0) or (dividend > 0 and divisor < 0原创 2015-05-19 02:23:39 · 306 阅读 · 0 评论 -
Leetcode 38 Sudoku Solver
Sudoku Solverclass Solution: # @param {character[][]} board # @return {void} Do not return anything, modify board in-place instead. def solveSudoku(self, board): def c原创 2015-06-23 10:02:12 · 563 阅读 · 0 评论 -
Leetcode 41 First Missing Positive
class Solution: # @param {integer[]} nums # @return {integer} def firstMissingPositive(self, nums): Length = len(nums) for i in range(Length): while nums[i] !=原创 2015-06-23 17:18:42 · 564 阅读 · 0 评论 -
Leetcode 43 Multiply Strings
class Solution: # @param {string} num1 # @param {string} num2 # @return {string} def multiply(self, num1, num2): if num1 == "0" or num2 == "0": return "0"原创 2015-07-01 15:46:52 · 666 阅读 · 0 评论 -
Leetcode 219 Contains Duplicate II
class Solution: # @param {integer[]} nums # @param {integer} k # @return {boolean} def containsNearbyDuplicate(self, nums, k): dictNums = {} for i in range(len(nums)):原创 2015-07-04 14:22:05 · 385 阅读 · 0 评论 -
Leetcode 136 Single Number
class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): dictNum = {} for i in xrange(len(nums)): dictNum[nums[i]] = dictNum.原创 2015-07-04 14:28:30 · 382 阅读 · 0 评论 -
Leetcode 94 Binary Tree Inorder Traversal
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param {TreeNod原创 2015-07-04 14:29:40 · 344 阅读 · 0 评论 -
Leetcode 121 Best Time to Buy and Sell Stock
class Solution: # @param {integer[]} prices # @return {integer} def maxProfit(self, prices): if len(prices) <= 1: return 0 low = prices[0]; mostProfit = 0 for i原创 2015-07-04 17:10:30 · 862 阅读 · 0 评论 -
Leetcode 202 Happy Number
class Solution: # @param {integer} n # @return {boolean} def isHappy(self, n): squart = dict([(i, int(i)**2) for i in '0123456789']) numSet = set() while n != 1 and原创 2015-07-04 14:24:01 · 394 阅读 · 0 评论 -
Leetcode 49 Anagrams
class Solution: # @param {string[]} strs # @return {string[]} def anagrams(self, strs): dict = {} for word in strs: sortedword = ''.join(sorted(word))原创 2015-07-04 14:27:17 · 395 阅读 · 0 评论 -
Leetcode 96 Unique Binary Search Trees
class Solution: # @param {integer} n # @return {integer} def numTrees(self, n): dp = [0]*(n+1); dp[0] = 1 for nodeNum in range(1,n+1): for k in range(nodeNum):原创 2015-07-04 17:08:39 · 365 阅读 · 0 评论 -
Leetcode 221 Maximal Square
class Solution: # @param {character[][]} matrix # @return {integer} def maximalSquare(self, matrix): if matrix == []: return 0 m, n = len(matrix), len(matrix[0])原创 2015-07-04 17:09:34 · 842 阅读 · 0 评论 -
Leetcode 70 Climbing Stairs
class Solution: # @param {integer} n # @return {integer} def climbStairs(self, n): dp = [0, 1, 2] i = 3 while i <= n: dp.append(dp[i-1] + dp[i-2])原创 2015-07-04 17:07:38 · 335 阅读 · 0 评论 -
Leetcode Best Time to Buy and Sell Stock II
class Solution: # @param {integer[]} prices # @return {integer} def maxProfit(self, prices): profit = 0; for i in xrange(1, len(prices)): if prices[i] > prices[原创 2015-07-04 17:11:41 · 716 阅读 · 0 评论 -
Leetcode 204 Count Primes
class Solution: # @param {integer} n # @return {integer} def countPrimes(self, n): isPrime = [True] * max(n, 2) isPrime[0]=False; isPrime[1]=False x = 2 whi原创 2015-07-04 14:26:04 · 346 阅读 · 0 评论 -
Leetcode 187 Repeated DNA Sequences
class Solution: # @param {string} s # @return {string[]} def findRepeatedDnaSequences(self, s): ans = [] valCnt = dict() map = {'A' : 0, 'C' : 1, 'G': 2, 'T' : 3}原创 2015-07-04 14:31:53 · 795 阅读 · 0 评论 -
Leetcode 198 House Robber
class Solution: # @param {integer[]} nums # @return {integer} def rob(self, nums): total0,total1 = 0,0 for item in nums: total0,total1 = max(total0,total1), tot原创 2015-07-04 17:05:34 · 466 阅读 · 0 评论 -
Leetcode 217 Contains Duplicate
class Solution: # @param {integer[]} nums # @return {boolean} def containsDuplicate(self, nums): dictNum = {} for i in range(len(nums)): dictNum[nums[i]] = dict原创 2015-07-04 14:23:10 · 334 阅读 · 0 评论 -
Leetcode 25 Reverse Nodes in k-Group
class Solution: # @param {ListNode} head # @param {integer} k # @return {ListNode} def reverse(self,start,end): head = ListNode(0);head.next=start while head.next!=end:原创 2015-05-19 01:01:28 · 484 阅读 · 1 评论 -
Leetcode 28 Implement strStr()
暴力匹配,时间复杂度为O(n*n),建议KMPclass Solution: # @param {string} haystack # @param {string} needle # @return {integer} def strStr(self, haystack, needle): if len(haystack) < len(need原创 2015-05-19 01:45:50 · 284 阅读 · 0 评论 -
Leetcode 35 Search Insert Position
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def searchInsert(self, nums, target): left, right = 0, len(nums)-1 while left <=原创 2015-05-21 13:07:55 · 517 阅读 · 0 评论 -
Leetcode 32 Longest Valid Parentheses
class Solution: # @param {string} s # @return {integer} def longestValidParentheses(self, s): if len(s)<2: return 0 result = 0; stack = [(-1, ')')] for原创 2015-05-21 09:29:11 · 269 阅读 · 0 评论 -
Leetcode 34 Search for a Range
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[]} def searchRange(self, nums, target): if target in nums: result = []原创 2015-05-21 11:55:59 · 461 阅读 · 0 评论 -
Leetcode 36 Valid Sudoku
三次9*9 循环,效率不高吧class Solution: # @param {character[][]} board # @return {boolean} def isValidSudoku(self, board): if len(board)!=9 or len(board[0])!=9: return False原创 2015-05-21 18:40:30 · 535 阅读 · 0 评论 -
Leetcode 33 Search in Rotated Sorted Array
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def search(self, nums, target): for i in xrange(len(nums)): if nums[i]==targ原创 2015-05-21 10:19:20 · 338 阅读 · 0 评论 -
Leetcode 30 Substring with Concatenation of All Words
class Solution: # @param {string} s # @param {string[]} words # @return {integer[]} def findSubstring(self, s, words): lenS=len(s);lenL=len(words);lenW=len(words[0]) re原创 2015-05-20 13:47:37 · 418 阅读 · 0 评论 -
Leetcode 24 Swap Nodes in Pairs
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param {ListNode} head # @return {ListN原创 2015-05-20 12:48:10 · 338 阅读 · 0 评论 -
Leetcode 23 Merge k Sorted Lists
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param {ListNode[]} lists # @return {Li原创 2015-05-18 16:51:52 · 420 阅读 · 0 评论 -
Leetcode 26 Remove Duplicates from Sorted Array
class Solution: # @param {integer[]} nums # @return {integer} def removeDuplicates(self, nums): Length = len(nums);size = 0 if Length==0: return 0 for i in range(Le原创 2015-05-19 01:02:08 · 303 阅读 · 0 评论 -
Leetcode 38 Count and Say
class Solution: # @param {integer} n # @return {string} def countAndSay(self, n): str = "1" for i in range(n-1): temp = str; str = ""; count = 1 for原创 2015-05-22 00:22:42 · 263 阅读 · 0 评论 -
Leetcode 22 Generate Parentheses
递归:class Solution: # @param {integer} n # @return {string[]} def generateParenthesis(self, n): def Recursive(left,right,string,res): if left==0 and right==0:原创 2015-05-18 15:27:45 · 272 阅读 · 0 评论 -
Leetcode 21 Merge Two Sorted Lists
class Solution: # @param {ListNode} l1 # @param {ListNode} l2 # @return {ListNode} def mergeTwoLists(self, l1, l2): head=ListNode(0) temp = head if l1 == None:原创 2015-05-18 12:55:08 · 230 阅读 · 0 评论 -
Leetcode 20 Valid Parentheses
class Solution: # @param {string} s # @return {boolean} def isValid(self, s): Length = len(s) List = [] for i in range(Length): if s[i] == '(' or s[i] =原创 2015-05-18 12:07:33 · 249 阅读 · 0 评论 -
Leetcode 17 Letter Combinations of a Phone Number
class Solution: # @param {string} digits # @return {string[]} def letterCombinations(self, digits): Dict = {"0":'', "1":'', "2": 'abc',"3": 'def', "4": 'ghi', "5":原创 2015-05-18 10:42:01 · 304 阅读 · 0 评论 -
Leetcode19 Remove Nth Node From End of List
class Solution: # @param {ListNode} head # @param {integer} n # @return {ListNode} def removeNthFromEnd(self, head, n): dummy=ListNode(0); dummy.next=head p1=p2=dummy原创 2015-05-18 11:14:11 · 230 阅读 · 0 评论 -
Leetcode 17 Letter Combinations of a Phone Number
class Solution: # @param {string} digits # @return {string[]} def letterCombinations(self, digits): alpha = [" ", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]原创 2015-05-18 23:01:29 · 362 阅读 · 0 评论 -
Leetcode 27 Remove Element
class Solution: # @param {integer[]} nums # @param {integer} val # @return {integer} def removeElement(self, nums, val): Length = len(nums);size = 0 if Length==0: retur原创 2015-05-19 01:15:18 · 280 阅读 · 0 评论 -
Leetcode 166 Fraction to Recurring Decimal
class Solution: # @param {integer} numerator # @param {integer} denominator # @return {string} def fractionToDecimal(self, numerator, denominator): negativeFlag = numerator * d原创 2015-07-04 14:33:15 · 795 阅读 · 0 评论