Java编程题

一:如何在字符串中找到第一个不重复的字符

例如字符串“hello”,除了“l”之外所有字符都是不重复的,但是“h”是第一个不重复的字符。同样,字符串“swiss”中“w”是第一个不重复的字符。

解决方案如下,可以在一次字符串扫描中找到第一个不重复的字符,它应用了典型的空间时间权衡技术。它使用了两个存储空间来减少一次循环,是标准的空间-时间折衷。由于我们将重复和不重复的字符分开存放,在循环结束后,存放不重复字符的列表中的第一个元素就是我们所要找的第一个非重复字符。这种解决方案稍微优于上一种。如果在字符串中没有不重复的字符,你可以选择返回null或者空字符串。

public class Program10 {
//到现在为止我也不知道为什么不可以使用两个List集合,下面的解释我没看懂,我尝试了两个list结果也是正确的
    // 解释:将重复的元素放入到set集合,不重复的元素放入List集合。
    //不能使用两个List集合,因为当你将重复元素往set集合里存时,list中不再包含此重复元素,当下次再遇到此重复元素时,又会存入list集合,并存入到set集合,假使此时set集合变为list集合的话,
    // 在其中中会包含两个相同元素。可以解决,将set提前判决就行。
    public static char firstNonRepeatingChar(String word) {
        List<Character> repeating = new ArrayList<Character>();
        List<Character> nonRepeating = new ArrayList<Character>();
        for (int i = 0; i < word.length(); i++) {
            char letter = word.charAt(i);// 依次获得字符串中的每个字符
            // System.out.println(letter);
            // System.out.println(repeating);
            if (repeating.contains(letter)) {
                continue;
            }
            if (nonRepeating.contains(letter)) {
                nonRepeating.remove((Character) letter);// 这里强制转换和不强制转换区别这么大么?,如果这里输入的是char的话,默认转变为Int了
                repeating.add(letter);
            } else {
                nonRepeating.add(letter);
            }
            System.out.println(nonRepeating);
        }
        return nonRepeating.get(0);
    }

    public static void main(String[] args) {
        Program10 pro = new Program10();
        String str = "hanzhonghao";
        char c = pro.firstNonRepeatingChar(str);
        System.out.println("输入的字符串为:    " + str);
        System.out.println("字符串中第一个非重复的字符为:  "+c);
    }
}

二.将字符串输入,然后逆序输出

(1)stringbuild自带的方法

public class program11 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        StringBuilder sb = new StringBuilder(nextLine);
        String string = sb.reverse().toString();
        System.out.println(string);
    }

}

(2)常规方法

public class program12 {

    public void output(String str) {
        for (int i = 1; i <= str.length(); i++) {
            System.out.print(str.charAt(str.length() - i));
        }
        return;
    }

    public static void main(String[] args) {
        program12 program12 = new program12();
        program12.output("Test");
    }

}

(3)采用递归的方法

public class program13 {

    public static void main(String[] args) {
        reverseString("abcde");
    }
    public static void reverseString(String str) {
        if(str.length() == 1) {
            System.out.print(str);
        } else {
            String str1 = str.substring(0,str.length()-1);
            String str2 = str.substring(str.length()-1);      
            System.out.print(str2);
            reverseString(str1);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting_Boss_Hao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值