【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 ...
随机推荐
- 15个关于Chrome的开发必备小技巧
一.快速查找文件 如果你使用过Sublime,那么你会知道’Go to anything’的强大.没错,Chrome现在也有了这一功能. 操作如下: 1.F12打开你的Chrome调试器: 2.按下C ...
- 【C语言入门教程】3.1 程序的 3 种基本结构
程序设计是一个问题求解的过程,解决问题的步骤可看作是程序的控制结构.简单地说,程序的运行过程就是数据输入.数据处理.数据输出 3 个步骤.其中,数据处理过程是否快捷和准确,主要依赖于程序控制结构的设计 ...
- 《深入浅出WPF》笔记一
1.项目模板 Visual Studio自动配置编译器参数,并准备好一套基本的源代码. 2.App.xaml/App.xaml.cs 声明程序的进程,并指定程序的主窗体. 3.Attribute和Pr ...
- php正则预查
php正则预查 // 把ing结尾的单词词根部分(即不含ing部分)找出来$str = 'hello ,when i am working , don not coming';零宽度:站在原地往前看, ...
- Java VisualVM使用:堆OOM
背景 近期遇到了一个java.lang.OutOfMemoryError: Java heap space的问题,排除了堆设置过小的问题,代码走查没有头绪,所以使用VisualVM工具分析堆内存情况. ...
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...
- github 多个项目共用同一个key的方法
后面的项目不用添加ssh keys, 直接在项目下设置合作者(Collaborators), 搜索出去加进去即可免密码传代码.
- python entrypoint
entrypoint, 实际是一张匹配表.匹配简短指令和具体的python函数的执行路径.有点快捷方式的概念. 不同的是,这种快捷方式不仅可以给命令行使用,还可以供其他代码简单调用,而无需关注太多细节 ...
- django 技巧锦集
1. 如何将model对象转化成一个dict,这在restful api的设计上会有用. 答案:使用django.forms.models的model_to_dict方法,demo如下: from d ...
- EXT Grid 默认展开所有行
grid.getStore().load({ //默认展开所有行. callback:function() { var expander = grid.plugins[0]; var count = ...