283:Move Zeroes

来源:互联网 发布:简单java单线程程序 编辑:程序博客网 时间:2024/05/22 07:50

题目链接:https://leetcode.com/problems/move-zeroes/

方法:无

思路:最近leetcode挺火的,决定上手试一下,leetcode只需要写函数,不需要整体实现,但是其实这样限制更多,难度也会更高,这道题是第一道题,难度也不算大,直接上c++和python的代码吧。

难点:无

c++

class Solution {public:    void moveZeroes(vector<int>& nums) {        for(int i = 0,j = 0;i < nums.size();i++)        {            if(nums[i] != 0)                swap(nums[i],nums[j++]);        }    }};

python

class Solution(object):    def moveZeroes(self, nums):        import copy        L = copy.deepcopy(nums)        del nums[:]        counter = 0        m = len(L)        for i in range(m):            if L[i] != 0:                nums.append(L[i])                counter += 1        for i in range(m-counter):                nums.append(0)


0 0
原创粉丝点击