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)的更多相关文章

  1. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  2. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

  3. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. 【leetcode】Plus One (easy)

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  6. 【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 ...

  7. 【leetcode】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  8. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. 【leetcode】Majority Element (easy)(*^__^*)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. ngCordova插件安装使用

    为什么ngCordova ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...

  2. Linux系统安装LAMP

    说明: 系统版本:Ubuntu14.04-LTS,可以在Ubuntu官网直接下载.Ubuntu其他版本也可安装本方法搭建LAMP环境! 步骤一,安装apache2 1 sudo apt-get ins ...

  3. Android工程文件下assets文件夹与res文件夹的区别

    1.assets:不会在R.java文件下生成相应的标记,assets文件夹可以自己创建文件夹,必须使用AssetsManager类进行访问,存放到这里的资源在运行打包的时候都会打入程序安装包中, 2 ...

  4. Unity3D Pro破解

    Win破解方法: 全新安装Unity且未打开Unity后!!! 下载程序, 右键管理员运行, 点击Browse选择Unity安装目录内的Editor文件夹, 确定. 然后点击大按钮PATCH即可, 如 ...

  5. tinyhttp源码阅读(注释)

    这里就不细述了,代码很简单. 其实现的功能比较若,可以做一个参考. 因为其通过文件的权限位来判断是否是一个CGI脚本,所以在权限位不对的情况下会判断不正确.例如我将这个目录放置在NTFS分区,所有的文 ...

  6. iframe自适应宽度

    <iframe id="course_content" style="width:100%;margin:5px 0 0;" scrolling=&quo ...

  7. 10. JEB1.5 插件编写二

    一些实例 1. 遍历当前光标处函数所有的Element Java代码: import java.io.*; import java.util.List; import jeb.api.IScript; ...

  8. MySQL 查看最大连接数, 当期连接数.

    查看最大连接数 select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='MAX_CONN ...

  9. MySQL查询交集

    MySQL表 CREATE TABLE `viewhistory` (   `viewid` int(11) NOT NULL AUTO_INCREMENT,   `uid` int(11) NOT ...

  10. iOS关于UILabel 基本属性 背景图片 背景色

    [代码] iOS关于UILabel 基本属性 背景图片 背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...