【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/move-zeroes/
Total Accepted: 77443 Total Submissions: 175420 Difficulty: Easy
题目描述
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.
Example:
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换到数组的结尾去,同时需要保证其他数字的位置不能改变。另外需要在原地进行操作。
解题方法
in-place的把0放到数组的最后,其他顺序不变。用两个指针。
方法一:首尾指针
两个指针,一个指向开始,一个指向结尾。开始这个判断是否为零,如果是零,就把其余的数据往前排,零放到末尾,开始和结尾指针向中间移动。开始这个不是零的话,就开始指针后移,结尾不变。直到两个指针重合为止。
算法效率较低,原因在于所有的数据往前移动,需要耗时。
public class Solution {
public void moveZeroes(int[] nums) {
int start=0;
int end=nums.length-1;
while(start!=end){
if(nums[start]==0){
for(int j=start+1;j<=end;j++){
nums[j-1]=nums[j];
}
nums[end]=0;
end--;
}else{
start++;
}
}
}
}
AC:21ms
方法二:头部双指针+双循环
二刷的Python版本,使用两个指针,找到下个不为0的位置,然后和当前的0进行交换。
时间复杂度是O(N^2),空间复杂度是O(1)。
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
N = len(nums)
for i in range(N):
if nums[i] != 0:
continue
j = i + 1
while j < N:
if nums[j] == 0:
j += 1
continue
nums[i], nums[j] = nums[j], nums[i]
j += 1
break
if j == N:
break
方法三:指针指向第一个0的位置
使用两个指针遍历数组,一个指向数值为0的元素,另一个指向数值不为0的元素,在遍历的过程中,不断交换两个指针的值。
这个比我的算法一简单,因为不需要把所有的数据进行搬移,只需要搬移正的数据。
public class Solution {
public void moveZeroes(int[] nums) {
int zero = 0;
while (zero < nums.length && nums[zero] != 0) {
zero++;
}
int non_zero = zero + 1;//非零的数值开始的位置应该在零值的后头,这样交换才有意义
while (non_zero < nums.length && nums[non_zero] == 0) {
non_zero++;
}
while (zero < nums.length && non_zero < nums.length) {
int temp = nums[zero];
nums[zero] = nums[non_zero];
nums[non_zero] = temp;
while (nums[non_zero] == 0) {//寻找下一个非零数值
non_zero++;
if (non_zero >= nums.length) {
return;//找不到说明已经排好序了
}
}
zero++;
}
}
}
AC:1ms
这个算法竟然写了一个小时。。崩溃。主要注意while里边进行判断,不要溢出。
二刷的时候写了个python版本的这个代码,简洁的多了。
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
N = len(nums)
i = 0
for j in range(N):
if nums[j] != 0:
nums[i], nums[j] = nums[j], nums[i]
i += 1
日期
2016/4/30 0:17:23
2018 年 11 月 10 日 —— 这么快就到双十一了??
【LeetCode】283. Move Zeroes 解题报告(Java & Python)的更多相关文章
- 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 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 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】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
随机推荐
- Matlab 调用 Python 脚本
Matlab 调用 Python 脚本 最近尝试在 Matlab 环境中调用 Python 脚本,这里总结下碰到的几个问题. 1. Python 模块加载 在 Matlab 函数中,想要将 Pytho ...
- Docker 外部访问容器Pp、数据管理volume、网络network 介绍
Docker 外部访问容器Pp.数据管理volume.网络network 介绍 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来 指定端口映射. ...
- 解决sourceforge下载文件慢的方法
Sourceforge是一些开源软件经常用到的网站,然而国内的网站一直不稳定,如今是可以访问,但是一直无法下载,或者是下载速度慢,导致下载中断 镜像源:http://sourceforge.mirro ...
- 【Java 泛型】之 <? super T> 和<? extends T> 中 super ,extends如何理解?有何异同?
Java 泛型 <? super T> 和<? extendsT>中 super ,extends怎么 理解?有何不同? 简介 前两篇文章介绍了泛型的基本用法.类型擦除以及泛型 ...
- python web框架学习笔记
一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket def handle_request(cli ...
- 单元测试(Jest 和 Mocha)
Vue CLI 拥有通过 Jest 或 Mocha 进行单元测试的内置选项. Jest 是功能最全的测试运行器.它所需的配置是最少的,默认安装了 JSDOM,内置断言且命令行的用户体验非常好.不过你需 ...
- 使用CORS处理跨域请求
package com.leyou.gateway.config;import org.springframework.context.annotation.Bean;import org.sprin ...
- 【MySQL】统计累计求和
https://geek-docs.com/sql/sql-examples/sql-cumulative-sum.html
- windows 显示引用账户已被锁定,且可能无法登录
今天遇到一个比较尴尬的事情,清理笔记本键盘时,在锁屏界面多次碰到enter键,在登录界面被锁定无法登录. 一开始慌了,因为没遇到过这样的问题.百度一看方法不少,便开始尝试, 有的说是重启进入安全模式, ...
- 安全刻不容缓「GitHub 热点速览 v.21.50」
作者:HelloGitHub-小鱼干 本周最热的事件莫过于 Log4j 漏洞,攻击者仅需向目标输入一段代码,不需要用户执行任何多余操作即可触发该漏洞,使攻击者可以远程控制用户受害者服务器,90% 以上 ...