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. Tif文件合并类

    using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...

  2. java8 集合对象间的处理

    eg1:List<CarVo> carVoList = carService.getList(carVo); List<String> listVins = carVoList ...

  3. 关于学习oi的一些事项

    我只是突然有感而发!(脑抽罢了 我其实是那种一直都没有计划说去学什么的人. 当然也不是那种点开洛谷一道题去写这道题不会就去学习相应的知识点的人. 随着洛谷 poj bzoj HDU CH Vojs 等 ...

  4. day3_字符串常用方法

    s.upper()s.lower()s.capitalize()s.split(',')s.strip('abc')s.lstrip()s.rstrip()s.replace('old','new') ...

  5. js中的事件轮询(event loop)机制

    异步任务指的是,不进入主线程.而进入"任务队列"(task queue)的任务,只有"任务队列"通知主线程,某个异步任务可以执行了,该任务才会进入主线程执行. ...

  6. Aop的基本介绍

    基本概念 通知  就是你想要的功能,也就是我们常说的安全.事物.日志等.先定义好这些,然后再想用的地方用一下.包含Aspect的一段处理代码 注意:其实这些功能(通知)并不是我们业务逻辑所必须的,只是 ...

  7. Python库源码学习1:Flask之app.run

    先列出app.run()实现的功能,我们以debug=True的情况下进行分析. 1. web服务器,处理http请求 2. 当代码修改后,重启服务器 那么app.run()是如何实现这两个功能的呢? ...

  8. NOIP初赛知识点

    http://www.doc88.com/p-9982181637642.html 连载中…… (一)八大排序算法 下面这张表摘自博客http://blog.csdn.net/whuslei/arti ...

  9. SpringBoot-Jar打包方式

    发布打包 Jar类型打包方式 1.使用mvn celan  package 打包 2.使用java –jar 包名 war类型打包方式 1.使用mvn celan package 打包 2.使用jav ...

  10. import Vue form 'vue’的意思

    1.import Vue form ‘vue’ 写全的话是import Vue from ‘…/nodemouls/vue/list/vue.js’: 此时在webpack.base.conf.js中 ...