class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
flag = 0
if n < 0:
flag = 1
n = abs(n)
res = 1
while n > 0:
if n % 2 == 1:
res = res * x
x = x * x
n >>= 1
if flag:
return 1/res
else:
return ...- read more
49. Group Anagrams
read moreclass Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ map = {} for item in strs: order = ("".join(sorted(item))).lower() if order not in map: map[order] = [item.lower()] else: map[order] += [item.lower()] l = [] for i in map.keys(): l.append(map[i]) return ...
48. Rotate Image
read more# -*- coding: utf-8 -*- #首先沿逆对角线翻转一次,然后按x轴中线翻转一次。 class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """ n=len(matrix) for i in range(n): for j in range(n-i-1): matrix[i][j], matrix[n - 1 - j][n - 1 ...
47. Permutations II
read moreclass Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.result = [] nums.sort() for item in list(set(nums)): i = nums.index(item) length = len(nums) self.recursive_comb([nums[i]], nums[:i] + nums[i + 1:], length) return self.result def recursive_comb(self, temp_array, rest_array ...
46. Permutations
read moreclass Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.result = [] nums.sort() for i in range(len(nums)): length=len(nums) self.recursive_comb([nums[i]], nums[:i] + nums[i + 1:], length) return self.result def recursive_comb(self,temp_array, rest_array, target): if len(temp_array ...
43. Multiply Strings
read moreclass Solution(object): def multiply(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ list1 = list(num1) list2 = list(num2) int1 = 0 for i in list1: int1 = int1 * 10 + (ord(i)-ord("0")) int2 = 0 for j in list2: int2 = int2 * 10 + (ord(j) - ord("0")) return str ...
40. Combination Sum II
read more# -*- coding: utf-8 -*- class Solution(object): def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ self.result = [] candidates.sort() for i in range(len(candidates)): self.recursive_comb([candidates[i]], candidates[i + 1:], target) return self.result def recursive_comb(self,temp_array, rest_array, target): if sum ...
39. Combination Sum
read more# -*- coding: utf-8 -*- class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ self.result = [] candidates.sort() for i in range(len(candidates)): self.recursive_comb([candidates[i]], candidates[i:], target) return self.result def recursive_comb(self, temp_array, rest_array, target): if sum(temp_array ...
38. Count and Say
read moreclass Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ s="1" for i in range(n-1): s=self.generator(s) return s def generator(self,nums): flag = nums[0] count = 1 newnums = "" for i in range(1, len(nums)): if nums[i] != flag: newnums += str(count ...
36. Valid Sudoku
read moreclass Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ for i in range(9): for j in range(9): if board[i][j]!="." and not self.checkSudoku(i,j,board): return False return True def checkSudoku(self,i,j,board): for compare in range(9 ...