405. Convert a Number to Hexadecimal
。。感觉做的很蠢。
主要就是看负数怎么处理。
举个例子,比如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的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- 405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果 ...
- 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 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- Leetcode: Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...
- [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
随机推荐
- windows下node.js+sublime中安装coffeescript
node.js中安装Coffeescript 1.我的node.js安装目录 2.node.js 全局模块所在目录 3.node.js安装coffeescript npm install -g c ...
- js学习--DOM操作详解大全 前奏(认识DOM)
一 . 节点属性 DOM 是树型结构,相应的,可以通过一些节点属性来遍历节点树: 方法 说明 nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeNa ...
- js 中特殊形势的函数-匿名函数的应用
javascript中的匿名函数,那什么叫做匿名函数? 匿名函数就是没有函数名称:演示代码: <script> function(x,y){ return x+y //这个就是一个匿名函数 ...
- linux安装python使用的MySQLdb
安装mysqldb模块需已安装mysql 使用pip安装MySQLdb pip install mysql-python mac os安装mysqldb sudo pip install mysql- ...
- 在oj平台上练习的一些总结【转】
程序书写过程中的一些小技巧:1. freopen(“1.txt”,”r”,stdin); //程序运行后系统自动输入此文档里面的内容(不需要进行手动输入)freopen(“1.txt”,”w”,std ...
- iOS代码实现:创建按钮,绑定按钮事件,读取控件值
// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...
- pragma指令简介
整理日:2015年3月12日 资源来来自己网络 在编写程序的时候,我们经常要用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. 下面介绍了一下该指令的一些常用参数,希望对大家 ...
- vbox里面的Ubuntu虚拟机与主机win7之间设置共享文件夹
有时候我们希望虚拟机和主机之间进行通信,例如传一些文件.那么设置共享文件夹就是一种很好的方式. 这里我的主机是win7系统,vbox里面的虚拟机是Ubuntu. 1.首先安装vbox的VBOXGues ...
- 转:使用Tengine替代Nginx作为负载均衡服务器
原文来自于:http://heylinux.com/archives/2938.html Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级 ...
- nodejs广播
http://site.douban.com/185124/widget/notes/10805558/note/240909343/ http://t42dw.iteye.com/blog/1767 ...