zoukankan      html  css  js  c++  java
  • 初级算法 数组

     
    alist = [0,0,0,1,1,1,2,2,3]
    []
    
     
    def distinct(index, alist):
        if index < 0:
            print(alist, len(alist))
            return alist
        if alist[index] in alist[index+1:]:
            alist.pop(index)
        distinct(index-1, alist)
     
    distinct(len(alist) - 1, alist)
    [0, 1, 2, 3] 4
    
     
    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            for index in range(len(nums)-1,-1,-1):
                print(index)
                if nums[index] in nums[index+1:]:
                    nums.pop(index)
            return len(nums)
     
    Solution().removeDuplicates([1,1,2])
    2
    1
    0
    
    2
     
    # 买卖股票的最佳时间
     
    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            max = 0 
            for index in range(len(prices)-1):
                print(index)
                if prices[index] < prices[index+1]:
                    max += prices[index+1] - prices[index]
            return max
     
    Solution().maxProfit([7,1,5,3,6,4])
    0
    1
    2
    3
    4
    
    7
     
    # 存在重复
     
    def exist_duplicate(alist):
        for index in range(1, len(alist)):
            if alist[index] in alist[0:index]:
                return True
        return False
     
    exist_duplicate([1,2,3,4,4])
    True
  • 相关阅读:
    jQuery插件主要有两种扩展方式
    系统负载测试工具-LoadRunner
    安全扫描工具-AppScan
    newinstance()和new有什么区别?(转)
    类的加载、连接和初始化 (转)
    tar 基础
    了解【重放攻击】
    DDLDMLDCLDQL
    web.xml的配置问题
    组合与聚合
  • 原文地址:https://www.cnblogs.com/zhangjian0092/p/11763456.html
Copyright © 2011-2022 走看看