Preface explains

Algorithm learning, daily brush record.

Subject to connect

Hex magic numbers

The subject content

You have a decimal number, turn it into a “hexadecimal magic number” by following this rule: first turn it into an uppercase hexadecimal string, then turn all zeros into O’s and 1s into I’s.

If A digital contains only after conversion {” A “, “B”, “C”, “D”, “E”, “F” and “I”, “O”}, then we will think the transformation is effective.

You are given the string num, which represents a decimal number N. If its hex magic number conversion is valid, return the converted result, otherwise return “ERROR”.

Example 1:

Enter: num = “257”

Output: “IOI”

Explanation: The hexadecimal representation of 257 is 101.

Example 2:

Enter: num = “3”

Output: “ERROR”

Tip:

1 <= N <= 10^12

A given string will not have a leading 0.

All letters in the result should be uppercase.

The analysis process

1 <= N <= 10^12, so use long to save decimal number.

The first step

Defines the decimal number decimal, starting with 0.

Define the decimal bit e, starting with 10^0=1.

The second step

The string num is converted to a decimal number decimal by summing the result of multiplying the current digit by 10 to the power of n each time, incrementing the power of 10 by 1 each time, for example: 257 = 7 * 10^0 + 5 * 10^1 + 2 * 10^2 = 7 + 50 + 200 = 257.

The third step

Define a stack used to hold hexadecimal magic number characters.

The decimal number decimal is converted to hexadecimal magic number characters in a loop, using the mod method, saving the characters to the stack, but the result is backwards.

The fourth step

During each loop:

1. Decimal number decimal mod 16 to obtain remainder REM. If remainder REM is between 10 and 16, it is represented in uppercase and pushed onto the stack; If the remainder REM is 0, it is converted to the magic number O and pushed onto the stack; If the remainder REM is 1, it is converted to the magic number I and pushed onto the stack; If the remainder rem is any other value, then it does not meet the requirement that the problem contains only magic numbers. Immediately return ERROR and end the program.

2. Close the loop by dividing decimal numbers by 16 and shrinking them until they become 0.

After the loop ends, the hexadecimal magic number character is saved in the stack.

Step 5

Define string set SB, used to store hexadecimal magic number characters, transfer the stack of flashback characters to string set SB, finally string set SB to string, return the result, end the program.

To solve the code

Class Solution {public String toHexspeak(String num) { In analog, the string is converted to a decimal number, and the decimal number is converted to a hexadecimal magic number character. The result is reversed, and the hexadecimal magic number character is reversed to a sequential string. Note that the range of decimal numbers is 1 <= N <= 10^12, so use long to save decimal numbers // define decimal numbers. Initialize to 0 long decimal = 0; // Define decimal bits, starting with 10^0=1 long e =1; For (int I = num.length() -1; i >= 0; -- I) {// Each time equals the current number multiplied by decimal bits Decimal += (num.charat (I) -48) * e; // Each time the decimal bit is expanded ten times e *= 10; } // define the Stack, save the hex magic number Character Stack<Character> Stack = new Stack<>(); / / define the hex 10 to 16 of the capital letters in the char [] capitalLetters = {' A ', 'B', 'C', 'D', 'E', 'F'}; // Using the mod method, decimal numbers are converted to hexadecimal magic number characters, saving the characters on the stack, resulting in a reverse while (decimal > 0) {// Each mod int rem = (int) (decimal % 16); If (rem >= 10) {// capitalLetters stack. Push (capitalLetters[REM-10]); } else if (rem == 0) {// If remainder is 0, turn to magic number O stack.push('O'); } else if (rem == 1) {// If remainder is 1, turn to magic number I stack.push('I'); } else {return "ERROR";} else {return "ERROR"; } // Divide by 16 and shrink until 0 decimal /= 16; StringBuilder sb = new StringBuilder(); // Move the stack to the string set while (! stack.isEmpty()) { sb.append(stack.pop()); Return sb.toString(); return sb.tostring (); }}Copy the code

Submit the results

It took 1ms to execute, beating 100.00% of the user in time, 38.1MB of memory consumption, and 52.63% of the user in space.

The original link

Hex magic numbers