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);
        }
    }
    
  • 相关阅读:
    Intouch 制作自定义登录弹窗
    ifix vba 读取计算机中的txt文件,截取字符串显示
    intouch 开发源程序加密方法
    语音报警系统说明
    ifix历史数据(H04/H08/H24)转换为CSV文件导出
    ifix 自动化(Automation)错误弹窗的解决方案
    云平台制作(二)-平台展示
    knudson hypothesis 二次突变假说
    纯合子,杂合子,杂合性缺失
    zero-base coordinate 和one-base coordinate
  • 原文地址:https://www.cnblogs.com/chaoren399/p/4984948.html
Copyright © 2011-2022 走看看