283. Move Zeroes - LeetCode
Question

Solution
题目大意:将0移到最后
思路:
1. 数组复制
2. 不用数组复制
Java实现:
数组复制
public void moveZeroes(int[] nums) {
int[] arr = Arrays.copyOf(nums, nums.length);
int start = 0;
int end = nums.length - 1;
for (int i=0; i<arr.length; i++) {
int tmp = arr[i];
if (tmp == 0) {
nums[end--] = 0;
} else {
nums[start++] = tmp;
}
}
}
不用数组复制
public void moveZeroes(int[] nums) {
int start = 0;
int zeroCount = 0;
for (int i=0; i<nums.length; i++) {
int tmp = nums[i];
if (tmp == 0) {
zeroCount++;
} else {
nums[start++] = tmp;
}
}
for (int i = 0; i < zeroCount; i++) {
nums[nums.length - 1 - i] = 0;
}
}
283. Move Zeroes - LeetCode的更多相关文章
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 283. Move Zeroes@python
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 (移动零)
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 -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
随机推荐
- Leetcode刷题之矩阵中的指针用法
矩阵中的指针用法 1 快慢指针 Leetcode27移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...
- (stm32学习总结)—LCD—液晶显示
显示器简介 显示器属于计算机的 I/O 设备,即输入输出设备.它是一种将特定电子信息输出到屏幕上再反射到人眼的显示工具.常见的有 CRT 显示器.液晶显示器.LED 点阵显示器及OLED 显示器 本章 ...
- C++大作业——教职工管理系统
教职工信息管理系统 1.问题描述: 设计一个学校职工管理系统,要求实现如下功能:建立职工信息数据, 包括职工编号.姓名. 性别.工资.出生时间.岗位.参加工作时间和年 龄(必须计算得到),初始模拟数据 ...
- 用Canvas画一棵二叉树
笔墨伺候 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); // 然后便可以挥毫泼墨 ...
- 如何形成一个完整的HTML对象
写在前面,本文将同步发布于Blog.掘金.segmentfault.知乎等处,如果本文对你有帮助,记得为我得到我的个人技术博客项目给个star哦. 为何写这篇文章? 你可能做Web开发已经有一段时间, ...
- hdfs对文件的增删改查
源代码: pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- SpringBoot 项目搭建(详细介绍+案例源码)
SpringBoot 项目搭建 SpringBoot 项目整合源码 SpringBoot 项目整合 一.项目准备 1.1 快速创建 SpringBoot 项目 1.2 标准项目结构图如下 1.3 添加 ...
- 从零搭建react开发环境
早在六年前,前端开发已经实现了模块化.工程化开发,既然是模块化工程化开发那就少不了包管理工具,所以我们的第一步就是先从安装nodejs开始(安装nodejs携带JavaScript的包管理工具npm) ...
- Java报错:Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run (default-cli) on project ssm-mybatis-plus: Failure
修改一下端口就好了,不要用80端口. <plugin> <groupId>org.eclipse.jetty</groupId> <!--嵌入式Jetty的M ...
- Python入门-正则表达式
正则匹配函数 # 需要先导入re模块 import re #字符串,匹配查找 info = "www baidu com" print("=======字符串自带find ...