LeetCode Convert a Number to Hexadecimal

原题链接在这里:https://leetcode.com/problems/convert-a-number-to-hexadecimal/

题目:

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0‘; otherwise, the first character in the hexadecimal string will not be the zero character.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

Input:
26
Output:
"1a"

Example 2:

Input:
-1
Output:
"ffffffff"

题解:

每4位组成一个hexadecimal digit, 每次取出最后4位和1111做位运算&得到一个digit.

num 右移4位,这里用>>>而不是>>. e.g. -1的two’s complement表示为ffffffff, 32位全是1 bit. 若用>> 首位的1 bit不会移动, while loop的condition num!=0不会出现.

Time Complexity: O(1). Space: O(1).

AC Java:

 1 public class Solution {
 2     public String toHex(int num) {
 3         if(num == 0){
 4             return "0";
 5         }
 6
 7         StringBuilder sb = new StringBuilder();
 8         while(num != 0){
 9             int digit = num & 0xf;
10             sb.insert(0, digit<10 ? (char)(digit+‘0‘) : (char)(digit-10+‘a‘));
11             num >>>= 4;
12         }
13         return sb.toString();
14     }
15 }
时间: 2024-08-07 07:57:12

LeetCode Convert a Number to Hexadecimal的相关文章

[LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. Note: All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must not contain extra leading 0s. If the nu

38. leetcode 405. Convert a Number to Hexadecimal

405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's complement method is used. Note: All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must no

【leetcode?python】Convert a Number to Hexadecimal

#-*- coding: UTF-8 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',\            10:'a',            11:'b',            12:'c',            13:'d',            14:'e',            15:'f'}            def t

405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's complement method is used. Note: All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must not contain extra leading 0s. If the nu

[Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complementmethod is used. Note: All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must not contain extra leading 0s. If the num

405 Convert a Number to Hexadecimal 数字转换为十六进制数

给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意:    十六进制中所有字母(a-f)都必须是小写.    十六进制字符串中不能包含多余的前导零.如果要转化的数为0,那么以单个字符'0'来表示:对于其他情况,十六进制字符串中的第一个字符将不会是0字符.     给定的数确保在32位有符号整数范围内.    不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法.示例 1:输入:26输出:"1a"示例 2:输入:-1输出:"

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be ver

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen