国产探花免费观看_亚洲丰满少妇自慰呻吟_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++;    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东山县| 榆中县| 文水县| 云梦县| 横山县| 江山市| 台北县| 拉萨市| 吴旗县| 应城市| 山东| 南宁市| 三门县| 临漳县| 老河口市| 柘城县| 清原| 辉南县| 石家庄市| 陵川县| 南城县| 隆德县| 浠水县| 方正县| 密山市| 焉耆| 贡山| 缙云县| 惠水县| 新乡市| 台江县| 中山市| 呈贡县| 梁山县| 鄂州市| 阿瓦提县| 遵化市| 溧水县| 莆田市| 江安县| 泌阳县|