[leetcode]283. 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.
Input: [0,1,0,3,12]
Output: [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.
题意:
将数组中,一旦有 0 元素, 统统拖到数组末尾。
思路:
两个指针base, i 从index为0的位置出发。
指针 i 用来扫数组,若 i 对应的元素非 0 时,
直接将指针 i 对应元素赋值给指针 base 对应元素,同时 base++。
这样,指针 i 遍历完毕数组,指针 base 也将所有非0元素保存在数组了 index [0 ~ i] 的位置
最后将数组 index ( i ~ nums.length ) 的后半部分都赋值为 0。
代码:
class Solution {
public void moveZeroes(int[] nums) {
int base = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
nums[base] = nums[i];
base++;
}
}
for(int i = base; i< nums.length; i++){
nums[i] = 0;
}
}
}
[leetcode]283. Move Zeroes移零的更多相关文章
- [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 ...
- 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 ...
- 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] ...
- 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 ...
- 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 -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- LeetCode 283 Move Zeroes(移动全部的零元素)
翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...
- 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 ...
随机推荐
- Mac parallels desktop安装windows,linux
前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件 ...
- Session的使用与Session的生命周期
1.HttpSession的方法 Object getAttribute(String); Enumeration<String> getAttributeNames(); long ge ...
- Windows10 命令行中使用网络驱动器
Windows10中,我们在局域网内使用共享文件夹,建立映射的网络驱动器,有时候需要一些软件去调用网络驱动器内的资源,但是发现在资源管理器能正常打开,应用软件却无法识别,命令行中提示:“系统找不到指定 ...
- python 之路06day
一 字符编码 1 字符编码的定义: 计算机要想工作必须通电,即用‘电’驱使计算机干活,也就是说‘电’的特性决定了计算机的特性.电的特性即高低电平(人类从逻辑上将二进制数1对应高电平,二进制数0 ...
- 可视化学习Tensorboard
可视化学习Tensorboard TensorBoard 涉及到的运算,通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算.为了更方便 TensorFlow 程序的理解.调试与优化,发布了一 ...
- hive 锁
HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...
- leetcode485
public class Solution { public int FindMaxConsecutiveOnes(int[] nums) { ; ; ; i < nums.Length; i+ ...
- HTML5拖动
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- IIS 更新EXE文件
IIS 更新EXE文件 MIME,add,文件扩展名带不带.都可以,会自动加上.的 文件扩展名:.exe MIME类型:application/octet-stream .ini文件
- Activity服务类-6 ManagementService服务类
一共含有17个方法 // 获取包含了Activiti数据库模式的{表名.行计数}项的映射.Map<String, Long> getTableCount();//获取诸如任务.执行之类的A ...