[Leetcode] 27. Remove Element

閱讀時間約 3 分鐘

題目 : 27. Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • Return k.


Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).


1.

雖然題目的文字很多,但是其實目的就只是要將nums中所有的val值進行刪除的動作。

如果這邊是使用if而不是while的話,就只會刪除第一個出現在nums的val值

class Solution:
def removeElement(self, nums: List[int], val: int) -> int:

# 將nums中的val值移除,直到nums沒有val的值出現
while val in nums:
nums.remove(val)
# 最後回傳nums剩下幾個元素
return len(nums)
    Youna
    Youna
    留言0
    查看全部
    發表第一個留言支持創作者!
    Youna 的其他內容
    [Leetcode] 9. Palindrome Number
    閱讀時間約 1 分鐘
    [Leetcode] 66. Plus One
    閱讀時間約 3 分鐘
    [Leetcode] 35. Search Insert Position
    閱讀時間約 2 分鐘
    [Leetcode] 69. Sqrt(x)
    閱讀時間約 2 分鐘
    [Leetcode] 88. Merge Sorted Array
    閱讀時間約 4 分鐘
    你可能也想看
    [TS LeetCode] 2631. Group By 重要知識點: 1. TypeScript 全局擴展,使所有陣列都能使用 groupBy 方法。 2. 利用泛型創建彈性函數,提高代碼可重用性。 3. 迭代陣列中的元素,實現遍歷和處理功能。 4. 物件的鍵值對操作,用於建立以函數輸出為鍵的物件。
    Thumbnail
    avatar
    毛怪
    2023-10-12
    一魚三吃 用雙指針的觀念來解 Remove Element 移除元素 Leetcode #27雖然表面是一道移除元素的題目,但實際上考的還是搬移。 題目要求的是把val目標值移除,等價於 把非目標值的搬到前面,並且回傳這些剩下來的元素個數(也就是 陣列裡面,非目標值的元素總數​)。 請看下方範例,會更好理解。
    Thumbnail
    avatar
    小松鼠
    2023-09-28
    一魚多吃 用 二分搜尋 來尋找相對最大值 Find Peak Element Leetcode #162_精選75題解題目會給定一個陣列,裡面有大有小,可以把數字的大小類比成高度 要求我們找出陣列裡面的相對極大值(relative max value)所在的陣列索引 也就是說當下這個元素,大於左邊鄰居的元素值,也大於右邊鄰居的元素值
    Thumbnail
    avatar
    小松鼠
    2023-09-27
    【LeetCode】19.Remove Nth Node From End of ListInput: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
    Thumbnail
    avatar
    2021-10-09
    【LeetCode】189. Rotate Array之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
    Thumbnail
    avatar
    2021-10-08
    【LeetCode】557. Reverse Words in a String III今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
    Thumbnail
    avatar
    2021-10-08
    【LeetCode】344. Reverse String今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
    Thumbnail
    avatar
    2021-10-08
    【LeetCode】283. Move Zeroes題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
    Thumbnail
    avatar
    2021-10-07
    【LeetCode】704. Binary SearchLeetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
    Thumbnail
    avatar
    2021-10-05
    【LeetCode】20. Valid Parentheses 前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
    Thumbnail
    avatar
    2021-10-04