27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

Hint:

  1. Try two pointers.
  2. Did you use the property of "the order of elements can be changed"?
  3. What happens when the elements to remove are rare?

注:对一个无序数组,删除其中数值为val 的元素.提示中提到可以改变顺序,不知是何用意,难不成要先排序,再删除?

vector<int>::iterator nums_i = nums.begin();
while( nums_i != nums.end() )
{
if (*nums_i == val)
{
nums_i = nums.erase(nums_i);
}
else{
nums_i++;
}
}
return nums.size();

28. Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

注:在学习string 的时候看到过现成的函数,但想来这并不是这道题的本意吧。

int Solution::strStr(string haystack, string needle) {

    return haystack.find(needle);
}

Leetcode 题目整理-7 Remove Element & Implement strStr()的更多相关文章

  1. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  2. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  3. Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  4. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  5. LeetCode(27)Remove Element

    题目 Given an array and a value, remove all instances of that value in place and return the new length ...

  6. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. leetcode第25题--Remove Element

    problem: Given an array and a value, remove all instances of that value in place and return the new ...

  8. [LeetCode&Python] Problem 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...

  9. 【LeetCode 28_字符串_匹配】Implement strStr()

    解法一:Brute-force int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle ...

随机推荐

  1. java基础 -- 关键字final的用法

    用法一(修饰变量): Final变量能被显式地初始化并且只能初始化一次.被声明为final的对象的引用不能指向不同的对象.但是final对象里的数据可以被改变.也就是说final对象的引用不能改变,但 ...

  2. SpringJDBC的使用(转载)

    转载自   https://www.yiibai.com/spring/maven-spring-jdbc-example.html 工具: eclipse4.7.2及mysql-8.0.13 项目最 ...

  3. 别怕,"卷积"其实很简单(上)

    文章来自我的CSDN同名博客,欢迎文末扫码关注~   前言 相信很多时候,当我们在看到“卷积”时,总是处于一脸懵逼的状态,不但因为它的本身概念比较难理解,还因为它在不同的应用中发挥出的变幻莫测的作用也 ...

  4. 菜鸟系列Fabric源码学习 — committer记账节点

    Fabric 1.4 源码分析 committer记账节点 本文档主要介绍committer记账节点如何初始化的以及committer记账节点的功能及其实现. 1. 简介 记账节点负责验证交易和提交账 ...

  5. BridgePattern(桥接模式)-----Java/.Net

    桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化.这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦

  6. JavaScript数据类型 - Symbol

    ES5:对象的属性名只能是字符串,当给对象添加新属性时,很容易造成属性名冲突,从而覆盖了原有的属性. ES6:所以ES6中引入了symbol数据类型,他表示独一无二的值,避免了属性名的冲突,此时对象的 ...

  7. Django ORM调优实践

    一.分析请求慢响应的主要原因 将请求执行的任务按功能分为几块,用time.time()打印每个模块的执行时间,大部分情况下性能会主要消耗在某一个模块上,即80%的性能问题是出在20%的代码上 找到主要 ...

  8. windebug(转载别人的节选)

    问题一:WinDBG分X86和X64两个版本 如果你用的是32位的WinDBG,那直接打开就行:你如果用的是64位的版本,那么如果调试64位代码也直接打开,如果调试x86的代码,要使用Wow64下的W ...

  9. ORA-00903表名无效关于${}和#{}的使用

    相当于对数据 加上 双引号,$相当于直接显示数据 ${xxx}这样格式的参数会直接参与SQL编译,从而不能避免注入攻击 但可以使用在from之后传入表名 {}占位符? where之后 防止注入

  10. VMware显示错误:“未能锁定文件 无法打开磁盘 ..\*.vmdk 或者某一个快照所依赖的磁盘。”解决办法

    问题描述: 使用VMware时遇到错误:“未能锁定文件 无法打开磁盘 ..\*.vmdk 或者某一个快照所依赖的磁盘.” 问题出现的原因: 虚拟磁盘(.vmdk)本身有一个磁盘保护机制,为了防止多台虚 ...