Leetcode 题目整理-7 Remove Element & Implement strStr()
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:
- Try two pointers.
- Did you use the property of "the order of elements can be changed"?
- 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()的更多相关文章
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 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 ...
- 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 ...
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- leetcode第25题--Remove Element
problem: Given an array and a value, remove all instances of that value in place and return the new ...
- [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 ...
- 【LeetCode 28_字符串_匹配】Implement strStr()
解法一:Brute-force int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle ...
随机推荐
- Harbor本地镜像库安装与使用
Harbor安装很简单,不过安装以后要怎么操作,百度了好久都没有找到实操的方法,怎么从官方拉取镜像后更新到私有仓库中?这个弄了好久,一样样测试后才找到方法,对于不熟悉私有仓库的小白来说,有点坑.还好花 ...
- 将Samba设置为Active Directory域控制器
一 简介 从版本4.0开始,samba可以作为Active Directory(AD)域控制器(DC)运行,如果在生产环境中安装samba,建议运行两个或者多个DC用于故障转移 本文介绍如何让将一个S ...
- 轻松搭建基于 SpringBoot + Vue 的 Web 商城应用
背景介绍 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数 ...
- 「学习笔记」动态规划 I『初识DP』
写在前面 注意:此文章仅供参考,如发现有误请及时告知. 更新日期:2018/3/16,2018/12/03 动态规划介绍 动态规划,简称DP(Dynamic Programming) 简介1 简介2 ...
- 基于CentOS 7 部署MySQL 5.7的基本操作
关闭selinux # sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config重启后生效# sestatus 修改提示符配置# vi / ...
- Ant Design Pro路由传值
Ant Design Pro 路由传值 了解Ant Design Pro组件间通讯原理的小伙伴肯定都知道,两个页面之间可以通过Models进行传值,在以往的传值过程中,我都是直接将需要的值直接一股脑的 ...
- structs2的048漏洞复现 2.3.32版本
S2-048 远程代码执行漏洞 影响版本: 2.0.0 - 2.3.32 之前在网上说只要时2.3.*的版本都有这个漏洞,自己装了个2.3.34的试了几次都没有执行 然后下载了找到了这篇文章才知道影响 ...
- es6中的面向对象写法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- Vuex实践(下)-mapState和mapGetters
Vuex系列文章 <Vuex实践(上)> <Vuex实践(中)-多module中的state.mutations.actions和getters> <Vuex实践(下)- ...
- 超级火的java自学网站
学靠的是毅力和自律,一定要坚持,否则就会前功尽弃,我自己也一直在边学边工作,当然自学要配合好的学习资料. 我是通过这个地方去学习的,它可以添加学习计划,从java基础到高级,从后台到前端,从细节到框架 ...