[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 ...
随机推荐
- 操他妈的,jquery1.4以上不能用toggle()轮流切换函数
query 1.9里面已经删除了toggle(fn1, fn2)函数 (2013-05-07 13:44:27) 转载▼ 标签: it 分类: js jquery 1.9里面已经删除了toggle(f ...
- python 模块被引用多次但是里面的全局表达式总共只会执行一次
python 模块被引用多次但是里面的全局表达式总共只会执行一次
- Oracle跨库复制表结构
1.首先建立远程连接 create public database link LINK_SJPSconnect to system identified by manager using '(DESC ...
- Solr学习总结(六)solr的函数查询Function Queries
摘要: 函数查询允许你使用一个或多个数字字段的真实值生成一个相关性分数,函数查询在standard,DisMax,eDisMax下都能使用. 查询函数可以是常量,字段或者其他函数的组合.使用函数可以影 ...
- mysql更新(五) 完整性约束 外键的变种 三种关系 数据的增删改
11-数据的增删改 本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识. ...
- JavaScript函数及作用域
知识内容: 1.JavaScript函数 2.JavaScript全局函数及特殊函数 3.JavaScript作用域 4.本节练习 参考资料:<JavaScript高级程序设计> 一.Ja ...
- div+css样式命名规则,值得收藏
div+css样式命名规则,值得收藏 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:w ...
- 数据分析利器之hive优化十大原则
hive之于数据民工,就如同锄头之于农民伯伯.hive用的好,才能从地里(数据库)里挖出更多的数据来. 用过hive的朋友,我想或多或少都有类似的经历:一天下来,没跑几次hive,就到下班时间了. h ...
- Zabbix结合Grafana绘图
参考网站: https://www.jianshu.com/p/6eec985c5c94 注意事项: 1.在ganafa添加datasource的时候url写成http://10.116.33.116 ...
- leetcode455
public class Solution { public int FindContentChildren(int[] g, int[] s) { var listg = g.OrderBy(x = ...