题目
题解
class Solution {
public String toHex(int num) {
StringBuilder sb=new StringBuilder();
//存储表示0-15的数组
char [] array = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
//临界条件
if(num==0){
return "0";
}
while (sb.length() < 8 && num != 0) {
sb.append(array[num & 0xf]);
num >>= 4;
}
return sb.reverse().toString();
}
}
/*
if(num==0){
//边界条件
return "0";
}else {
//处理负数
long temp=(long)Math.pow(2,32);
if(num<0){
temp=num+temp;
}else{
temp=num;
}
//正整数取余换算
while(temp>0){
sb.append(array[(int)temp%16]);
temp=temp/16;
}
}
return sb.reverse().toString();
}
*/
通关详情
实在不知道如何处理那个负数(捂脸)