package com.mischen.it.test;
/**
* @ClassName TestInterview
* @Description DOTO 给定一个字符串,查找第一个不重复的字符
* @Author mischen
* @Date 2021/9/30 0030 7:22
* @Version 1.0
**/
public class TestInterview {
static String str = new String("asdsdasfbft");
public static void main(String[] args) {
long startTime=System.currentTimeMillis(); //获取开始时间
System.out.println(findViewOneString(TestInterview.str));
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
}
public static String findViewOneString(String str ){
char[] arrChar = str.toCharArray(); //为什么没toStringArray?
int arrChar_len = arrChar.length;
String strOne = "";
StringBuffer strBuf = new StringBuffer("");
for (int i = 0; i < arrChar_len; i++) {
strOne = String.valueOf(arrChar[i]);//a
strBuf.append(strOne);//a
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.print(strBuf.indexOf(strOne));
System.out.print("---");
System.out.println(strBuf.length()-1);
System.out.println(strBuf);
//如果两个不相等,则证明不包括,即之前没有出现过的字符串
if(!(strBuf.indexOf(strOne)!= (strBuf.length()-1))){
//if里面为true的时候直接走下面的逻辑,进行查找后面到末尾是否出现过该字符,
// 查询不到则直接返回,查询到不做处理
if((str.substring(i+1, arrChar_len).indexOf(strOne) == -1)){
System.out.println(i);
return strOne;
}
}
}
return "没有一个不重复的字符";
}
}
给定一个字符串,查找第一个不重复的字符(最优解法)
猜你喜欢
转载自blog.csdn.net/miachen520/article/details/120591427
今日推荐
周排行