leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50493772
题目地址:https://leetcode.com/problems/move-zeroes/
Move Zeroes
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.
解法(java):
public class Solution {
public void moveZeroes(int[] nums) {
if (nums != null) {
int length = nums.length;
for (int i = 0, j = 0; i < length; i++) {
if (nums[i] != 0) {
if (i != j) {
nums[j] = nums[i];
nums[i] = 0;
}
j++;
}
}
}
}
}leetcode:283. Move Zeroes(Java)解答的更多相关文章
- 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 ...
- Java [Leetcode 283]Move Zeroes
题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- 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 ...
- [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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- 11. 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 ...
- LeetCode 283 Move Zeroes 解题报告
题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...
- [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 ...
- Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)
题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...
随机推荐
- vs 2012安装ASP.NET MVC5
VS2012能使用MVC5开发,但VS2012不自带MVC5,需要安装“用于 Visual Studio 2012 的 ASP.NET 和 Web 工具 2013.1” 从下面提供的链接下载安装: h ...
- eclipse下svn的分支与合并指南 - 更新版
http://wenku.baidu.com/link?url=ul5vzBHZpHgzENp46RQwTYrkCUYLeVg9TuhmPM_qisR1BGzp6Qca7onhS-SOzwDYuYdA ...
- luogu 1113 杂务--啥?最长路?抱歉,我不会
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- Ahoi2014&Jsoi2014 支线剧情
题目描述 题解: 每条边至少经过一次,说明经过下界为$1$. 然后套有源汇上下界最小费用可行流板子. 口胡一下. 此类问题的建图通式为: 1.假设原来的边流量上下界为$[l,r]$,那么在新图中建流量 ...
- python实现快速排序、冒泡
快速排序:首先任意选取一个数据(通常选用数组的第一个参数)作为关键数据,然后将比它小的数放在它前面,比它大的数放在后面,这个过程称之为快速排序 def quick_sort(l): if len(l) ...
- nginx 配置虚拟机 支持pathinfo
server { server_name shopx.local *.shopx.local; charset utf-8; root /Users/x/www/php/shopx.local/sho ...
- 【Codeforces 1006D】Two Strings Swaps
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 注意只能改变a不能改变b 然后只要让a[i],b[i],a[n-i-1],b[n-i-1]这4个字符能凑成两对.全都一样就可以了 分类讨论下就 ...
- 一个可以获取知乎timeline的爬虫
# -*- coding: utf-8 -*- import requests import lxml import os,time from bs4 import BeautifulSoup as ...
- CentOS7 设置代理
大多数公司的网络都使用局域网加代理上网,也就是说上外网必须使用公司指定的代理服务器,这有几个好处: 1. 首先代理可以一定程度提高浏览速度,因为可以将更多的网页缓存在代理服务器上,需要的时候直接拿就很 ...
- Leetcode 306.累加数
累加数 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 '0'- ...