lintcode:移动零
题目
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项
1.必须在原数组上操作
2.最小化操作数
给出 nums = [0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].
解题
快速排序思想,以0为界划分
public class Solution {
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
public void moveZeroes(int[] nums) {
// Write your code here
int slow = -1;
int fast = 0;
int n = nums.length;
int x = 0;
while(slow < fast && fast < n){
if(nums[fast]!=x){
slow++;
swap(nums,slow,fast);
}
fast++;
}
}
public void swap(int[] A,int i,int j){
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
稍作更新
public class Solution {
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
public void moveZeroes(int[] nums) {
// Write your code here
int one = 0;
int fast = 0;
int n = nums.length;
int x = 0;
for(int i=0;i<n;i++){
if(nums[i]!=x) { // 不为0 的向前移动
nums[one] = nums[i];
one++;
}
}
for(int i= one;i<n;i++) // 后面的就是0
nums[i] = x;
}
}
lintcode:移动零的更多相关文章
- lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...
- lintcode :Trailing Zeros 尾部的零
题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...
- LintCode #2 尾部的零
计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [LintCode] 尾部的零
class Solution { public: // param n : description of n // return: description of return long long tr ...
- LintCode之移动零
题目描述: 分析:由于要使非零元素保持原数组的顺序,我只能想出在找到一个0时,逐个移动数组元素使得后一个元素覆盖前一个元素,再将这个0移到后头去. 我的代码: public class Solutio ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
随机推荐
- maven学习手记 - 1
学习目标 windows下安装maven环境: 使用命令创建maven项目结构: maven项目编译测试打包安装运行: 在maven项目中使用插件. 在windows下安装maven环境 在win ...
- C++ vector介绍
<span style="font-family: Arial; ">在此总结一下模板vector的使用介绍</span> 标准库vector类型使用需要的 ...
- linux中的文件类型
1.使用ls -l命令可以查看文件的类型和权限 [tansheng@localhost etc]$ ls -l ----------. root root 10月 : gshadow -------- ...
- Log4j的常见用法
对于log4j,一般常用的只要保存如下的内容为属性文件即可: #log4j.rootLogger=DEBUG,A1,R log4j.rootLogger=INFO,A1,R log4j.appende ...
- jsp或Action获取请求参数中文乱码
普通情况下,中文字符会被自动转换成iso-8859-1的编码格式通过网络传输,而这种格式是没办法直接表示出我们认识的中文字符的,所以还要手动将他转换回之前的字符集. 一般在servlet或者actio ...
- 读书笔记:<我是一只IT小小鸟>
<我是一只IT小小鸟>第一次听到这本书的时候,我便有了深深的好奇,虽然我是一名学习软件工程的大学生,但是还是第一次听到“IT”这个名词,既陌生又好奇.听到老师提起了这本书的意义以及看法,我 ...
- 发送Http Get和Post请求
发送Get请求 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; r ...
- Windows下配置使用MemCached
工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manager win下的Mem ...
- 【BZOJ】【2480】【SPOJ 3105】Mod
扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...
- Winform DataGridView扩展
public class ObjectPropertyCompare<T> : IComparer<T> { private readonly PropertyDescript ...