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 0
s. 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. The given number is guaranteed to fit within the range of a 32-bit signed integer. You must not use any method provIDed by the library which converts/formats the number to hex directly. Example 1:
input:26Output:"1a"
Example 2:
input:-1Output:"ffffffff"
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
注意:
十六进制中所有字母(a-f
)都必须是小写。 十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符‘0‘
来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。 给定的数确保在32位有符号整数范围内。 不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。 示例 1:
输入:26输出:"1a"
示例 2:
输入:-1输出:"ffffffff"
8ms
1 class Solution { 2 var hex:String = "0123456789abcdef" 3 func toHex(_ num: Int) -> String { 4 if num == 0 {return "0"} 5 var number:Int = num 6 var result:String = "" 7 var count:Int = 0 8 while(number != 0 && count < 8) 9 {10 var index = hex.index(hex.startIndex,offsetBy: (number & 0xf))11 result.insert(hex[index],at: result.startIndex)12 //注意符号:>>=13 number >>= 414 count += 115 }16 return result17 }18 }
8ms
1 class Solution { 2 func toHex(_ num: Int) -> String { 3 var result = "" 4 var map = [10:"a",11:"b",12:"c",13:"d",14:"e",15:"f"] 5 6 let base = 16 7 var tmp = num 8 9 if tmp < 0 {10 tmp += 2 << 3111 } else if tmp == 0 {12 return "0"13 }14 15 while tmp != 0 {16 let mod = tmp % base17 tmp = tmp / base18 19 if mod < 10 {20 result = String(mod) + result21 } else {22 if let value = map[mod] {23 result = value + result24 }25 }26 }27 28 return result29 }30 }
12ms
1 class Solution { 2 func toHex(_ num: Int) -> String { 3 guard num != 0 else { 4 return "0" 5 } 6 7 let map = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"] 8 var result = "" 9 var num = num < 0 ? Int(UInt32.max) + num + 1 : num10 while num > 0 {11 result = map[num % 16] + result12 num /= 1613 }14 15 return result16 }17 }
12ms
1 class Solution { 2 func toHex(_ num: Int) -> String { 3 let map = ["0","f"] 4 if num > 0 { 5 var temp = num 6 var res = "" 7 while temp != 0 { 8 let v = temp & 15 9 res = map[v] + res10 temp >>= 411 }12 return res13 } else if num == 0 {14 return "0"15 } else {16 var res = ""17 for i in 0..<8 {18 let v = num & (15 << (i*4))19 res = map[v>>(i*4)] + res20 }21 return res22 }23 }24 }
16ms
1 class Solution { 2 func toHex(_ num: Int) -> String { 3 if num == 0 { 4 return "0" 5 } 6 7 let map = ["0","f"] 8 var num = num 9 var res = ""10 var i = 011 12 while num != 0 && i < 8 {13 res = map[num & 0b1111] + res14 num >>= 415 i += 116 }17 18 return res19 }20 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal全部内容,希望文章能够帮你解决[Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)