LintCode之移动零
题目描述:
分析:由于要使非零元素保持原数组的顺序,我只能想出在找到一个0时,逐个移动数组元素使得后一个元素覆盖前一个元素,再将这个0移到后头去。
我的代码:
public class Solution {
/*
* @param nums: an integer array
* @return:
*/
public void moveZeroes(int[] nums) {
// write your code here
//当数组为空时直接返回
if(nums.length == 0) {
return;
}
boolean b = true;
//判断数组是否全为0,若是,则直接返回
for(int i=0; i<nums.length; i++) {
if(nums[i] != 0) {
b = false;
}
}
if(b == true) {
return ;
} int k = nums.length-1;
int i = k;
while(i >= 0) {
//从后往前找元素值为0的数
while(i>=0 && nums[i]!=0) {
i--;
}
if(i >= 0) {
int temp = nums[i];
for(int j=i; j<k; j++) {
nums[j] = nums[j+1];
}
nums[k] = temp;
k--;
}
}
}
}
LintCode之移动零的更多相关文章
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- lintcode:移动零
题目 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数 样例 给出 nums = [0, 1, 0, 3, 1 ...
- [LintCode] 尾部的零
class Solution { public: // param n : description of n // return: description of return long long tr ...
- 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] 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 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
随机推荐
- Java ——String 类 StringBuffer 和 StringBuilder类 随机字符
本节重点思维导图 String 类 创建字符串 String str = "I love ai"; 在代码中遇到字符串常量时,"I love ai",编译器会使 ...
- 【ABAP系列】SAP ABAP ALV合计或者小计 添加自定义文本
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP ALV合计或者小计 ...
- 【Unity Shader】---入门知识点
着色器声明(“名字”)Shader "ShaderDiffuseExample" { 一.属性定义(作用:外部传入参数) 属性定义语法:PropName("Display ...
- Node.js实战11:fs模块初探。
fs模块封装了对文件操作的各种方法,比如同步和异步读写.批量操作.流.监听. 我们还是通常例程学习, 获取目录下的文件清单: var fs =require("fs"); fs.r ...
- [Git] 004 初识 Git 与 GitHub 之查看历史记录
在 GitHub 的 UI 界面使用 Git 查看历史纪录 1. 右侧有个 history 2. 点击后跳转页面 3. 点击相应标题(commit 时写的),进入相应版本(历史) 4. 我选择了 I ...
- JDK8 parallelStream性能测试
https://blog.csdn.net/u011870280/article/details/80700993 public static void main(String[] args) {lo ...
- [Bzoj2004][Hnoi2010]Bus 公交线路(状压dp&&矩阵加速)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 看了很多大佬的博客才理解了这道题,菜到安详QAQ 在不考虑优化的情况下,先推$dp ...
- C# EF优化
原文:https://www.cnblogs.com/wangyuliang/p/10338902.html https://www.cnblogs.com/simadi/p/6879366.ht ...
- ECarts 的初步使用
ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...
- k3 cloud出现应收单下推收款单,把收款单是结算方式修改成银行承兑汇票之后保存提示:收款单明细中结算方式为票据业务的实收金额之和不等于票据的当前占用金额之和,请检查数据!
收款单结算方式选择[银行承兑汇票],系统就默认该笔业务在系统中要存在一张应收票据.则在应收票据页签,需要选择一张出纳的应收票据(要先存在应收票据,才能保存单据,相当于是根据这张票据审核生成的这张收单单 ...