LeetCode283:Move Zeros
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.
主要思路:保持两个指针,保证两个指针之间[zero_bit,nonzero_bit)范围内都是0,然后调换首个0和首个非零
void moveZeroes(int* nums, int numsSize)
{
int zero_bit = ;
int nonzero_bit = ;
while(nonzero_bit<numsSize)
{
if(nums[nonzero_bit] != )
{
if(nonzero_bit != zero_bit)
{
nums[zero_bit++] = nums[nonzero_bit];
nums[nonzero_bit] = ;
}else{
++zero_bit;
}
}
++nonzero_bit;
}
}
LeetCode283:Move Zeros的更多相关文章
- [LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- Leetcode Move Zeros
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode 283 Move Zeros
Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...
- Leetcode-283 Move Zeroes
#283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- leetcode-easy-array-283 move zeros
mycode 77.24% class Solution(object): def moveZeroes(self, nums): """ :type nums: Li ...
- [刷题] 283 Move Zeros
要求 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 实现 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostr ...
- LeetCode 283
Move Zeros Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- LeetCode Animation 题目图解汇总(持续更新中...)
我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub ...
随机推荐
- js如何判断一个对象是不是Array
typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 var arr=new Array(&quo ...
- XML中对特殊字符的处置
str = str.replaceAll("‘", "‘"); str = str.replaceAll("’", "‘" ...
- 51nod1175 区间中第K大的数
裸的主席树. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm&g ...
- memcached内存管理及key value长度限制
1)什么是内存碎片?内存是大小有限的资源.例如把内存比作一张小床,来了一个小伙伴,可以睡下,再来一个小伙伴也能睡下.现在两个人了,他们点了差不多的大小的位置(资源),位置还有剩下.然后再来一个小胖子, ...
- python处理ajax请求
先要起一个服务 server.py,可以支持python的cgi脚本. #!coding:utf8 from BaseHTTPServer import HTTPServer from CGIHTTP ...
- $^,$@,$?,$<,$(@D),$(@F) of makefile
makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^)常用 ...
- ffmpeg命令学习
1.组成 程序:ffmpeg.ffplay.ffprobe.ffserverffmpeg:转码程序ffplay:播放程序ffserver:服务器程序 库:libavcodec.libavdevice. ...
- php的类型约束
//如下面的类 class MyClass { /** * 测试函数 * 第一个参数必须为 OtherClass 类的一个对象 */ public function test(OtherClass $ ...
- 【英语】Bingo口语笔记(31) - Bring系列
bring up 表示在哪长大 要用被动形式 BYOB 请自带酒瓶
- 【英语】Bingo口语笔记(53) - 口语中不可望文生义的词语