zoukankan      html  css  js  c++  java
  • 字符串查找-1

    /**
     * Created by zzy on 15/11/16.
     */
    
    /**
     * 对于一个给定的 source 字符串和一个 target 字符串,
     * 你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。
     * 如果不存在,则返回 -1。
     *
     * 如果 source = "source" 和 target = "target",返回 -1。
     * 如果 source = "abcdabcdefg" 和 target = "bcd",返回 1。
     *
     *
     * O(n2)的算法是可以接受的。如果你能用O(n)的算法做出来那更加好。(提示:KMP)
    
     */
    public class StringSearch {
    
        public static  int  strString (String source,String target){
            if (source == null || target == null){
                return -1;
            }
            int i,j;
            for ( i = 0; i < source.length() - target.length() + 1; i++) {
                for (j = 0; j < target.length();j++){
                    if (source.charAt(i+j) !=target.charAt(j)){
                        break;
                    }
    
                }
                if (j == target.length()){
                    return i;
                }
            }
            return -1;
        }
    
        public static void main(String[] args){
          int temp =  strString("abadcddfrefad","ba");
            System.out.println(temp);
        }
    }
    
  • 相关阅读:
    异步多线程 Thread ThreadPool Task
    ASP.NET MVC IActionFilter IResultFilter IExceptionFilter/HandleError
    ASP.NET MVC Area 区域
    ASP.NET MVC Bundles 合并压缩(js css)
    ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性
    ASP.NET MVC Form表单验证与Authorize特性
    ASP.NET MVC Owin 基本理解
    CSS 基础 例子 图片拼合技术
    CSS 基础 例子 display属性:block、inline和inline-block的区别
    UVa 1326
  • 原文地址:https://www.cnblogs.com/chaoren399/p/4984948.html
Copyright © 2011-2022 走看看