|
| 1 | +use std::ops::{Shl, ShlAssign, Shr}; |
| 2 | +use std::hint::unreachable_unchecked; |
| 3 | + |
| 4 | +/// @number 405 |
| 5 | +/// @title Convert a Number to Hexadecimal |
| 6 | +/// @url https://leetcode.com/problems/convert-a-number-to-hexadecimal |
| 7 | +/// @difficulty easy |
| 8 | +
|
| 9 | +// |
| 10 | +//Given an integer, write an algorithm to convert it to hexadecimal. For negativ |
| 11 | +//e integer, two’s complement method is used. |
| 12 | +// |
| 13 | +// |
| 14 | +// Note: |
| 15 | +// |
| 16 | +// All letters in hexadecimal (a-f) must be in lowercase. |
| 17 | +// The hexadecimal string must not contain extra leading 0s. If the number is ze |
| 18 | +//ro, it is represented by a single zero character '0'; otherwise, the first chara |
| 19 | +//cter in the hexadecimal string will not be the zero character. |
| 20 | +// The given number is guaranteed to fit within the range of a 32-bit signed int |
| 21 | +//eger. |
| 22 | +// You must not use any method provided by the library which converts/formats th |
| 23 | +//e number to hex directly. |
| 24 | +// |
| 25 | +// |
| 26 | +// |
| 27 | +// Example 1: |
| 28 | +// |
| 29 | +//Input: |
| 30 | +//26 |
| 31 | +// |
| 32 | +//Output: |
| 33 | +//"1a" |
| 34 | +// |
| 35 | +// |
| 36 | +// |
| 37 | +// Example 2: |
| 38 | +// |
| 39 | +//Input: |
| 40 | +//-1 |
| 41 | +// |
| 42 | +//Output: |
| 43 | +//"ffffffff" |
| 44 | +// |
| 45 | +// Related Topics Bit Manipulation |
| 46 | +// 👍 433 👎 112 |
| 47 | + |
| 48 | +struct Solution; |
| 49 | + |
| 50 | +//leetcode submit region begin(Prohibit modification and deletion) |
| 51 | +impl Solution { |
| 52 | + pub fn to_hex(num: i32) -> String { |
| 53 | + if num == 0 { return "0".into(); } |
| 54 | + |
| 55 | + |
| 56 | + let mut leading_zero = false; |
| 57 | + let mut ret = String::new(); |
| 58 | + let mask: u32 = 0xf0000000; |
| 59 | + |
| 60 | + let mut looping_num: u32 = num as u32; |
| 61 | + for i in 0..8 { |
| 62 | + let i1 = (looping_num & mask) >> 28; |
| 63 | + let position_char = match i1 { |
| 64 | + 0 => '0', |
| 65 | + 1 => '1', |
| 66 | + 2 => '2', |
| 67 | + 3 => '3', |
| 68 | + 4 => '4', |
| 69 | + 5 => '5', |
| 70 | + 6 => '6', |
| 71 | + 7 => '7', |
| 72 | + 8 => '8', |
| 73 | + 9 => '9', |
| 74 | + 10 => 'a', |
| 75 | + 11 => 'b', |
| 76 | + 12 => 'c', |
| 77 | + 13 => 'd', |
| 78 | + 14 => 'e', |
| 79 | + 15 => 'f', |
| 80 | + _ => unreachable!() |
| 81 | + }; |
| 82 | + |
| 83 | + if position_char != '0' || leading_zero == true { |
| 84 | + ret.push(position_char); |
| 85 | + leading_zero = true; |
| 86 | + } |
| 87 | + |
| 88 | + looping_num <<= 4; |
| 89 | + } |
| 90 | + ret |
| 91 | + } |
| 92 | +} |
| 93 | +//leetcode submit region end(Prohibit modification and deletion) |
| 94 | + |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod test { |
| 98 | + use crate::convert_a_number_to_hexadecimal::Solution; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test1() { |
| 102 | + assert_eq!("1a".to_string(), Solution::to_hex(26)); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test2() { |
| 107 | + assert_eq!("0", Solution::to_hex(0)); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test3() { |
| 112 | + assert_eq!("ffffffff", Solution::to_hex(-1)) |
| 113 | + } |
| 114 | +} |
0 commit comments