1. 50. Pow(x, n)

    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
  2. 49. Group Anagrams

    class 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 ...
    read more
  3. 48. Rotate Image

    # -*- 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 ...
    read more
  4. 47. Permutations II

    class 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 ...
    read more
  5. 46. Permutations

    class 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 ...
    read more
  6. 43. Multiply Strings

    class 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 ...
    read more
  7. 40. Combination Sum II

    # -*- 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 ...
    read more
  8. 39. Combination Sum

    # -*- 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 ...
    read more
  9. 38. Count and Say

    class 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 ...
    read more
  10. 36. Valid Sudoku

    class 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 ...
    read more

« Page 12 / 21 »

blogroll

social