Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. 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.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

Input:
26 Output:
"1a"

Example 2:

Input:
-1 Output:
"ffffffff"

Code    T: O(1)

class Solution:
def toHex(self, num):
ans, d = "", {10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f'}
if num == 0:
return ""
if num < 0:
num = 2**32 + num
while num > 0:
tem, rem = divmod(num, 16)
if rem > 9:
ans += d[rem]
else:
ans += str(rem)
num = tem
return ans[::-1]

[LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation的更多相关文章

  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

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

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

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

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

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

  6. 405. Convert a Number to Hexadecimal

    ..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...

  7. LeetCode 1290. Convert Binary Number in a Linked List to Integer

    题目 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN ...

  8. 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 ...

  9. how to convert a number to a number array in javascript without convert number to a string

    how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...

随机推荐

  1. vmware虚拟机与主机共享文件

    参考: http://blog.csdn.net/season_hangzhou/article/details/8162704 前言:本文提供的方法是吧windows主机上的文件夹共享给vmware ...

  2. 读《Spring源码深度解析》途中问题1

    step 1:检查自己的eclipse版本:在help->About Eclipse中查看: step 2:进入 https://github.com/groovy/groovy-eclipse ...

  3. 转载:mybatis踩坑之——foreach循环嵌套if判断

    转载自:作者:超人有点忙链接:https://www.jianshu.com/p/1ee41604b5da來源:简书 今天在修改别人的代码bug时,有一个需求是在做导出excel功能时,mybatis ...

  4. 无法在Web服务器上启动调试。

    Ⅰ x 操作超时 有关详细信息,请单击"帮助" x IIS--应用程序池--找到用到的程序池--回收   2 报这个错误的时候,我的IIS应用程序池只有一个>>> ...

  5. Orchard之Module开发

    一:生成新项目 首先,要启动 Code Generation,参考<Orchard之生成新模板>. 其次,进入命令行,输入: codegen module Tminji.Requireme ...

  6. 用CountDownLatch提升请求处理速度

    countdownlatch是java多线程包concurrent里的一个常见工具类,通过使用它可以借助线程能力极大提升处理响应速度,且实现方式非常优雅.今天我们用一个实际案例和大家来讲解一下如何使用 ...

  7. 线上bug处理

    http:response X-Frame-Options是什么? x-frame-options是一个HTTP响应头,用来告诉浏览器这个网页是否可以放在iframe内. x-frame-option ...

  8. stl, string不仅是charString, 更是byteString

     转载至:http://chzhou.blog.sohu.com/97459512.html 以前一直没有注意到STL中的string的length函数,但一直用它.天真的以为它会返回字符串的长度 ...

  9. MyEclipse中JDK运行环境和编译环境的设置

    一.设置myEclipse中新项目使用的JDK 1.运行环境   [Window]->[Preferences]->[Java]->[Installed JREs] 步骤:Add-- ...

  10. wait()函数的详细分析

    之前一直没太深入的去理解wait()函数,今天机缘巧合之前又看了看,发现之前没有真正的理解该函数. 众所周知,wait()函数一般用在父进程中等待回收子进程的资源,而防止僵尸进程的产生. (In UN ...