283. Move Zeroes
来源:互联网 发布:维生素c 知乎 编辑:程序博客网 时间:2024/06/05 05:49
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.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [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
简单的同向双指针问题
java
class Solution { public void moveZeroes(int[] nums) { if (nums == null || nums.length == 0 || nums.length == 1) { return; } int left = 0, right = 0; while (right < nums.length) { if (nums[right] != 0) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; } right++; } }}
python
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ if nums == None or len(nums) == 0: return left, right = 0, 0 while right < len(nums): if nums[right] != 0: nums[left], nums[right] = nums[right], nums[left] left += 1 right += 1
阅读全文
0 0
- 283.Move Zeroes
- 283. Move Zeroes
- 283. Move Zeroes
- 283. Move Zeroes
- 283. Move Zeroes
- 【LeetCode】283. Move Zeroes
- 283. Move Zeroes
- 283. Move Zeroes
- Leetcode 283. Move Zeroes
- 283. Move Zeroes
- LeetCode #283. Move Zeroes
- 283. Move Zeroes LeetCode
- 283. Move Zeroes
- LeetCode 283. Move Zeroes
- LeetCode 283. Move Zeroes
- 283. Move Zeroes
- 283. Move Zeroes
- LeetCode 283. Move Zeroes
- 自学git 常用git(一)
- 如何在 Azure 中的经典 Windows 虚拟机上设置终结点
- 商品列表页面(模板)
- Leetcode题解-24. Swap Nodes in Pairs
- 解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
- 283. Move Zeroes
- Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.google.android.webview-2/li
- Linux安装配置memcached
- centos7开机后进入命令行模式
- CNN入门paper
- python 基础
- select2下拉搜索框匹配汉字全拼及首字母
- Oracle实现自动删除大数据量表7天前的数据
- 连接池改造JDBC工具类