题解 | #字符串加密#

字符串加密

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;
    }
}


全部评论

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务