。。感觉做的很蠢。

主要就是看负数怎么处理。

举个例子,比如8位:

0111 1111 = 127

1111 1111 = -1

1000 0000 = -128

正常情况1111 1111应该是256,就是最大值127+最小值的绝对值128+1+num

其实点开那个提供的WIKI链接就一目了然了。。

用LONG是怕溢出

public class Solution {

    char[] b = new char[16];

    public String toHex(int num)
{
if(num == 0) return "0"; long a = 0;
if(num < 0)
a = (long)Integer.MAX_VALUE+(-1)*(long)Integer.MIN_VALUE+num+1;
else
a = (long) num; for(int i = 0; i < 10;i++) b[i] = (char)('0'+i);
b[10] = 'a';
b[11] = 'b';
b[12] = 'c';
b[13] = 'd';
b[14] = 'e';
b[15] = 'f'; return helper(a); } public String helper(long a)
{
String res = "";
while(a >= 16)
{
res += helper(a/16);
a%=16;
} return res + b[(int)a]; }
}

二刷。

通刷这个题也卡了,次奥。

看了新的简单的做法。

我们可以把4位4位的来看NUM,因为4位bits代表的就是16进制。

右移要用>>>而不是>>,因为后者会帮你添加符号,而我们恰好不能让它这么做。。因为16禁止最后是没符号的。

public class Solution {
public String toHex(int num) {
if(num == 0) return "0"; StringBuilder sb = new StringBuilder();
char[] digits = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; // map while(num != 0){
sb.append(digits[(num & 15)]);
num = num >>> 4;
} return sb.reverse().toString(); } }

顺便,cnblog该如何搜搜发过的随笔。。

405. Convert a Number to Hexadecimal的更多相关文章

  1. 38. leetcode 405. Convert a Number to Hexadecimal

    405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecim ...

  2. LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  3. 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. [leetcode] 405. Convert a Number to Hexadecimal

    https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...

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

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意:    十六进制中所有字母(a-f)都必须是小写.    十六进制字符串中不能包含多余的前导零.如果 ...

  6. LeetCode_405. Convert a Number to Hexadecimal

    405. Convert a Number to Hexadecimal Easy Given an integer, write an algorithm to convert it to hexa ...

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

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  8. Leetcode: Convert a Number to Hexadecimal

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...

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

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

随机推荐

  1. C#的输入输出流

      一 .NET Framework 类库的System.IO 命名空间 System.IO 命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型.      二 C#文件读写之Fi ...

  2. C++静态成员函数访问非静态成员的几种方法

    大家都知道C++中类的成员函数默认都提供了this指针,在非静态成员函数中当你调用函数的时候,编译器都会“自动”帮你把这个this指针加到函数形参里去.当然在C++灵活性下面,类还具备了静态成员和静态 ...

  3. 新增的output元素 progress元素 meter元素 keygen元素

    结果图 <output>是双标签 name:定义对象的唯一属性 for:定义输出域相关的一个或多个元素. form:定义所属的一个至多个表单. progress和meter一般和JS一起使 ...

  4. php批量发送短信或邮件的方案

    最近遇到在开发中遇到一个场景,后台管理员批量审核用户时候,需要给用户发送审核通过信息,有人可能会想到用foreach循环发送,一般的短信接口都有调用频率,循环发送,肯定会导致部分信息发送失败,有人说用 ...

  5. include()、include_once()与require()、require_once()的异同点

    相同点: 首先include().include_once()与require().require_once()都是用来包含并运行指定文件的,并且包含的文件在执行时在结构上是完全一样的. 例如:inc ...

  6. Hibernate报错 ** is not mapping

    使用easyui+struts+hibernate 新增加一个页面功能时,总是报错,后来发现是数据库语句,不能写表名称,而是要写映射的数据库实体类名 1.struts文件修改增加action < ...

  7. Android 安全概述

    1. 保密: 信息.文档加密.解密 2. 鉴别.认证:就像确认与我们打电话的确实是XXX,而不是骗子 3. 完整性:信息不能被篡改, 4. 不可否认性:确定信息有who产生,并且将来不能否认

  8. Laravel之路——file缓存修改为redis缓存

    1.Session: 修改.evn文件: SESSION_DRIVER:redis (如果还不行的话,修改config/session.php的driver) 2.缓存修改为redis 注意:使用 L ...

  9. bzoj 3624: [Apio2008]免费道路 生成树的构造

    3624: [Apio2008]免费道路 Time Limit: 2 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 111  Solved: 4 ...

  10. BZOJ 4008 亚瑟王

    Description 小K不慎被LL邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚瑟王是一个看脸的游 ...