【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 ...
随机推荐
- aperm方法
本文原创,转载请注明出处,本人Q1273314690(交流学习) 感觉很多地方提到了aperm,但都没讲清楚,我自己参考了大家的资料,做了下总结,希望能够让对大家有所帮助. aperm方法 Tran ...
- IGV软件
它支持各种各样的数据类型,包括基于芯片测序.二代测序数据和基因组注释数据等.整合基因组浏览器(IGV,Integrative Genomics Viewer)进行可视化浏览,它支持各种各式的数据类型, ...
- 清除浮动(clearfix hack)
eg:
- Linux程序编写shell script的格式
#!/bin/bash #program # 在此处写下此程序的作用 #History: #此处写下写此程序的时间 作者 版本号 PATH=/bin:/sbin:/usr/bin:/usr/sbin: ...
- 安装 vue.js和第一个hello world
一.在自己的项目文件中使用npm下载vue npm install vue 二.在文件中引入vue.js 三.第一个hello world 注:scritpt代码必须写在html代码的下面
- Ubuntu 14 修改默认打开方式
通过研究,有三种修改方式. 方式一: 修改路径:右上角“系统设置” -> 详细信息 -> 默认应用程序 但是,有个缺陷,可修改的项比较少. 方式二: 例如,修改pdf的打开方式,只要查看任 ...
- Javascript高级程序设计——垃圾收集
javascipt具有自动垃圾回收机制 局部变量只在函数执行过程中存在,在这个过程中,会为局部变量在栈上(或堆)内存分配相应空间,来储存他们的值,当函数执行完,局部变量就没有存在的必要了,所以这个时候 ...
- Error 0x800704cf
重装了系统,改过网络配置,结果共享和打印机连不上,显示Error 0x800704cf 在本地连接的属性里将"Client for Microsoft Networks"勾选上就可 ...
- IBM B16光纤交换机ZOON划分方法
一.ZOON的含义及划分原则 Zoon在光纤存储交换机中的功能类似于以太网交换机VLAN的作用,主要是为了在一台交换机划分出多个逻辑区,用于防范不同应用的存储连接发起广播包,提高光纤交换机 ...
- OpenCV成长之路 01、图像的读写与显示
一.工具篇 工欲善其事,必先利其器.学习OpenCV,肯定少不于基本的编程工具与OpenCV库.在Windows平台下你可以选择Visual Studio.CodeBlock等,当然你也可以选择在Li ...