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 ...
随机推荐
- Saving output of a grep into a file with colors
19 down vote favorite 7 I need to save the result of a grep command into a file, but I also want the ...
- vue 报错 Uncaught (in promise) error
可尝试在then()后加上catch() ps:该图来自网络
- React 从入门到进阶之路(三)
之前的文章我们介绍了 React 创建组件.JSX 语法.绑定数据和绑定对象.接下来我们将介绍 React 绑定属性( 绑定class 绑定style).引入图片 循环数组渲染数据. 上一篇中我们 ...
- [Windows]获取当前时间(年/月/日/时/分/秒)
struct tm* GetCurTime(time_t inTime) { struct tm* curTime = localtime(&inTime); curTime->tm_y ...
- (扫码二维码不显示)php微信扫码支付sdk不能用了
解决方案: img标签中src 该为qrcode.php的绝对路径,如: 备注:微信端这个生成二维码的路径老是改,之前是http://paysdk.weixin.qq.com/example/qrco ...
- PostFX v2后期处理特效包:升级更惊艳的视觉效果
https://mp.weixin.qq.com/s/BMkLLuagbhRSWspzeGhK7g Post-Processing Stack后期处理特效包能够轻松创建和调整高质量视觉效果,实现更为惊 ...
- 最小生成树(prim和kruskal)
最小生成树(prim和kruskal) 最小生成树的最优子结构性质 设一个最小生成树是T.如果选出一个T中的一条边,分裂成的两个树T1,T2依然是它们的点集组成的最小生成树.这可以用反证法来证.反着来 ...
- 如何在app里利用js调取手机第三方地图--以高德地图和百度地图为例(2)
接着上篇文章说一下js调取第三方地图的问题,上次的方式是通过一个链接直接接到了第三方的web页面,又从第三方的web页面调用的第三方app;结果,这个方法被否定了,因为需求不是这样,需求直接就想调用第 ...
- K8S上的ELK和应用日志上报实战
来源:DevOps ID:Idevops168 本次实战的基础结构如下图所示: 一共有两个Pod:ELK和web应用: ELK的Pod会暴露两个服务,一个暴露logstash的5044端口,给file ...
- springboot 简单使用shiro登录
首先引入需要的pom <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shir ...