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 ...
随机推荐
- PS2018学习笔记(25-29节)
25-亮度与色阶看懂直方图-part1 # 本节知识点: 灰度模式 明暗对比 明度/对比度命令 直方图 色阶命令 调整图层 # 本节段落表: 了解亮度对比 灰度模式观察明暗 明度/对比度命令 认知对比 ...
- Reboot
目标是将浏览器的预设样式设为一致 Native font stack 本机字体堆栈 由于padding 及 border 会改变元素在运算后的宽度 此时的实际宽度为: width+左右padding ...
- Flask-SQLAlchemy 配置,处理对象-关系,一对多,多对多
ORM(Object Relational Mapper) 对象关系映射.指将面对对象得方法映射到数据库中的关系对象中. Flask-SQLAlchemy是一个Flask扩展,能够支持多种数据库后 ...
- Centos7 安装MySQL 5.7 (通用二进制包)
1.下载安装包 下载地址 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz ...
- 不重新编译安装php模块的方法
如果你有下面两种经历: 如果php通过源码安装(php7),如果后来需要开启某个自带模块(例如ldap,snmp等),通常需要重新编译. 另外一些安装php模块的经历,例如redis,swoole,y ...
- MarkDown基础语法大全
一.MarkDown是什么? Markdown是一种轻量级的「标记语言」,创始人为约翰·格鲁伯,用简洁的语法代替排版,目前被越来越多的知识工作者.写作爱好者.程序员或研究员广泛使用.其常用的标记符号不 ...
- Mysql的子查询与连接查询
子查询: 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句 主查询和子查询的关系: 子查询是嵌入到主查询中,子查询是辅助主查询的,要 ...
- php数组处理函数
array_reverse()数组反向排序,$arr=array_reverse($arr)
- Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
在maven web项目中的index.jsp中的错误信息如下: The superclass "javax.servlet.http.HttpServlet" was not f ...
- linux下拼接字符串的代码
DATA_DIR=/home/liupan/.navinsight/gm result="" for i in $(ls -a $DATA_DIR) do if [ $i != & ...