Solution#2
public class Solution {
public String toHex(int num) {
if (num == 0) return "0";
String[] map = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
StringBuilder sb = new StringBuilder();
while (num != 0) {
sb.append(map[num & 15]);
num = (num >>> 4);
}
return sb.reverse().toString();
}
}
Solution#1
Problem#2
- 帶符號右移,無符號右移
Problem#1
- 試着用位操作去解決,而不是通過除法