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 ...
随机推荐
- 前端学习(三十九)移动端app(笔记)
移动端App 开发App的三种方式 Native App 原生 底层语言 java Android oc ...
- CentOS 7 LNMP环境搭建 Zabbix3.4
概述:在CentOS 7 64位操作系统环境下搭建LNMP(Linux+Nginx+MySQL+PHP)来运行Zabbix 3.4 监控程序 预先安装: yum install -y autoconf ...
- MetaException(message:For direct MetaStore DB connections, we don't support retries at the client level.)
在mysql中执行以下命令: drop database hive; create database hive; alter database hive character set latin1 ...
- HTML5 arc的例子
demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...
- Hadoop中的排序和连接
MapReduce的全排序 主要是为了保证分区排序,即第一个分区的最后一个Key值小于第二个分区的第一个Key值 与普通的排序仅仅多一个自定义分区类MyPartitioner见自己所写的实验 (设置一 ...
- 【leetcode】722. Remove Comments
题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...
- 如何隐藏一个让人很难发现的bug?
程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含bug. 那么如何写出一个让(坑)人(王)很(之)难(王)发现的bug呢? - 1 - 新手开发 ...
- vue循环渲染变量类样式
由于需求的需要,将五种不同的颜色样式通过v-for进行遍历渲染,所以我这里采用绑定类函数进行判断方式.代码: 效果: 灵感来自:https://www.jianshu.com/p/33e181be3d ...
- window安装nodejs
nvm管理nodejs 原文: https://www.cnblogs.com/shimily/articles/7244058.html1.下载nvm(nodejs版本管理工具) https://g ...
- [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)
题目背景 $Billions\ of\ lighthouses...stuck\ at\ the\ far\ end\ of\ the\ sky.$ 题目描述 平面有$n$个灯塔,初始时两两之间可以相 ...