Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:对应检查char是否一样,不一样直接跳出检查下一组。用boolean来判断这组是不是符合,如果符合直接输出。

119/145

public class Solution {public int strStr(String haystack, String needle) {if(haystack==null&&needle==null||haystack.length()<needle.length()){return -1;}for(int i=0;i<=haystack.length()-needle.length();i++){boolean check=true;for(int j=0;j<needle.length();j++){if(needle.charAt(j)!=haystack.charAt(i+j)){check=false;break;}}if(check==true){return i;}}return -1;}
}

转载于:https://www.cnblogs.com/Machelsky/p/5944465.html

28. Implement strStr()相关推荐

  1. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  2. leetCode 28. Implement strStr() 字符串

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

  3. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  4. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  5. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  6. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  7. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  8. LeetCode 28 Implement strStr()(实现strStr()函数)

    翻译 实现strStr()函数.返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1.其实也就是说字符串str2在字符串str1中第一次出现的索引而已. ...

  9. [leetcode] 28. Implement strStr() 解题报告

    题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...

  10. leetcode 28. Implement strStr() 实现strStr()

    C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...

最新文章

  1. 《HBase企业应用开发实战》—— 2.5 停止HBase集群
  2. 2014-06-25nbsp;12:55
  3. JFreeChart项目实例
  4. VMware “Transport(VMDB)error -44:Message”
  5. C#LeetCode刷题之#100-相同的树(Same Tree)
  6. 区间dp——cf1025D二叉搜索树的中序遍历好题!
  7. java 异常处理线程_转:Java子线程中的异常处理(通用)
  8. tomcat ---- server.xml
  9. 用Adomd.net构建最清凉的olap示例
  10. Oracle Database 11g Express Edition学习笔记
  11. Java面试题全集(中)
  12. 胡水生:中小型企业如何应对互联网的发展
  13. 当年表白流行写情书,现在流行的是……
  14. HTTPS的工作原理
  15. BIT2022年春软件需求工程和UML建模 - 智慧校园数据管理系统 - Final
  16. BeanFactory和applicationContext区别
  17. 反编译微信小程序错误: $gwx is not defined和__vd_version_info__ is not defined 已解决
  18. Linux内核TSS的使用
  19. 实现阶乘计算功能,输入一个整数 n,计算并输出他的阶乘。
  20. Linux高级查询命令

热门文章

  1. Python(四):字符串
  2. 用Python 开发您的第一个 XGBoost 模型(收藏)
  3. linux定时器无法重启pm2,在linux下开机启动pm2 不成功
  4. php hidden属性,微信小程序关于组件的hidden属性的使用建议
  5. MCS:随机数的生成
  6. 文件怎么更新_干货!Win10更新总失败?学会这三招搞定它
  7. html5鼠标悬停遮罩,js实现鼠标移动到图片产生遮罩效果
  8. 295.数据流的中位数
  9. 墙面有几种装修方法_卧室装修静音环保攻略,赶紧收藏起
  10. 电脑电子版文件怎么弄_清空回收站后,如何找回删除的文件?