- 博客(51)
- 收藏
- 关注
原创 python 全排列
itertools模块现成的全排列:for i in itertools.permutations('abcd',4): print ''.join(i)相关全排列算法:def perm(l): if(len(l)<=1): return [l] r=[] for i in range(len(l)):
2015-08-09 11:12:55
825
原创 决策树--Python实现
信息增益导入模块:from math import logimport operator计算给定数据集的香农熵:def calcShannonEnt(dataSet): numEntries = len(dataSet) lableCounts = {} for featVec in dataSet: currentLable =
2015-08-03 09:52:13
763
原创 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
744
原创 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
902
原创 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
872
原创 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
393
原创 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
364
原创 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
489
原创 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
834
原创 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
826
原创 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
362
原创 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
426
原创 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
430
原创 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
372
原创 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
431
原创 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
366
原创 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
421
原创 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
698
原创 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
598
原创 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
596
原创 python decode()、encode() 、编码与反编码
python中提到unicode,一般指的是unicode对象,比如:“叫我”的对象为'\u53eb\u6211''.字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。decode的作用是将其他编码的字符串转换成uni
2015-05-24 18:03:57
919
原创 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
287
原创 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
563
原创 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
550
原创 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
491
原创 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
367
原创 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
299
原创 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
440
原创 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
373
原创 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
327
原创 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
312
原创 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
312
原创 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
324
原创 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
520
原创 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
392
原创 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
451
原创 Python heapq模块
heapq模块提供了如下几个函数:heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表)heapq.heappop(heap) 把堆顶元素弹出,返回的就是堆顶heapq.heappushpop(heap, item) 先把item加入到堆中,然后再pop,比heappush()再heappop()要快得多h
2015-05-18 16:30:22
418
原创 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
304
原创 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
255
原创 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
276
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人