python
YANG_BLOG
这个作者很懒,什么都没留下…
展开
-
LeetCode#4 Median of Two Sorted Arrays (2)
def findKth(self,a,b,k,aStart,aEnd,bStart,bEnd): aLen = aEnd - aStart + 1 bLen = bEnd - bStart + 1 if aLen == 0: return b[bStart+k] if bLe转载 2015-07-09 12:23:29 · 388 阅读 · 0 评论 -
LeetCode #22 Generate Parentheses
class Solution: # @param {integer} n # @return {string[]} def generateParenthesis(self, n): left = right = n s = '' sset = set() self.parentGen(s,left,righ转载 2015-08-05 12:01:29 · 275 阅读 · 0 评论 -
LeetCode #27 Remove Element
class Solution: # @param {integer[]} nums # @param {integer} val # @return {integer} def removeElement(self, nums, val): size = 0 for i in range(0,len(nums)):原创 2015-08-05 17:07:19 · 243 阅读 · 0 评论 -
LeetCode #26 Remove Duplicates from Sorted Array
class Solution: # @param {integer[]} nums # @return {integer} def removeDuplicates(self, nums): if len(nums) == 0: return 0 if len(nums) == 1: retur原创 2015-08-05 16:31:08 · 265 阅读 · 0 评论 -
判断一个树是否为另一棵树的子树
class treeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Nonedef loopTree(node): if node!= None: print node.val loopTree(node.left) loopTree(node.right原创 2015-09-07 22:22:50 · 647 阅读 · 0 评论 -
LeetCode #1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-07-03 07:50:18 · 409 阅读 · 0 评论 -
Binary Search 二分查找
def binarySearch(A,val): if A == None or len(A) == 0: return None l = 0 h = len(A) - 1 while l <= h: mid = l + (h-l) / 2 if A[mid] > val: h = mid - 1 elif A[mid] < val: l = mid +转载 2015-09-07 21:38:11 · 263 阅读 · 0 评论 -
汉字数字转化为阿拉伯数字
#汉字数字转化为阿拉伯数字def hanTwoMoney(s): money = {u'一':1,u'二':2,u'三':3,u'四':4,u'五':5,u'六':6,u'七':7,u'八':8,u'九':9} if len(s) == 0: return None sum = 0 if len(s) >= 0: if money.has_key(s[len(s)-1]):原创 2015-09-08 19:52:39 · 866 阅读 · 0 评论 -
5道经典的程序题 (2)
输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。def minKValues(A,k): kValues = [] for i in range(0,k): kValues.append(A[i]) for i in range(k,len(A)): maxPos = 0转载 2015-09-04 13:44:36 · 341 阅读 · 0 评论 -
LeetCode #2 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2015-07-03 07:51:55 · 381 阅读 · 0 评论 -
5道经典的程序题 (1)
题目1输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18。def maxSubArry(A): sumVal =转载 2015-09-03 22:15:17 · 422 阅读 · 0 评论 -
5道经典的程序题 (3)
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student. a am I”。def reverseSentense(S): i = 0 j = len(S) - 1 while i<j: temp = S[i] S[i转载 2015-09-04 18:06:02 · 372 阅读 · 0 评论 -
5道经典的程序题 (5)
输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。def sumVal(A,k): i = 0 j = len(A)-1 while i < j: if A[i] + A[j] > k: j-=1 elif A[i] + A[j] < k:转载 2015-09-04 21:34:07 · 443 阅读 · 0 评论 -
5道经典的程序题 (4)
求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。def nSum(n): if n == 0: return 0 return n + nSum(n-1)转载 2015-09-04 21:26:19 · 318 阅读 · 0 评论 -
归并排序 MergeSort
def mergeSort(arry,left,right): if left < right: mid = (left + right) / 2 mergeSort(arry,left,mid) mergeSort(arry,mid+1,right) mergeArry(arry,left,right,mid) return arrydef mergeArry(arry转载 2015-08-16 11:14:23 · 296 阅读 · 0 评论 -
LeetCode #21 Merge Two Sorted Lists
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param {ListNode} l1 # @param {ListNode原创 2015-08-04 21:37:18 · 256 阅读 · 0 评论 -
LeetCode #14 Longest Common Prefix
class Solution: # @param {string[]} strs # @return {string} def longestCommonPrefix(self, strs): if len(strs) == 0: return "" #find the longest substr原创 2015-07-12 10:40:16 · 293 阅读 · 0 评论 -
LeetCode #9 Palindrome Number
class Solution: # @param {integer} x # @return {boolean} def isPalindrome(self, x): if x < 0: return False y = 0 cX = x while x:原创 2015-07-12 10:02:45 · 303 阅读 · 0 评论 -
LeetCode#4 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).class Solution: #原创 2015-07-04 07:21:25 · 372 阅读 · 0 评论 -
LeetCode #3 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-07-03 13:43:14 · 307 阅读 · 0 评论 -
LeetCode#5 Longest Palindromic Substring
class Solution: # @param {string} s # @return {string} def longestPalindrome(self, s): lenS = len(s) maxLen = 0 maxStr = "" for i in range(0,lenS):原创 2015-07-11 18:17:50 · 454 阅读 · 0 评论 -
LeetCode #15 3Sum
class Solution: # @param {integer[]} nums # @return {integer[][]} def threeSum(self, nums): if nums == None: return None if len(nums) == 0: return [原创 2015-07-12 15:44:12 · 269 阅读 · 0 评论 -
LeetCode #7 Reverse Integer
class Solution: # @param {integer} x # @return {integer} def reverse(self, x): isNegative = 1 y = 0.0 if x < 0: isNegative = -1 x = abs(x)原创 2015-07-11 19:00:47 · 456 阅读 · 0 评论 -
LeetCode #16 3Sum Closest
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def threeSumClosest(self, nums, target): if len(nums) == 0: return None原创 2015-07-17 17:19:54 · 279 阅读 · 0 评论 -
快速排序 QuickSort
def adjustArry(arry,left,right): i = left j = right x = arry[i] while i < j: while i= x: j-=1 if arry[j] < x: arry[i] = arry[j] i+=1 while i<j and arry[i] <= x: i+=1 if arry[转载 2015-08-11 17:02:31 · 282 阅读 · 0 评论 -
LeetCode #17 Letter Combinations of a Phone Number
class Solution: # @param {string} digits # @return {string[]} def letterCombinations(self, digits): depth = 0 ans = [] path = "" self.comb(depth,ans,digits,转载 2015-07-21 11:25:43 · 354 阅读 · 0 评论 -
LeetCode #18 4Sum
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[][]} def fourSum(self, nums, target): valMap = {} ans = set() nums.sort()转载 2015-07-23 10:49:38 · 275 阅读 · 0 评论 -
LeetCode #19 Remove Nth Node From End of List
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param {ListNode} head # @param {intege原创 2015-07-23 11:57:42 · 247 阅读 · 0 评论 -
LeetCode #20 Valid Parentheses
class Solution: # @param {string} s # @return {boolean} def isValid(self, s): tack = [] for i in range(0,len(s)): if s[i] == '(' or s[i] == '{' or s[i] == '[' o原创 2015-07-24 08:37:15 · 308 阅读 · 0 评论 -
LeetCode #58 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined a原创 2015-10-08 17:48:34 · 311 阅读 · 0 评论