题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

http://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

import java.util.Scanner;

public class Main {

// 上台阶  你可以爬1到2个台阶
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc =new Scanner(System.in);
	while(sc.hasNextLine()) {
		String inputStr =sc.nextLine();
		if(inputStr.length()==0) {
			return;
		}
		inputStr = inputStr.trim();
		boolean isHave = false;
		String result = "";
		for(int i = 0;i<inputStr.length();i++) {
			String str = inputStr.substring(i, i+1);
			if(inputStr.indexOf(str) == inputStr.lastIndexOf(str)) {
				result = str;
				isHave = true;
				break;
			}
		}
		if(!isHave) {
			System.out.println("-1");
		}else {
			System.out.println(result);
		}
		
	}

}

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务