题解 | #字符串加密#

字符串加密

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            // 1.生成对照表
            String strCom = sc.next();
            ArrayList<Character> list = getComparison(strCom);
            // 2.得到结果
            String str = sc.next();
            getResult(list, str);
        }
    }

    private static void getResult(ArrayList<Character> listValue, String str) {
        ArrayList<Character> listKey = new ArrayList<>();
        HashMap<Character, Character> map = new HashMap<>();
        for (char j = 'a'; j <= 'z'; j++) {
            listKey.add(j);
        }
        for (int i = 0; i < listKey.size(); i++) {
            map.put(listKey.get(i),listValue.get(i));
        }
        for (int n = 0; n < str.length(); n++) {
            System.out.print(map.get(str.charAt(n)));
        }
        System.out.println();
    }


    public static ArrayList<Character> getComparison(String string) {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < string.length(); i++) {
            if (list.contains(string.charAt(i))) {
                continue;
            } else {
                list.add(string.charAt(i));
            }
        }
        for (char j = 'a'; j <= 'z'; j++) {
            if (list.contains(j)) {
                continue;
            } else {
                list.add(j);
            }
        }
        return list;
    }
}


全部评论

相关推荐

07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
CARLJOSEPH...:宝宝你戾气太大了
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务