博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
和 LeetCode Battle (9、实现 strStr() 函数 )
阅读量:2054 次
发布时间:2019-04-28

本文共 749 字,大约阅读时间需要 2 分钟。

实现 strStr() 函数。

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符

字符串比较

public static int strStrChar(String haystack,String needle) {
if(needle==null | needle.equals("")) {
return 0; } char[] main = haystack.toCharArray(); char[] str = needle.toCharArray(); int y = 0;// hello ll for(int i = 0;i<=main.length-str.length;i++) {
for(y=0;y

双指针解法

public static int strStr(String haystack, String needle) {
if(needle==null | needle.equals("")) {
return 0; } int x = 0,y = 0,index = 0; while(x

字符串比较解析视频

实现 strStr() 函数 LeetCode题目

转载地址:http://wqclf.baihongyu.com/

你可能感兴趣的文章
BlockQueue 生产消费 不需要判断阻塞唤醒条件
查看>>
ExecutorService 线程池 newFixedThreadPool newSingleThreadExecutor newCachedThreadPool
查看>>
强引用 软引用 弱引用 虚引用
查看>>
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
win10将IE11兼容ie10
查看>>
checkbox设置字体颜色
查看>>
第一篇 HelloWorld.java重新学起
查看>>
ORACLE表空间扩张
查看>>
orcal 循环执行sql
查看>>
web.xml配置监听器,加载数据库信息配置文件ServletContextListener
查看>>
结构型模式之桥接模式(Bridge)
查看>>
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>