题解 | #字符串加解密# 【打表】

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

模拟

一开始,自己傻乎乎模拟....

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String line2 = in.nextLine();
        System.out.println(encrypt(line1));
        System.out.println(decrypt(line2));
        in.close();
    }

    // 加密
    static StringBuilder encrypt(String s) {
        StringBuilder res = new StringBuilder();
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (Character.isDigit(c)) {
                res.append((char) ((c - '0' + 1) % 10 + '0'));
            } else {
                if (Character.isUpperCase(c)) {
                    char next = (char) ((c - 'A' + 1) % 26 + 'A');
                    res.append(Character.toLowerCase(next));
                } else {
                    char next = (char) ((c - 'a' + 1) % 26 + 'a');
                    res.append(Character.toUpperCase(next));
                }
            }
        }
        return res;
    }

    // 解密
    static StringBuilder decrypt(String s) {
        StringBuilder res = new StringBuilder();
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (Character.isDigit(c)) {
                c = c == '0' ? '9': (char)(c - 1);
                res.append(c);
            } else {
                if (Character.isUpperCase(c)) {
                    c = Character.toLowerCase(c);
                    c = c == 'a' ? 'z' : (char) (c - 1);
                    res.append(c);
                } else {
                    c = Character.toUpperCase(c);
                    c = c == 'A' ? 'Z' : (char) (c - 1);
                    res.append(c);
                }
            }
        }
        return res;
    }
}

打表 :star:

题解区大佬 学到的打表方法,比较巧妙

import java.util.*;

public class Main {
    private static final String L1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String L2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
    private static Map<Character, Character> encryptMap = new HashMap<>();
    private static Map<Character, Character> decryptMap = new HashMap<>();

    static {
        for (int i = 0; i < L1.length(); i++) {
            encryptMap.put(L1.charAt(i), L2.charAt(i));
        }
        for (int i = 0; i < L2.length(); i++) {
            decryptMap.put(L2.charAt(i), L1.charAt(i));
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String line2 = in.nextLine();
        System.out.println(encrypt(line1));
        System.out.println(decrypt(line2));
        in.close();
    }

    // 加密
    static StringBuilder encrypt(String s) {
        StringBuilder res = new StringBuilder();
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            res.append(encryptMap.get(c));
        }
        return res;
    }

    // 解密
    static StringBuilder decrypt(String s) {
        StringBuilder res = new StringBuilder();
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            res.append(decryptMap.get(c));
        }
        return res;
    }
}

上述加密、解密函数存在重复部分,可将这两个方法进一步精简为一个方法 process

import java.util.*;

public class Main {
    private static final String L1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String L2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
    private static Map<Character, Character> encryptMap = new HashMap<>();
    private static Map<Character, Character> decryptMap = new HashMap<>();

    static {
        for (int i = 0; i < L1.length(); i++) {
            encryptMap.put(L1.charAt(i), L2.charAt(i));
        }
        for (int i = 0; i < L2.length(); i++) {
            decryptMap.put(L2.charAt(i), L1.charAt(i));
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String line2 = in.nextLine();
        System.out.println(procss(line1, 1));
        System.out.println(procss(line2, -1));
        in.close();
    }

    // id = 1: 加密;id = -1: 解密
    static StringBuilder procss(String s, int id) {
        StringBuilder res = new StringBuilder();
        char c = ' ';
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (id == 1) res.append(encryptMap.get(c));
            if (id == -1) res.append(decryptMap.get(c));
        }
        return res;
    }
}
全部评论

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
小谷围鸡肉卷阿姨:+1,腾子投完一动不动
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务