LeetCode初级算法问题(数组)

标签: Python
5人阅读 评论(0) 收藏 举报

从排序数组中删除重复项

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
查看评论

C++Builder的几则小应用

这是第一篇帖文,从网上找的。以下是原文。C++Builder的几则小应用(望月 2001年09月19日 19:00)Borland C++BUILDER(BCB)是一种面向对象的、可视化的快速应用程序...
  • lwd2k
  • lwd2k
  • 2001-10-17 22:34:00
  • 874

LeetCode初级算法练习——数组篇

数组篇26. 从排序数组中删除重复项给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度。不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到...
  • u012084802
  • u012084802
  • 2018-03-29 22:25:52
  • 23

第一周LeetCode算法题之一

题目名称:Two Sum题目难度:Easy题目描述:Given an array of integers, return indices of the two numbers such that th...
  • shujh_sysu
  • shujh_sysu
  • 2017-09-10 20:28:15
  • 57

LeetCode (算法,难度,频率)

Id Question Difficulty Frequency Data Structures Algorithms 1 T...
  • mcf171
  • mcf171
  • 2015-07-13 15:55:40
  • 502

Leetcode五大常用算法

Leetcode的5大常用算法
  • x_r_su
  • x_r_su
  • 2016-09-07 17:30:05
  • 877

Leetcode数组类题目

Move Zeroes:Easy。双指针。 Contains Duplicate:Easy。hashtable。 Majority Element:Easy。但是对答案的扩展不明白。 Remove E...
  • sjt091110317
  • sjt091110317
  • 2016-03-27 19:01:18
  • 464

LeetCode#189. Rotate Array(数组循环移位)

题目:给定一个数组和一个int值k,将数组元素向右移动k位 数组循环移位
  • u012559634
  • u012559634
  • 2017-06-01 21:35:51
  • 257

排颜色问题——数组 leetcode lintcode

问题描述: 给一个数组,并且数组里面元素的值只可能是0,1,2,然后现在把这个数组排序。 另外一种表述: 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左至...
  • TommyZht
  • TommyZht
  • 2015-06-08 09:11:15
  • 819

LeetCode初级算法问题(字符串)

反转字符串class Solution(object): def reverseString(self, s): &quot;&quot;&quot; :typ...
  • RuoShui2533
  • RuoShui2533
  • 2018-04-18 12:01:29
  • 3

leetcode_Maximum Subarray _medium(最大子数组的和)

Find the contiguous subarray within an array (containing at least one number) which has the largest ...
  • u013861066
  • u013861066
  • 2015-09-05 10:58:30
  • 644
    个人资料
    等级:
    访问量: 55
    积分: 54
    排名: 167万+
    文章分类
    文章存档