405. Convert a Number to Hexadecimal

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"

分析:将一个整数的二进制补码表示转化成对应十六进制的表示形式。正整数的补码就是它本身的原码表示,负整数的补码为它对应的正数的原码取反后加1。得到最终的二进制补码后,每次取出它的后4位表示的数,可以通过将它和0xF相与,也可以将它对16取模,取完右移四位,直到原数被被移位为0。

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

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

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

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

  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_Easy tag: Bit Manipulation

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

  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_405. Convert a Number to Hexadecimal

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

  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. Leetcode: Convert a Number to Hexadecimal

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

随机推荐

  1. java 局部变量几点笔记

    1.局部变量的作用时间很短暂,都被存储在方法的栈内存中:2.(没使用static)非静态变量=实例变量:(使用static)静态变量=类变量3.成员变量:类体内定义的变量:4.局部变量有三种:1)形参 ...

  2. 纯CSS3实现圆形进度条动画

    悄悄地,GIF 格式的进度条已经越来越少,CSS 进度条如雨后春笋般涌现.今天要介绍的这个 CSS3 进度条,效果和 Flyme OS 4 上的加载动画一样. 首先,来看下最终的效果: 它的 HTML ...

  3. ORA-00245问题总结

    (1)问题描述 在进行数据库归档备份时(备份归档日志文件和控制文件),有时成功,有时失败,失败报错如下: RMAN-00571: =================================== ...

  4. .NET Core 2.0 Preview2 发布汇总

    前言 关于 ASP.NET Core 2.0 的新功能可以查看我的这篇博客. 这篇文章是 Priview2中的一些改进. .NET Core 2.0 - Preview2 Azure 的改进 Dock ...

  5. jqueryl操作dom文档实例

    <include file="Public:header"/> <link rel="stylesheet" href="../Pu ...

  6. group by和count联合使用问题

    要根据用户发布的产品数量来排序做分页,使用group ) FROM( SELECT uid,COU 工作中要根据用户发布的产品数量来排序做分页,使用group by uid 用count(uid) 来 ...

  7. Chapter 1. Introduce

    前言 本书全名是<H.264 and MPEG-4 Video Compression, Video Coding For Next-generation Multimedia>,作者为 ...

  8. 【Android Developers Training】 57. 在UI线程之外处理图像

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. JS初步学习

    [使用JS的三种方式] 1.HTML标签中内嵌JS(不提倡使用): <button onclick="javascript:alert('小碧池!你真点啊!')">有本 ...

  10. Ionic2+ 环境搭建

    ionic2+官方guide 基础环境安装 nodejs安装 ionic,cordova安装 npm install -g ionic cordova 项目创建 ionic start MyIonic ...