Question

283. Move Zeroes

Solution

题目大意:将0移到最后

思路:

1. 数组复制

2. 不用数组复制

Java实现:

数组复制

public void moveZeroes(int[] nums) {
int[] arr = Arrays.copyOf(nums, nums.length);
int start = 0;
int end = nums.length - 1;
for (int i=0; i<arr.length; i++) {
int tmp = arr[i];
if (tmp == 0) {
nums[end--] = 0;
} else {
nums[start++] = tmp;
}
}
}

不用数组复制

public void moveZeroes(int[] nums) {
int start = 0;
int zeroCount = 0;
for (int i=0; i<nums.length; i++) {
int tmp = nums[i];
if (tmp == 0) {
zeroCount++;
} else {
nums[start++] = tmp;
}
}
for (int i = 0; i < zeroCount; i++) {
nums[nums.length - 1 - i] = 0;
}
}

283. Move Zeroes - LeetCode的更多相关文章

  1. 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 ...

  2. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  7. 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 ...

  8. LeetCode 283. Move Zeroes (移动零)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  9. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

随机推荐

  1. SpringAop实现原理及代理模式

    Spring Aop的原理 Spring的AOP就是通过动态代理实现的.当为某个Bean或者某些Bean配置切面时,Spring会为其创建代理对象,当调用该对象的某个方法时,实际是调用生成的代理类的对 ...

  2. expression:_CrtlsValidHeapPointer

    详见stackoverflow "_CrtIsValidHeapPointerUserData means, that you have a heap corruption, which i ...

  3. 15_伯德图,为什么是20logM?分贝又是什么?_Bode Plot_Part1

  4. 一个用于学习的react项目

    React-element 根据开源项目 vue-sell进行的开发,将其改造成了react的项目.在开始学习vue的时候就是用的这个项目,发现效果不错,所以在学习React也使用了此项目. 目的:将 ...

  5. javascript 理解和使用回调函数

    在javascript中,function是内置的类对象,也就是说它是一种类型的对象,可以和其他String.Array.Number.Objec类的对象一样用于内置对象的管理.因为function实 ...

  6. Qunee for HTML5 v1.6新版本发布

    Qunee for HTML5 V1.6正式发布,修复了一些 BUG,增加了滚动条支持,改进了编辑器,增加了JSON 导入导出.告警冒泡.连线流动,UI 定制等扩展示例,欢迎 访问 导航面板 增加了滚 ...

  7. React系列——websocket群聊系统在react的实现

    前奏 这篇文章仅对不熟悉在react中使用socket.io的人.以及websocket入门者有帮助. 下面这个动态图展示的聊天系统是用react+express+websocket搭建的,很模糊吧, ...

  8. 解决vue-cli项目在运行时控制台出现 [WDS] Disconnected! 错误

    在项目运行时 控制台输入 npm run dev 后浏览器出现该项目页面,但是出现了[WDS] Disconnected!错误 虽然有时并不影响,但是作为一名合格的程序员 我们还是尽量将其修复,以免将 ...

  9. css过渡效果和盒子缩放大小的结合

    给盒子一个鼠标经过时放大里面的图片效果在css中使用过渡效果transition结合 <html lang="en"> <head> <meta ch ...

  10. 如何科学衡量广告投放效果?HMS Core分析服务助您科学归因

    日益多元化的广告形式以及投放成本的不断攀升,让广告主们更加关注每一次广告投放带来的实际价值. 然而,广告主一般仅能从平台获得展示.点击.下载等前端效果字段,实际的用户注册.激活等后端深度转化指标并无法 ...