码迷,mamicode.com
首页 > 其他好文 > 详细

Implement strStr()

时间:2017-07-03 12:18:26      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:art   java   etc   i++   div   while   ons   imp   detail   

https://leetcode.com/submissions/detail/108134096/

brute force

 public int strStr(String haystack, String needle) {
        if (haystack.length() < needle.length()) {
            return -1;
        }
        int start = 0;
        while (start <= haystack.length() - needle.length()) {
            int i = start, j = 0;
            while (j < needle.length() && haystack.charAt(i) == needle.charAt(j)) { // 判断
               
                    i++;
                    j++;
                
            }
            if (j == needle.length()) {
                return start;
            }
            start++;
        }
        return -1;
    }

  

Implement strStr()

标签:art   java   etc   i++   div   while   ons   imp   detail   

原文地址:http://www.cnblogs.com/apanda009/p/7109771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!