leetcode27_Remove Element
一.問題描述
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3]
, val = 3
Your function should return length = 2, with the first two elements of nums being 2.
【注:個人認為本題最關鍵的資訊是 in place, 也就是要原地對陣列中的某元素,並返回移除後的新陣列的長度。也就是給定的陣列nums = [3,2,2,3],及元素val=3,經過程式處理後要變成nums = [2,2,x,x],並返回len=2】二.演算法實現
理解好了題意之後,演算法編寫還算簡單。程式碼如下:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
i = 0; j = 0
while j<len(nums):
if nums[j] == val:
j += 1
else:
nums[i] =nums[j]
i += 1
j += 1
return i
相關文章
- CSS選擇器筆記,element element和element > element 的區別CSS筆記
- Remove ElementREM
- Remove Element 解答REM
- presence_of_element_located對比visibility_of_element_located
- ? Element UI 新春快報UI
- jquery judge element existjQuery
- 169 Majority element
- Widget、RenderObject 與 ElementObject
- vue引入element-uiVueUI
- Flutter -- Element生命週期Flutter
- 深入前端之replaced element前端
- element-ui基本使用UI
- element-ui坑點UI
- jQuery element選擇器jQuery
- leetcode Remove ElementLeetCodeREM
- vue-element-admin 入坑記(一)vue-element-admin 中文Vue
- vue中使用element2Vue
- Vue Element校驗validateVue
- element-ui匯出表格UI
- [leetcode]remove-elementLeetCodeREM
- vue中使用element-UIVueUI
- Element 文件中的 Markdown 解析
- element-ui 匯出excelUIExcel
- Element theme-chalk 分析
- ? Element UI for Vue 3.0 來了!UIVue
- vue中Element-ui引入VueUI
- vue 之 element-ui 示例VueUI
- js: get event handler bound to the elementJS
- Leetcode-Remove ElementLeetCodeREM
- Remove Element leetcode javaREMLeetCodeJava
- Find Peak element leetcodeLeetCode
- Vue 3.0 + Element-Plus + RuoyiVue
- element-ui匯出excel表格,程式碼基於vue-element-admin-masterUIExcelVueAST
- spring boot + vue + element-ui全棧開發入門——整合element-uiSpring BootVueUI全棧
- 深入淺出 Flutter Framework 之 ElementFlutterFramework
- 思路:element 表格元件自適應元件
- ThinkPHP6、Vue、Element、Swoole、RedisPHPVueRedis
- ThinkPHP6、Vue、Element、Swoole、Redis、PHPVueRedis