日期:2014-05-20  浏览次数:20628 次

在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
1、在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
2、写一个方法去掉字符串前后空格。(主要考察代码思路,对字符串为空或NULL是否进行处理。)
  都用java实现的代码

------解决方案--------------------
Java code
public final class test {
    public static void main(String[] args) {
        System.out.println(single());
    }
    public static String single(){
        StringBuffer sb = new StringBuffer();
        Scanner input = new Scanner(System.in);
        System.out.println("输入一串字母");
        if (input.hasNext()) {//这里虽然判断了是否输入即是否为null。但是scanner控制台输入我还真不知道如何触发他为false的情况。。。
            String sth = input.next().trim();//这里获取控制台输入的字符串的时候进行了去空格处理。
            for (int i = 0; i < sth.length(); i++) {
                int count = 0;
                for (int j = 0; j < sth.length(); j++) {
                    if (sth.charAt(i) == sth.charAt(j)) {
                        count++;
                    }
                }
                if (count == 1) {
                    sb.append(sth.charAt(i));
                }
            }
            return sb.toString();
        }
        return "丫什么都没输入我给你判断什么?!";
    }
}

------解决方案--------------------

第一题
Java code

public class Test {

    /**
     * @Function:
     * @Since Oct 12, 2011
     * @param args
     */
    public static void main(String[] args) {
        String str = "abcbcefg";
        for(int i = 0 ; i < str.length();i++){
            if(str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))){
                System.out.println(str.charAt(i));
                break;
            }
        }

    }

}