[程序员面试金典-查找-]找出字符串

题目描述
有一个排过序的字符串数组,但是其中有插入了一些空字符串,请设计一个算法,找出给定字符串的位置。算法的查找部分的复杂度应该为log级别。
给定一个string数组str,同时给定数组大小n和需要查找的string x,请返回该串的位置(位置从零开始)。
测试样例:
[“a”,”b”,”“,”c”,”“,”d”],6,”c”
返回:3

代码如下:

package niuke;
/**
 * 题目描述
有一个排过序的字符串数组,但是其中有插入了一些空字符串,请设计一个算法,找出给定字符串的位置。算法的查找部分的复杂度应该为log级别。
给定一个string数组str,同时给定数组大小n和需要查找的string x,请返回该串的位置(位置从零开始)。
测试样例:
["a","b","","c","","d"],6,"c"
返回:3
 * @author Administrator
 *
 */
public class FindNoneString {
    public int find(String[] str,int n,String toFindString){
        if(str== null|| n==0)
            return -1;
        int low = 0,high =n-1;

        while(low<=high){
            int mid = (high-low)/2+low;
            if(str[mid]==""){
                int index = mid;
                while(index >= low && str[index]==""){
                    index --;
                }
                if(index<low)
                    low =mid+1;
                else if(str[index].compareTo(toFindString)>0)
                    high = index -1;
                else if(str[index] == toFindString)
                    return index;
                else 
                    low = mid + 1;
            }
            if(str[mid].compareTo(toFindString)>0)
                high = mid-1;
            else if(str[mid].compareTo(toFindString)<0)
                low =mid + 1;
            else if(str[mid]==toFindString)
                return mid;
        }
        return -1;
    }
    public int find2(String[] str,int n ,String x){
          // write code here
        if(str==null||n==0)
            return -1;
        //int length = str.length;
        int left = 0,right = n-1;
        while(left<=right){
            int mid = (right-left)/2+left;
            if(str[mid] == x) return mid;
            if(str[mid]==""){
                int index = mid;
                while(index>=left&&str[index]=="")
                    index--;
                if(index<left)
                    left = mid+1;
                if(str[index]==x)
                    return index;
                else if(str[index].compareTo(x)>0)
                    right = index-1;              
                else 
                    left = mid + 1;
            }//if(str[mid]=="")
            if(str[mid].compareTo(x)>0)
                right = mid -1;
            else if(str[mid].compareTo(x)<0)
                left = mid+1;
        }//while(left<=right){
        return -1;
    }
    //该方法更加简洁,简单。
    public int find3(String[] str,int n,String x){
           // write code here
        if(str == null||n==0)
            return -1;
        //int length = str.length;
        int left = 0;
        int right = n-1;
        int mid;
        while(left<=right){
             mid = (right+left)/2;

            if(str[mid].equals("")){
                mid--;
            }//if(str[mid]=="")
           if(str[mid].equals(x)) {//牛客编译器使用==无法得到结果,显示时间超时。
                 return mid;
           }         
           else  if(str[mid].compareTo(x)>0){
               right = mid -1;
           }          
           else {
               left = mid+1;
           }          
        }//while(left<=right){
        return -1;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] str= {"a","b","","c","","d","e",""};
        FindNoneString fn =new FindNoneString(); 
        int index = fn.find(str, str.length, "b");
        int index2= fn.find2(str, str.length, "d");
        System.out.println(index);
        System.out.println(index2);
//      String s1 ="hello world!";
//      String s2 ="nihao";
//      boolean flag = true;
//      if(5){
//          System.out.println("hello");
//      }
//      System.out.println(flag);   
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值