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. c++简单实现二叉树

    专业术语: 节点 父节点 根节点 子孙 堂兄弟 深度: 从根节点到最底层节点的层数称为深度 叶子节点: 没有子节点的节点称为叶子节点 非终端节点: 实际就是非叶子节点 度: 子节点的个数称为度 树的分 ...

  2. $bzoj2560$ 串珠子 容斥+$dp$

    正解:容斥+$dp$ 解题报告: 传送门$QwQ$ $umm$虽然题目蛮简练的了但还是有点难理解,,,我再抽象一点儿,就说有$n$个点,点$i$和点$j$之间有$a_{i,j}$条无向边可以连,问有多 ...

  3. springboot + freemarker 实现计算器

    使用RequestParam("") 接受参数 其后参数有自动类型转换的作用 @RequestParam("firstNumber") double first ...

  4. 「洛谷P2397」 yyy loves Maths VI (mode) 解题报告

    P2397 yyy loves Maths VI (mode) 题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居 ...

  5. (三)unittest断言方法的介绍

    断言如同在测试用例上,类似于预期结果与实际结果是否一致,如果一致则表示测试通过,Assert断言很好的用于测试结果判断上,更灵活的对预期结果和实际结果进行对比,下面简单的介绍一下unittest的As ...

  6. array_diff 大bug

    $aa = array("手机号", "first","keyword1","keyword2","keywo ...

  7. RHEL6.6安装Oracle 11g RAC - 基于VMware的实验环境

    实验环境准备虚拟机:VMware® Workstation 14 Pro操作系统:Red Hat Enterprise Linux 6.6 x86_64rhel-server-6.6-x86_64-d ...

  8. Spring boot项目搭建及简单实例

    Spring boot项目搭建 Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for ...

  9. Redis 千万不要乱用KEYS命令,不然会挨打的

    Redis现如今使用的场景越来越多?如何批量删除key呢? 有人说用KEYS命令,刚开始学Redis的时候就是用这个命令列出库中键. KEYS命令要谨慎使用. 为何?客观别急,我们先一步步来看. KE ...

  10. Go Web 编程之 Hello World

    概述 计划写一个讲 Go Web 编程的系列文章.从基于 net/http 包编写 Go Web 程序开始,讲述处理器,请求,响应等基础知识.然后到框架的使用.中间会穿插一些源码的分析.最后做一个实战 ...