zoukankan      html  css  js  c++  java
  • leetcode算法题121-123 --78 --python版本

    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

    实例
    输入: [0,1,0,3,12] 输出: [1,3,12,0,0]

    说明:

    1. 必须在原数组上操作,不能拷贝额外的数组。
    2. 尽量减少操作次数。

    思路:从左到右遍历数组存在数字把是0的逐一的替换,左右更替,最后在遍历剩余的直接填写0就可以
    class Solution:
    def moveZeroes(self, nums):
    if len(nums)<0:
    return
    pos = 0
    for i in range(len(nums)):
    if nums[i]:

    nums[pos]=nums[i]
    pos = pos+1

    for j in range(pos,len(nums)):
    nums[j]=0
    return nums
    s=Solution()
    nums = [1,3,0,2,0,5]
    print(s.moveZeroes(nums))



    第二种方法:遍历数组过程中存在数字的时候才交换0和数字的位置,不存在数字时point还是在0的位置,可以自己感受一下的,逻辑很简单,但是有点拐弯

    class Solution(object):
    def moveZeroes(self, nums):
    """
    :type nums: List[int]
    :rtype: void Do not return anything, modify nums in-place instead.
    """
    point = 0
    for i in range(len(nums)):
    if nums[i]:
    nums[point] , nums[i] = nums[i], nums[point]
    point += 1

     

     分析
    这个相比于前两题就要花点心思了,看题目原话

    “你最多可以完成 两笔 交易。

    注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)”,这个参考上篇博客里的1, 2, 3这个例子,

    执行过程依然是

    第一天买入,第二天卖出,收入为1,然后第二天再买入,第三天再卖出,收入为1,累计收入为2,(交易两次)。

    等同于第一天买入,第三天卖出(交易一次)。没规定必须交易几次。

    但是两笔交易一定有先后。

    在[1, 2, ...... n-1, n] 中可把两次交易分为[1, 2, ...... i] 和 [i, ...... n-1, n],这个分解过程是122中的思想,

    接着分别计算[1, 2, ...... i] 和  [i, ...... n-1, n] 中的最大利润 f[i] 和 g[i],计算方法在121中得以体现

    我们最后就是取出 max(f[i], g[i]) 就可以了

     
     
    leetcode862. 和至少为 K 的最短子数组
    返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K 。
    如果没有和至少为 K 的非空子数组,返回 -1 。

    示例 1: 输入:A = [1], K = 1 输出:1 示例 2: 输入:A = [1,2], K = 4 输出:-1 示例 3: 输入:A = [2,-1,2], K = 3 输出:3

    #使用collections.deque模块版本
    class Solution:
    def shortestSubarray(self, A, K):
    from collections import deque
    startIndex = 0
    totalSum = 0 #总和
    minLen = -1
    dequeMinus = deque() #存储和为负数区域
    for i in range(len(A)):
    totalSum += A[i]
    if A[i] < 0 :
    minusRangeSum = A[i]
    n = i
    m = i
    while minusRangeSum < 0 and n >= startIndex:
    n -= 1
    minusRangeSum += A[n]
    n += 1
    while n <= startIndex and startIndex <= i:
    totalSum -= A[startIndex]
    startIndex +=1
    while len(dequeMinus) > 0 and n <= dequeMinus[-1][0]:
    dequeMinus.pop()
    dequeMinus.append((n,m))
    while totalSum >= K:
    if minLen == -1:
    minLen = i - startIndex + 1
    else:
    minLen = min(minLen, i - startIndex + 1)
    totalSum -= A[startIndex]
    startIndex += 1
    while len(dequeMinus) > 0 and startIndex >= dequeMinus[0][0]:
    a,b = dequeMinus.popleft()
    while a <= startIndex and startIndex <= b:
    totalSum -= A[startIndex]
    startIndex += 1
    return minLen

    #使用list版本
    class Solution:
    def shortestSubarray(self, A, K):
    startIndex = 0
    totalSum = 0 #总和
    minLen = -1
    listMinus = [] #存储和为负数区域
    for i in range(len(A)):
    totalSum += A[i]
    if A[i] < 0 :
    minusRangeSum = A[i]
    n = i
    m = i
    while minusRangeSum < 0 and n >= startIndex:
    n -= 1
    minusRangeSum += A[n]
    n += 1
    while n <= startIndex and startIndex <= i:
    totalSum -= A[startIndex]
    startIndex +=1
    while len(listMinus) > 0 and n <= listMinus[-1][0]:
    listMinus.pop()
    listMinus.append((n,m))
    while totalSum >= K:
    if minLen == -1:
    minLen = i - startIndex + 1
    else:
    minLen = min(minLen, i - startIndex + 1)
    totalSum -= A[startIndex]
    startIndex += 1
    while len(listMinus) > 0 and startIndex >= listMinus[0][0]:
    a, b = listMinus[0]
    del(listMinus[0])
    while a <= startIndex and startIndex <= b:
    totalSum -= A[startIndex]
    startIndex += 1
    return minLen
     

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

    如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。

    注意你不能在买入股票前卖出股票

    输入: [7,1,5,3,6,4]
    输出: 5
    解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
    注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。

    示例 2:

    输入: [7,6,4,3,1]
    输出: 0
    解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
    class Solution(object):
    def maxProfit(self, prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    if(len(prices) <= 1):
    return 0
    buy_price = prices[0]
    max_profit = 0
    for i in range(1,len(prices)):
    buy_price = min(buy_price, prices[i])
    max_profit = max(max_profit, prices[i] - buy_price)
    return max_profit

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

    设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

    示例 1:

    输入: [7,1,5,3,6,4]
    输出: 7
    解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
      随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
    示例 2:

    输入: [1,2,3,4,5]
    输出: 4
    解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
      注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
      因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
    示例 3:

    输入: [7,6,4,3,1]
    输出: 0
    解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

    class Solution(object):
    def maxProfit(self, prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    maxpro = 0
    for i in range(1,len(prices)):
    if prices[i] > prices[i-1]:
    maxpro += prices[i] - prices[i-1]
    return maxpro

    给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

    设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

    注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

    示例 1:

    输入: [3,3,5,0,0,3,1,4]
    输出: 6
    解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
      随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
    示例 2:

    输入: [1,2,3,4,5]
    输出: 4
    解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。  
      注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。  
      因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
    示例 3:

    输入: [7,6,4,3,1]
    输出: 0
    解释: 在这个情况下, 没有交易完成, 所以最大利润为 0。

    def maxProfit(self, prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    len_prices = len(prices)
    if len_prices < 2:
    return 0

    # 用来记录不同区间里的最大利润
    f = [0] * len_prices
    g = [0] * len_prices

    # 计算原则就是121的解
    minf = prices[0]
    for i in range(1, len_prices):
    minf = min(minf, prices[i])
    f[i] = max(f[i - 1], prices[i] - minf)

    maxg = prices[len_prices - 1]
    for i in range(len_prices - 1)[::-1]:
    maxg = max(maxg, prices[i])
    g[i] = max(g[i], maxg - prices[i])

    # 取其中最大值
    maxprofit = 0
    for i in range(len_prices):
    maxprofit = max(maxprofit, f[i] + g[i])

    return maxprofit


    Leetcode 78. Subsets Python DFS 深度优先搜索解法

    问题描述
    Given a set of distinct integers, nums, return all possible subsets (the power set).
    给定一个数据集合,求该集合的所有子集。
    Note: The solution set must not contain duplicate subsets.

    For example,
    If nums = [1,2,3], a solution is:

    [3], 
    [1], 
    [2], 
    [1,2,3], 
    [1,3], 
    [2,3], 
    [1,2], 
    [] 
    ]

    思路
    深度优先算法回溯:以【1,2,3】为例

    每轮都传递一个数组起始指针的值,保证遍历顺序:

    第一轮:先遍历以1 开头的所有子集,1→12→123 →13

    第二轮:遍历以2开头的所有子集,2→23

    第三轮:遍历以3开头的所有子集,3

    这样三轮遍历保证能找到全部1开头,2开头,3开头的所有子集;同时,每轮遍历后又把上轮的头元素去掉,这样不会出现重复子集。(包括空集)

    class Solution:
    def subsets(self, nums):

    res = []
    nums.sort()
    def dfs(nums,index,path,res):
    res.append(path)

    for i in range(index,len(nums)):
    dfs(nums,i+1,path+[nums[i]],res)
    dfs(nums,0,[],res)
    return res
    s = Solution()
    nums = [1,2,3]
    print(s.subsets(nums))
  • 相关阅读:
    3dmax安装、破解与插件安装--以2014为例
    两数求和java(字符串强转int型)
    《大道至简》第一章伪代码(四个小部分)
    咳咳,软工新手读《大道至简》读后感
    一:requests爬虫基础
    爬虫
    Django中ORM系统多表数据操作
    Django中ORM简介与单表数据操作
    Django初识
    jQuery基础
  • 原文地址:https://www.cnblogs.com/limingqi/p/11976325.html
Copyright © 2011-2022 走看看