【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 ...
随机推荐
- 使用原生JS封装Ajax
使用原生 的JS封装 Ajax,实现 仿JQuery的Ajax,post,get三种异步请求方式: var MAjax = { //根据浏览器创建异步对象 createXhr: function () ...
- poj3335 半交平面,多边形内核
Rotating Scoreboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5300 Accepted: 21 ...
- redis-string-统计
package com.ztest.redis.string; import org.junit.Test; import redis.clients.jedis.Jedis; import com. ...
- ckeditor、ckeditor配置--整合
1.将ckeditor和ckfinder文件夹拷入项目文件夹中,刷新项目. 2.ckfinder把文件夹中的bin目录下的dll文件(CKFinder.dll)添加到网站的引用中,防止出现找不到类的错 ...
- 解决IE6不支持position:fixed的bug
/*完整代码 */ /* 除IE6浏览器的通用方法 */ .ie6fixedTL { position: fixed; left:; top:; } .ie6fixedBR { position: f ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- BZOJ4610——[Wf2016]Ceiling Functi
水题一道,不是很懂为啥没人做... 1.题意:纠正一下..bzoj的题意不是很对...注意不是堆,是不平衡的二叉树,就是非旋转的treap, 另外...插入的时候,小于插在左边..大于等于插在右边 2 ...
- shareSDK
一. 编写sharesdk代码 在AppDelegate.m中 //shareSDK相关 #import <ShareSDK/ShareSDK.h> #import "Weibo ...
- Sqli-LABS通关笔录-3
/*此时心情xxxx*/ 通过这一关卡我学习到了 1.大概的能够mysql回显错误注入的面目,可以根据报错,写出闭合语句. 加一个单引号.报错如下所示. 加了一个单引号就说 1'') LIMIT 0, ...
- 新塘ARM平台交叉编译minigui界面库
简介 MiniGUI 是一款面向嵌入式系统的高级窗口系统(Windowing System)和图形用户界面(Graphical User Interface,GUI)支持系统,由魏永明先生于 1998 ...