博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LF.85.Determine If One String Is Another's Substring
阅读量:6877 次
发布时间:2019-06-26

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

Determine if a small string is a substring of another large string. Return the index of the first occurrence of the small string in the large string. Return -1 if the small string is not a substring of the large string. Assumptions Both large and small are not null If small is empty string, return 0 Examples “ab” is a substring of “bcabc”, return 2 “bcd” is not a substring of “bcabc”, return -1 "" is substring of "abc", return 0
1 public int strstr(String large, String small) { 2         // Write your solution here 3         if (large == null || small == null){ 4             return -1 ; 5         } 6         if (large.length() == 0 && small.length() ==0 ){ 7             return 0 ; 8         } 9         if (large.length() == 0){10             return -1 ;11         }12 13         if (small.length() ==0){14             return 0;15         }16 17         if (large.length() < small.length()){18             return -1 ;19         }20         //loop through large and small21         //if L != S break22         /*23             b c a b c24                 i25             a b c26             j27         * */28         for (int i = 0; i <= large.length() - small.length() ; i++) {29             int j = 0 ;30             for (; j < small.length(); j++) {31                 if (large.charAt(i+j) != small.charAt(j)){32                     //out of inner loop, increase the i33                     break;34                 }35             }36             //check to make sure only return when j reaches the end, otherwise when out of the break, will execute this line37             if (j == small.length()){38                 return i ;39             }40         }41         return -1 ;42     }

 

转载于:https://www.cnblogs.com/davidnyc/p/8726633.html

你可能感兴趣的文章
回到顶部代码(兼容IE6)
查看>>
web.xml文件的作用
查看>>
iOS开发篇——OC延展类目协议介绍
查看>>
期盼已久,不负所望!XenServer 6.5版本正式发布
查看>>
全方位解读云计算
查看>>
出来了,下雪了
查看>>
我的友情链接
查看>>
AFN网络图片获取
查看>>
Springboot官方构建下的mvnw.cmd的作用
查看>>
http://jquerymobile.com/ 手机特效制作
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Oracle EBS R12查看用户密码
查看>>
爬虫书籍
查看>>
ORACLE SQL Loader
查看>>
linux 双网卡绑定
查看>>
Split-Brain
查看>>
rap接口管理工具
查看>>
基于bind工具实现DNS子域授权、子域父域相互解析
查看>>
grep文本搜索命令+正则表达式搜索详解
查看>>