Leetcode Implement strStr() My Submissions
来源:互联网 发布:unity3d ongui button 编辑:程序博客网 时间:2024/06/16 13:57
Implement strStr()
My SubmissionsImplement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
坑有这么几个:
1.needle可能啥也没有.
2.可能needle 的长度大于 haystack
3.可以走着走着到最后了.
说白了,关键还是长度,长度,长度....
public class Solution { public String strStr(String haystack, String needle) { if (haystack == null){ return null; } if (needle == null || needle.equals("")){ return haystack; } if (needle.length() > haystack.length()){ return null; } int leng = 0; for (int i = 0; i < haystack.length(); i++){ if (needle.charAt(0) == haystack.charAt(i)){ leng = needle.length() - 1; for (int j = 1; j < needle.length(); j++){ if (i + j > haystack.length() - 1){ return null; } if (needle.charAt(j) == haystack.charAt(i + j)){ leng--; } } if (leng == 0){ return haystack.substring(i); } } } return null; }}
0 0
- Leetcode Implement strStr() My Submissions
- LeetCode: Implement strStr()
- [Leetcode] Implement strstr()
- Leetcode: Implement strStr
- [Leetcode] Implement strStr()
- LeetCode Implement strStr()
- 【leetcode】Implement strStr()
- [LeetCode]Implement strStr()
- [LeetCode] Implement strStr()
- [LeetCode] Implement strStr()
- LeetCode-Implement strStr()
- LeetCode: Implement strStr()
- LeetCode - Implement strStr()
- LeetCode: Implement strStr()
- 【leetcode】Implement strStr()
- Leetcode: Implement strStr()
- leetcode implement strStr
- LeetCode Implement strStr()
- Android判断当前界面是否是home界面
- 今天秀逗了啊
- Android开发-精华帖推荐
- ubuntu下开启mysql的允许远程访问
- 免费电子书:The Guide to Minimum Viable Products
- Leetcode Implement strStr() My Submissions
- Java 内部类小结
- Spring的事务管理难点剖析(1):DAO和事务管理的牵绊
- php 数组指定位置插入数据单元
- window7环境下配置nginx+tomcat+memcached集群
- 一个农民天天学习养猪!记我的WEB开发学习之路!发表于: 2013-08-21 22:02:43
- chroot的作用及详解
- 资质修改查询(经营范围转换为中文)
- n阶螺旋矩阵