国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Move Zeroes

2019-11-08 20:18:05
字體:
來源:轉載
供稿:網友
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function,nums should be [1, 3, 12, 0, 0].

Note:

You must do this in-place without making a copy of the array.Minimize the total number of Operations.
class Solution(object):    def moveZeroes(self, nums):        """        :type nums: List[int]        :rtype: void Do not return anything, modify nums in-place instead.        """        i = 0        while i<len(nums):  #find the first zero            if nums[i] == 0:                break            else:                i += 1                        j = i+1;        while j < len(nums):    #find the first none zero            if nums[j] != 0:                break            else:                j += 1                while(j<len(nums)):            nums[i] = nums[j]             nums[j] = 0            j += 1            i += 1            while j<len(nums) and nums[j] == 0:                j += 1            while i<len(nums) and nums[i] != 0:                i += 1
void moveZeroes(vector<int>& nums) {    int last = 0, cur = 0;        while(cur < nums.size()) {        if(nums[cur] != 0) {            swap(nums[last], nums[cur]);            last++;        }                cur++;    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 醴陵市| 安丘市| 邢台县| 安泽县| 拉孜县| 达拉特旗| 饶平县| 华容县| 民权县| 锡林郭勒盟| 政和县| 五常市| 成都市| 循化| 辽中县| 海盐县| 金山区| 海南省| 南乐县| 江口县| 怀宁县| 鄂伦春自治旗| 察雅县| 澄迈县| 永仁县| 兴山县| 桃江县| 舟曲县| 高雄市| 团风县| 承德市| 安平县| 霍城县| 盐边县| 格尔木市| 富民县| 任丘市| 清河县| 嘉义市| 江永县| 景宁|