【leetcode】Implement strStr() (easy)
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:
注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字。
for(int i = ; i < int( - ); ++i)
实现很常规:
int strStr(string haystack, string needle) {
for(int i = ; i <= int(haystack.size() - needle.size()); ++i)
{
int j;
for(j = ; j < needle.size() && haystack[i + j] == needle[j]; ++j);
if(j == needle.size())
return i;
}
return -;
}
【leetcode】Implement strStr() (easy)的更多相关文章
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- 【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】Plus One (easy)
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 【leetcode】Climbing Stairs (easy)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- U盘操作系统,Kali Linux操作系统安装
为什么要用U盘装操作系统,那好处多了去了.1.随身携带,想用就用.2.平常娱乐还是用Windows比较方便,不用做双系统那么麻烦. 准备: U盘,从天猫上买了个三星闪存盘,32G,USB3.0: 从官 ...
- HDOJ 1520 Anniversary party
树形DP....在树上做DP....不应该是猴子干的事吗? Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 谈谈使用echarts过程中踩过的坑
小结: 1. 使用jquery获取json对象遇到的问题 由于自己对ajax用的还不熟练,之前都是拷贝别人的代码拿来用的,这次自己写的时候倒是碰到好多麻烦一一列举如下: 1.1 在$ 与ajax之间还 ...
- Go - 内置函数大全
Package builtin import "builtin" Overview Index Overview ▾ Package builtin provides docume ...
- Java BigDecimal详解
借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供 较为精确的快速近 ...
- OpenCV成长之路 01、图像的读写与显示
一.工具篇 工欲善其事,必先利其器.学习OpenCV,肯定少不于基本的编程工具与OpenCV库.在Windows平台下你可以选择Visual Studio.CodeBlock等,当然你也可以选择在Li ...
- ReLU 和sigmoid 函数对比以及droupout
参考知乎的讨论:https://www.zhihu.com/question/29021768 1.计算简单,反向传播时涉及除法,sigmod求导要比Relu复杂: 2.对于深层网络,sigmod反向 ...
- 快速排序(Quicksort)
快速排序:是对冒泡排序的一种改进. 什么是冒泡排序:简单一点就是冒气泡.极值数据会到达数据的顶端. 实现步骤:建立一个数据排列标准,从大到小还是从小到大. [从大到小排列]:从第一个数据开始遍历,比较 ...
- linux回收站设计
linux回收站设计 在windows下有一个很好的东西,那就是回收站,虽然有很多人批评它.linux不是没有回收站,很多桌面环境都可以看到是有回收站的. 这里是讨论如何设计一个回收站,而不是有没有的 ...
- COGS 2188. [HZOI 2015] Math 题解
题目描述: 给定n个数X1-Xn,求下面式子的值(整数部分): n<=107,xi<=109且互不相同. 分析: 其实一开始看见这道题我也吓傻了,k这么大,再说我又是数论鶸渣,打死也不 ...