★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-oshmbhty-bg.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complementmethod is used.git
Note:github
a-f
) must be in lowercase.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.Example 1:算法
Input: 26 Output: "1a"
Example 2:微信
Input: -1 Output: "ffffffff"
給定一個整數,編寫一個算法將這個數轉換爲十六進制數。對於負整數,咱們一般使用 補碼運算 方法。ide
注意:spa
a-f
)都必須是小寫。'0'
來表示;對於其餘狀況,十六進制字符串中的第一個字符將不會是0字符。 示例 1:code
輸入: 26 輸出: "1a"
示例 2:orm
輸入: -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 >>= 4 14 count += 1 15 } 16 return result 17 } 18 }
8mshtm
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 << 31 11 } else if tmp == 0 { 12 return "0" 13 } 14 15 while tmp != 0 { 16 let mod = tmp % base 17 tmp = tmp / base 18 19 if mod < 10 { 20 result = String(mod) + result 21 } else { 22 if let value = map[mod] { 23 result = value + result 24 } 25 } 26 } 27 28 return result 29 } 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 : num 10 while num > 0 { 11 result = map[num % 16] + result 12 num /= 16 13 } 14 15 return result 16 } 17 }
12ms
1 class Solution { 2 func toHex(_ num: Int) -> String { 3 let map = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","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] + res 10 temp >>= 4 11 } 12 return res 13 } 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)] + res 20 } 21 return res 22 } 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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"] 8 var num = num 9 var res = "" 10 var i = 0 11 12 while num != 0 && i < 8 { 13 res = map[num & 0b1111] + res 14 num >>= 4 15 i += 1 16 } 17 18 return res 19 } 20 }