數組

  1. 數組中重複的數據

給定一個整數數組 a,其中1 ≤ a[i] ≤ n (n爲數組長度), 其中有些元素出現兩次而其他元素出現一次。

找到所有出現兩次的元素。

你可以不用到任何額外空間並在O(n)時間複雜度內解決這個問題嗎?

代碼:

class Solution(object):
    def findDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        if nums is None or len(nums) < 2:
            return []
    
        result = []

        for i in range(len(nums)):

            while nums[i] != i + 1:  # 當前位置的值是否符合index
                if nums[i] == nums[nums[i] - 1]:  # 發現重複項
                    result.append(nums[i])
                    break

                temp = nums[nums[i] - 1]
                nums[nums[i] - 1] = nums[i]
                nums[i] = temp
        return list(set(result))
  1. 兩數之和

代碼:

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        n=len(nums)
        #創建一個空字典
        HashMap= {}
        for i in range(n):
            complement = target - nums[i]
            #字典d中存在nums[x]時
            if complement in HashMap.keys():   #  if complement in HashMap:判斷某個鍵是否存在的兩種方式
                return HashMap[complement],i
            #否則往字典增加鍵/值對
            else:
                HashMap[nums[i]] = i
        #邊往字典增加鍵/值對,邊與nums[x]進行對比
  1. 盛最多水的容器

代碼:

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left = 0
        right = len(height) - 1
        maxArea = 0
        while left < right:
            b = right - left
            if height[left] < height[right]:
                h = height[left]
                left += 1
            else:
                h = height[right]
                right -= 1
            area = b*h
            if maxArea < area:
                maxArea = area
        return maxArea
  1. 三數之和

代碼:

class Solution:
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = list()
        nums_len = len(nums)
        if nums_len < 3:
            return result
        l, r, dif = 0, 0, 0
        nums.sort()
        for i in range(nums_len - 2):
            if nums[i] > 0: 
                break
            if i > 0 and nums[i - 1] == nums[i]:
                continue

            l = i + 1
            r = nums_len - 1
            dif = -nums[i]
            while l < r:
                if nums[l] + nums[r] == dif:
                    result.append([nums[l], nums[r], nums[i]])
                    while l < r and nums[l] == nums[l + 1]:
                        l += 1
                    while l < r and nums[r] == nums[r - 1]:
                        r -= 1
                    l += 1
                    r -= 1
                elif nums[l] + nums[r] < dif:
                    l += 1
                else:
                    r -= 1
        
        return result
  1. 刪除排序數組中的重複項

給定一個排序數組,你需要在原地刪除重複出現的元素,使得每個元素只出現一次,返回移除後數組的新長度。

不要使用額外的數組空間,你必須在原地修改輸入數組並在使用 O(1) 額外空間的條件下完成。

代碼:

class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) <= 1:
            return len(nums)
        s = 0
        for f in range(1, len(nums)):
            if nums[s] != nums[f]:
                s += 1
                nums[s] = nums[f]
        return s + 1
  1. 接雨水

給定 n 個非負整數表示每個寬度爲 1 的柱子的高度圖,計算按此排列的柱子,下雨之後能接多少雨水。
代碼:

class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        """
        基本思路:先找到最大值,即山峯peak的位置,然後用雙指針,分別從兩端的柱子靠近,
        如果柱子大於前面柱子的最大值則不存在積水,小於則存在積水,並且當前柱子的積水量:前面柱子的最大值-當前柱子的高度        
        
        
        """
        if height is None:
            return 0
        maxh=0
        indexmax=0
        for i in range(len(height)):
            if maxh < height[i]:
                maxh=height[i]
                indexmax=i
        left,right=0,0
        re=0
        for i in range(indexmax):
            if height[i]>left:
                left=height[i]
                continue
            re+=left-height[i]
        for j in range(len(height)-1,indexmax,-1):
            if right<height[j]:
                right=height[j]
                continue
            re+=right-height[j]
        return re
  1. 刪除排序數組中的重複項 II
class Solution:
    def removeDuplicates(self, nums: 'List[int]') -> 'int':
        i = 0
        for e in nums:
            if i < 2 or e != nums[i-2]:
                nums[i] = e
                i += 1
        
        return i
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章