LeetCode初级算法问题(数组)

从排序数组中删除重复项

class Solution(object):  
    def removeDuplicates(self, nums):  
        """ 
        :type nums: List[int] 
        :rtype: int 
        """  
        if not nums: return 0  
        length,index,t = len(nums),1,nums[0]  
        for s in nums[1:]:  
            if  s == t:
                length -=1
            else:
                nums[index] = s
                index +=1
                t = s
        return length  

买卖股票的最佳时机 II

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        length = len(prices)
        if not prices or length == 1:
            return 0
        max_profit = 0
        temp_min = prices[0]
        j = 1
        while j < length:
            if prices[j] > temp_min:
                max_profit += prices[j] - temp_min
                temp_min = prices[j]
                j += 1
                continue
            if prices[j] <= temp_min:
                temp_min = prices[j]
                j += 1
        return max_profit

旋转数组

class Solution:
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k = k % n
        nums[:k], nums[k:] = nums[n-k:], nums[:n-k]

存在重复

class Solution:
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        return len(nums) != len(set(nums))

只出现一次的数字

class Solution:
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a =  0
        for i in nums:
            a ^= i
        return a

两个数组的交集 II

class Solution:
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        from collections import Counter
        c1 = Counter(nums1)
        c2 = Counter(nums2)
        return list((c1&c2).elements())

加一

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        i = len(digits) - 1
        extra = 1
        while i >= 0 and extra == 1:
            digits[i] = digits[i] + 1
            extra = 0
            if digits[i] > 9:
                digits[i] = 0
                extra = 1
            i -= 1
        if i == -1 and extra == 1:
            digits[0] = 1
            digits += [0]
        return digits

移动零

class Solution:
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        tmp = [x for x in nums if x != 0]
        nums[:len(tmp)] = tmp
        nums[len(tmp):] = [0] * (len(nums) - len(tmp))

两数之和

class Solution:
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        tmp = [x for x in nums if x != 0]
        nums[:len(tmp)] = tmp
        nums[len(tmp):] = [0] * (len(nums) - len(tmp))

有效的数独

class Solution:
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        row = [[] for _ in range(9)] #行记录器
        col = [[] for _ in range(9)]#列记录器
        area = [[] for _ in range(9)]#子区域记录器
        for i in range(9):
            for j in range(9):
                element = board[i][j]#遍历所有元素
                if element != '.':#有数字才记录下来
                    top_id = i//3*3 + j//3#每个元素子区域的计算方法
                    if element in row[i] or element in col[j] or element in area[top_id]:#如果在这一行列子区域重读出现过了
                        return False
                    else:#没有出现过就加进去
                        row[i].append(element)
                        col[j].append(element)
                        area[top_id].append(element)
        return True

旋转图像

class Solution:
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        l=len(matrix)
        for i in range(l//2):
            for j in range(i,l-i-1):
                k=matrix[i][j]
                matrix[i][j]=matrix[l-j-1][i]
                matrix[l-j-1][i]=matrix[l-i-1][l-j-1]
                matrix[l-i-1][l-j-1]=matrix[j][l-i-1]
                matrix[j][l-i-1]=k
阅读更多
文章标签: Python
上一篇Python:汉诺塔问题
下一篇LeetCode初级算法问题(字符串)
想对作者说点什么? 我来说一句

leetcode算法题答案PDF

2016年10月14日 1.54MB 下载

没有更多推荐了,返回首页

关闭
关闭