[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 的其他內容
    題目 : 69. Sqrt(x)
    題目 : 35. Search Insert Position
    題目 : 28. Find the Index of the First Occurrence in a String
    題目:66. Plus One
    題目 : 9. Palindrome Number
    題目 : 69. Sqrt(x)
    題目 : 35. Search Insert Position
    題目 : 28. Find the Index of the First Occurrence in a String
    題目:66. Plus One
    題目 : 9. Palindrome Number
    你可能也想看
    Thumbnail
    作家 Morgan Housel 在《華爾街日報》中提出一個觀點:我們是用理解知識的方式在思考、學習金錢觀,而不是用理解心智與行為模式的方式學習理財。這段話讓我意識到,我們的財務決策並非總是理性的,情緒因素也扮演著重要角色。通過掌握金錢心理學,能夠幫助我們改善用錢習慣,使財務決策更為理性和長遠。
    Thumbnail
    重要知識點: 1. TypeScript 全局擴展,使所有陣列都能使用 groupBy 方法。 2. 利用泛型創建彈性函數,提高代碼可重用性。 3. 迭代陣列中的元素,實現遍歷和處理功能。 4. 物件的鍵值對操作,用於建立以函數輸出為鍵的物件。
    Thumbnail
    雖然表面是一道移除元素的題目,但實際上考的還是搬移。 題目要求的是把val目標值移除,等價於 把非目標值的搬到前面,並且回傳這些剩下來的元素個數(也就是 陣列裡面,非目標值的元素總數​)。 請看下方範例,會更好理解。
    Thumbnail
    題目會給定一個陣列,裡面有大有小,可以把數字的大小類比成高度 要求我們找出陣列裡面的相對極大值(relative max value)所在的陣列索引 也就是說當下這個元素,大於左邊鄰居的元素值,也大於右邊鄰居的元素值
    Thumbnail
    之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
    Thumbnail
    今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
    Thumbnail
    今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
    Thumbnail
    題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
    Thumbnail
    Leetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
    Thumbnail
    前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
    Thumbnail
    作家 Morgan Housel 在《華爾街日報》中提出一個觀點:我們是用理解知識的方式在思考、學習金錢觀,而不是用理解心智與行為模式的方式學習理財。這段話讓我意識到,我們的財務決策並非總是理性的,情緒因素也扮演著重要角色。通過掌握金錢心理學,能夠幫助我們改善用錢習慣,使財務決策更為理性和長遠。
    Thumbnail
    重要知識點: 1. TypeScript 全局擴展,使所有陣列都能使用 groupBy 方法。 2. 利用泛型創建彈性函數,提高代碼可重用性。 3. 迭代陣列中的元素,實現遍歷和處理功能。 4. 物件的鍵值對操作,用於建立以函數輸出為鍵的物件。
    Thumbnail
    雖然表面是一道移除元素的題目,但實際上考的還是搬移。 題目要求的是把val目標值移除,等價於 把非目標值的搬到前面,並且回傳這些剩下來的元素個數(也就是 陣列裡面,非目標值的元素總數​)。 請看下方範例,會更好理解。
    Thumbnail
    題目會給定一個陣列,裡面有大有小,可以把數字的大小類比成高度 要求我們找出陣列裡面的相對極大值(relative max value)所在的陣列索引 也就是說當下這個元素,大於左邊鄰居的元素值,也大於右邊鄰居的元素值
    Thumbnail
    之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
    Thumbnail
    今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
    Thumbnail
    今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
    Thumbnail
    題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
    Thumbnail
    Leetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
    Thumbnail
    前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。