LeetCode Array Easy 283. Move Zeroes
Given an array
nums
, write a function to move all0
's to the end of it while maintaining the relative order of the non-zero elements.Example:
Input: [,,,,]
Output: [,,,,]Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
问题描述:给一个数组,将数组中所有的0移动到数组的尾部,而不改变其他非零元素的相对位置
个人思路:
1.暴力解法。遍历整个数组,当找到0时,开始从0的下一位去找非零元素,找到后交换两个元素。
public void MoveZeroes(int[] nums) {
for(int i = ; i < nums.Length; i++){
if(nums[i]==){
for(int j = i +; j < nums.Length; j++){
if(nums[j] != ){
nums[i]=nums[j];
nums[j]=;
break;
}
}
}
}
}
第二种解法:设定一个指针i=0,遍历数组,如果当前值不为零 则交换 i j 的元素,
public void MoveZeroes(int[] nums) {
if(nums.Length == )
return;
int i =;
for(int j =; j < nums.Length; j++){
if(nums[j] != ){
Swap(nums, i, j);
i++;
}
}
while(i < nums.Length){
nums[i++]=;
}
}
private void Swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j]=temp;
}
LeetCode Array Easy 283. Move Zeroes的更多相关文章
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- [LeetCode&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- 283. Move Zeroes@python
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
随机推荐
- k8s和docker区别
简要介绍: docker是一个开源的应用容器引擎,开发者可以打包他们的应用以及依赖到一个容器中,发布到流行的liunx系统上,或者实现虚拟化. k8s是一个开源的容器集群管理系统,可以实现容器集群的自 ...
- 【leetcode】820. Short Encoding of Words
题目如下: 解题思路:本题考查就是找出一个单词是不是另外一个单词的后缀,如果是的话,就可以Short Encode.所以,我们可以把words中每个单词倒置后排序,然后遍历数组,每个元素只要和其后面相 ...
- selenium环境搭建,浏览器驱动安装
一安装Python: 1.下载Phtyon地址:https://www.python.org/getit/ 2.安装python会默认安装两个基础包setuptools,pip 也可以手动安装: ...
- 【Dart学习】--之Runes与Symbols相关方法总结
一,Runes 在Dart中,Runes代表字符串的UTF-32字符集, 另一种Strings Unicode为每一个字符.标点符号.表情符号等都定义了 一个唯一的数值 由于Dart字符串是UTF-1 ...
- C#_winform登陆框验证码的实现
验证码技术已愈来愈成熟,从最初的数字.字母.字符.汉字已经到目前的语言,其应用也甚广,之前大多数只有在网站上可以看到,现在在一些客户端软件也经常可见(比如证券相关软件).之前做的一个基于 C# 客户端 ...
- Sumdiv
题目链接 题意:求a^b的所有约数之和mod9901. 思路:因为一个数A能够表示成多个素数的幂相乘的形式.即A=(a1^n1)*(a2^n2)*(a3^n3)...(am^nm).所以这个题就是要求 ...
- python 对 excel 的操作
参考:https://www.php.cn/python-tutorials-422881.html 或 https://blog.51cto.com/wangfeng7399/2339556(使用 ...
- [CSP-S模拟测试]:v(hash表+期望DP)
题目背景 $\frac{1}{4}$遇到了一道水题,又完全不会做,于是去请教小$D$.小$D$看了$0.607$眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{ ...
- Spring Boot学习一之Spring Beans和依赖注入
你可以自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖.简单起见,我们经常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入. 如 ...
- python中nonlocal 的作用域
''' nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量. ''' def work(): x = 0 def new_work(): nonlocal x x=x+3 retur ...