【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
思路:
s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换。
int removeElement(int A[], int n, int elem) {
int s = , e = n - ;
while(s <= e)
{
if(A[e] == elem) //末尾待删 直接删除
{
e--;
continue;
}
if(A[s] == elem)
{
A[s++] = A[e--];
}
else
{
s++;
}
}
return e + ;
}
【leetcode】Remove Element (easy)的更多相关文章
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 【leetcode】Remove Element
题目概述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- 收到的电邮附件为Winmail.dat?
以下信息来源于微软帮助中心:您收到电子邮件,其中包含一个 winmail.dat 的附件.电子邮件被人使用的 Microsoft Outlook 发送给您.该邮件的格式是丰富文本格式 (RTF). 原 ...
- 2013nanjingJ
J - Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 谈谈使用echarts过程中踩过的坑
小结: 1. 使用jquery获取json对象遇到的问题 由于自己对ajax用的还不熟练,之前都是拷贝别人的代码拿来用的,这次自己写的时候倒是碰到好多麻烦一一列举如下: 1.1 在$ 与ajax之间还 ...
- Oracle添加数据报文字与格式字符串不匹配错误
今天在学习Oracle时碰到一个错:文字与格式字符串不匹配. 我在Oracle数据库中创建了一张表: --创建员工表employee create table employee ( empon ) n ...
- laravel中间件-----------middleware
middleware中间件 是访问到达服务器后在被对应的路由处理之前所经过的一层过滤层,故称中间件. 中间件是存放在app\http\middleware中,需要定一个 handle 处理方法,在ha ...
- 解决vs2010“创建或打开C++浏览数据库文件 发生错误”的问题 Microsoft SQL Server Compact 3.5
有网友说打开vs2010安装光盘,搜索 SSCERuntime_x86-chs.msi,重新安装之.于是果断搜索,发现SSCERuntime_x86-chs.msi,另外发现一个SSCEVSTools ...
- COM中的REFIID小解【转】
是在浏览器项目中的IDispatch调用QueryInterface( [in] REFIID riid, [out] void **ppvObject); 认识的REFIID,由于看声明说r ...
- 服务器&域名那些事儿
购买的阿里云的服务器(ECS)和域名 请移步: 服务器&域名那些事儿 服务器&域名那些事儿2 github 博客
- Android应用中返回键的监听及处理
MainActivity: package com.testnbackpressed; import android.os.Bundle; import android.view.KeyEvent ...
- ExtJs xtype类型介绍
自定义组件在定义的时候可以通过xtype配置为组件指定xtype短名称,此后创建对象可以通过xtype来创建自定义对象了,示例代码如下: Ext.define('MyApp.PressMeButton ...