LeetCode Move Zeroes (简单题)
题意:
给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变。
思路:
扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了。
C++
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int idx=;
for(int i=; i<nums.size(); i++)
if(nums[i]!=)
nums[idx++]=nums[i];
while(idx<nums.size())
nums[idx++]=;
}
};
AC代码
python3
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
nums.sort(key=lambda x:0 if x==0 else -1 )
AC代码
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
cnt=nums.count(0)
for i in range(cnt):
nums.remove(0)
nums.append(0)
AC代码
LeetCode Move Zeroes (简单题)的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- [LeetCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode——Move Zeroes
Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...
- 力扣485. 最大连续1的个数-C语言实现-简单题
题目 [题目传送门] 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3 ...
- 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简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
随机推荐
- IOS 完成来电归属地
首先是一个库:(有时间在上传) 然后设置一个工具类 .h @interface HMFoundLocation : NSObject AS_SINGLETON(HMFoundLocation) @pr ...
- IOS 通讯录 右侧的字母栏
http://blog.csdn.net/nicholas6lee/article/details/7633708 Android实现通讯录排序的方式,可借鉴.
- Ansible Playbooks高级使用
文件操作 文件创建 file 用于设置文件/链接/目录的属性,或者删除文件/链接/目录 ### state如果是directory当目录不存在时会自动创建:如果是file当文件不存在时不会自动创建 - ...
- nodebrew
创建: 2019/05/30 完成: 2019/05/30 安装 安装 curl -L git.io/nodebrew | perl - setup 更新nodebrew nodebrew sel ...
- Docker 容器的数据卷
数据卷的特点: 1. 数据卷在容器启动时初始化,如果容器使用的镜像在挂载点包含了数据,这些数据会拷贝到新初始化的数据卷中 2. 数据卷可以在容器之间共享和重用 3. 可以对数据卷里的内容直接进行修改 ...
- hdu2874(lca / tarjan离线 + RMQ在线)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 给出 n 个顶点 m 条边的一个森林, 有 k 个形如 x y 的询问, 输出 x, ...
- 786A(博弈&bfs)
题目链接: http://codeforces.com/problemset/problem/786/A 题意: 一个环形路径编号为1-n,1号点为黑洞,玩家轮流让怪物前进若干步(从自己的操作集合里随 ...
- JSP读取Oracle数据库里的图片Blob字段并显示在页面上
1.java代码: /** * 打印模板获取电子签名 * @param request * @param resp * @param id * @return * @throws Exception ...
- POJ1030 Rating
题目来源:http://poj.org/problem?id=1030 题目大意:有100支队伍(编号1->100),有两场比赛.以下表的形式列出了两场比赛的名次.(有的队伍没有参赛或只参加了一 ...
- 7、python数据类型之集合set
数据类型之集合setset 不允许重复的无序集合,不能通过下标取值,因为无序1.创建 创建空集合 s ={} 默认类型为字典,所以不是空集合,空集合如下 s = set() s = { ...