LeetCode-in-Java

3602. Hexadecimal and Hexatrigesimal Conversion

Easy

You are given an integer n.

Return the concatenation of the hexadecimal representation of n2 and the hexatrigesimal representation of n3.

A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9 and the uppercase letters A - F to represent values from 0 to 15.

A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9 and the uppercase letters A - Z to represent values from 0 to 35.

Example 1:

Input: n = 13

Output: “A91P1”

Explanation:

Example 2:

Input: n = 36

Output: “5101000”

Explanation:

Constraints:

Solution

public class Solution {
    public String concatHex36(int n) {
        int t = n * n;
        int k;
        StringBuilder st = new StringBuilder();
        StringBuilder tmp = new StringBuilder();
        while (t > 0) {
            k = t % 16;
            t = t / 16;
            if (k <= 9) {
                tmp.append((char) ('0' + k));
            } else {
                tmp.append((char) ('A' + (k - 10)));
            }
        }
        for (int i = tmp.length() - 1; i >= 0; i--) {
            st.append(tmp.charAt(i));
        }
        tmp = new StringBuilder();
        t = n * n * n;
        while (t > 0) {
            k = t % 36;
            t = t / 36;
            if (k <= 9) {
                tmp.append((char) ('0' + k));
            } else {
                tmp.append((char) ('A' + (k - 10)));
            }
        }
        for (int i = tmp.length() - 1; i >= 0; i--) {
            st.append(tmp.charAt(i));
        }
        return st.toString();
    }
}