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 ...
随机推荐
- Appium-入门实例1
参考:(https://blog.csdn.net/zh175578809/article/details/76862590) 第一步:启动虚拟设备 在运行App之前,首先需要创建一个Android模 ...
- linux挂载磁盘以及扩容主分区
新买的服务器,如果系统安装操作不当,可能会由于系统主分区过小,后期安装软件过多就会导致软件无法正常运行的问题,如果不做系统格式化,就需要通过购买新的硬盘来进行挂载和扩容主分区以解决问题.本文主要介绍l ...
- 反弹,反向穿越防火墙,NAT网关等乱七八糟的术语
反弹,反向穿越防火墙,反向穿越NAT网关等一切与反向有关的网络活动,往往都是指同一回事情. 以前的木马都是客户端(攻击者)控制服务器(用户),把配置好的服务器发给用户来攻击,叫做正向. 现在的木马代码 ...
- vue--》分页效果(前端实现)
<template> <div> <el-table style="width: 100%;" :data="ary"> & ...
- 第四周课程总结&试验报告
实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...
- [c#]Extesion method
做一个类似Vb.net里面with的方法 public static void Use<T>(this T item, Action<T> work) { work(item) ...
- [Web 前端] 020 css 定位之绑定定位
绑定定位 少废话,上例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Web API入门二(实例)
学习编程的最好方法就是实例,本人用的是VS2015 1.创建ASP.NET Web空项目 点击确定后即创建了空"WebApi"项目 2.下面,我们需要使用NuGet包管理器添加最新 ...
- Vue2+VueRouter2+webpack 构建项目实战(四)接通api,先渲染个列表
Vue2+VueRouter2+webpack 构建项目实战(四)接通api,先渲染个列表: Vue2+VueRouter2+Webpack+Axios 构建项目实战2017重制版(一)基础知识概述
- spring data solr 搜索关键字高亮显示
spring data solr 搜索关键字高亮显示 public Map<String, Object> highSearch(Map searchMap) { Map map = ne ...