# -*- coding:utf8 -*-
class Solution:
# @param {integer[]} nums
# @return {string}
def largestNumber(self, nums):
res = ''
while nums:
item = nums.pop(self.help(nums))
res += str(item)
if (res[0]=='0'):
return '0'
return res
def help(self, nums): # 如果a+b串大于b+a串,那么a比较大,反之b比较大。
strnums = [str(x) for x in nums]
index ...- read more
174. Dungeon Game
read moreclass Solution(object): def calculateMinimumHP(self, dungeon): """ :type dungeon: List[List[int]] :rtype: int """ # m = len(dungeon) # n = len(dungeon[0]) # dungeon[m-1][n-1] = 1 - dungeon[m-1][n-1] # for i in range(m-1, -1, -1): # for j in range(n-1, -1, -1): # if i == m-1 and j == n-1: # continue # if ...
173. Binary Search Tree Iterator
read more# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class BSTIterator(object): def __init__(self, root): """ :type root: TreeNode """ self.stack = [] self.pushleft(root) def hasNext(self): """ :rtype: bool """ return self.stack def next(self): """ :rtype: int ...
172. Factorial Trailing Zeroes
read moreclass Solution(object): def trailingZeroes(self, n): """ :type n: int :rtype: int """ res = 0 while n > 0: n /= 5 res += n return res
171. Excel Sheet Column Number
read moreclass Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ alpha = [chr(x) for x in range(ord('A'), ord('Z')+1)] array = list(s) res = 0 for a in array: res = res * 26 + alpha.index(a) + 1 return res
170. Two Sum III - Data structure design
read more# Time: O(n) # Space: O(n) # Design and implement a TwoSum class. It should support the following operations: add and find. # # add - Add the number to an internal data structure. # find - Find if there exists any pair of numbers which sum is equal to the value. # # For example, # add(1 ...
169. Majority Element
read moreclass Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ candidate = None count = 0 for num in nums: if count == 0: candidate = num count += 1 elif candidate == num: count += 1 else: count -= 1 return candidate if __name__ == "__main__": print Solution().majorityElement([1,2,2,3,3,3 ...
168. Excel Sheet Column Title
read moreclass Solution(object): def convertToTitle(self, n): """ :type n: int :rtype: str """ ans = '' while n > 0: ans = chr(ord('A') + (n - 1) % 26) + ans n = (n - 1) / 26 return ans if __name__ == "__main__": print Solution().convertToTitle(52)
167. Two Sum II - Input array is sorted
read moreclass Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ m = {} for i in range(len(numbers)): if numbers[i] in m: return [m[numbers[i]]+1, i+1] else: m[target - numbers[i]] = i
166. Fraction to Recurring Decimal
read moreclass Solution(object): def fractionToDecimal(self, numerator, denominator): """ :type numerator: int :type denominator: int :rtype: str """ flag = 1 if numerator * denominator < 0: flag = -1 numerator = abs(numerator) denominator = abs(denominator) before = numerator/denominator remain = numerator - before * denominator if remain == 0: if flag == 1: return str(before) else: return '-' + str(before ...