LeetCode题解之 Implement strStr()
1、题目描述
2.题目分析
字符串操作,注意边界条件即可。
3.代码
- int strStr(string haystack, string needle) {
- int n = needle.size();
- int res = -;
- if( needle.size() == )
- return ;
- for(string::iterator it = haystack.begin() ; it != haystack.end(); ++it){
- if((*it) == needle[]){
- bool isEqual = true;
- bool isEnd = false;
- for(int i = ; i < n; i++){
- if( it + i == haystack.end() ){
- isEnd = true;
- break;
- }
- if( needle[i] != *(it+i)){
- isEqual = false;
- break;
- }
- }
- if( isEnd == true )
- break;
- if( isEqual == true){
- res = it-haystack.begin();
- break;
- }
- }
- }
- return res;
- }
LeetCode题解之 Implement strStr()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- 【算法】LeetCode算法题-Implement strStr
这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...
随机推荐
- Intellij IDEA 环境配置与使用
Intellij IDEA 是我感觉最牛X的IDE开发工具,没有之一! 先share一篇教程: http://pan.baidu.com/s/1i3fzJff 调整字体 设置默认的JDK 显示行号 版 ...
- 用分布式缓存提升ASP.NET Core性能
得益于纯净.轻量化并且跨平台支持的特性,ASP.NET Core作为热门Web应用开发框架,其高性能传输和负载均衡的支持已广受青睐.实际上,10-20台Web服务器还是轻松驾驭的.有了多服务器负载的支 ...
- Vue图片懒加载插件 - vue lazyload的简单使用
Vue module for lazyloading images in your applications. Some of goals of this project worth noting i ...
- maven - 使用nexus 搭建maven私服
1, java环境 [wenbronk@localhost nexus]$ java -version java version "1.8.0_121" Java(TM) SE R ...
- rpm 软件包管理
rpm---Redhat Pachage Manager 挂载光盘: [root@localhost sdb1]# mount /dev/sr0 /mnt [root@localhost sdb1]# ...
- 制作openstack使用的Ubuntu镜像
一.环境准备 OS:Ubuntu-14.04 制作镜像版本:Ubuntu-14.04.4-server-amd64.iso 查看是否支持虚拟化(有输出代表支持,否则在BIOS页面中设置即可): egr ...
- 下载imagenet2012数据集,以及label说明
updated@2018-12-07 15:22:08 官方下载地址:http://www.image-net.org/challenges/LSVRC/2012/nonpub-downloads , ...
- WebForm 【复合控件】
一 复合控件(取值,赋值用法相近) RadioButtonList --单选按钮 (一组列表) <asp:RadioButtonList ID="RadioButtonL ...
- 浅谈sql中的in与not in,exists与not exists的区别以及性能分析
1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表 ...
- java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.String(Short类型无法强转成String类型)
有一行Java代码如下: String code1 = (String)qTable1.getValueAt(i, 0); 这是一个Java的图形界面获取表格中值的代码,其中qTable1.getVa ...