[LeetCode]--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:

All letters in hexadecimal (a-f) must be in lowercase.
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.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
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"

把一个十进制数转换成用字符串表示的16进制字符串,不需要过多的考虑负数问题,因为负数在做位操作时已经自动转换成了补码+1的形式直接进行正常操作即可
每四位取一次,用一个数组表示对应关系

public String toHex(int num) {
        if (num >= 0 && num < 10)
            return Integer.toString(num);
        char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                'b', 'c', 'd', 'e', 'f' };
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8 && num != 0; i++) {
            sb.insert(0, hex[num & 15]);
            num = num >> 4;
        }
        return sb.toString();
    }
时间: 2024-10-23 16:06:34

[LeetCode]--405. Convert a Number to Hexadecimal的相关文章

[LeetCode]109.Convert Sorted List to Binary Search Tree

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 无 [代码] 在下面超时的代码上进行改进:把链表先转换为vector再进行操作. [LeetCode]108.Convert Sorted Array to Binary Search Tree /*******************************

[LeetCode]108.Convert Sorted Array to Binary Search Tree

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 二分法,以中间元素i为根节点[start,i-1]递归构建左子树,[i+1,end]递归构建右子树 [代码] /********************************* * 日期:2014-12-28 * 作者:SJF0115 * 题目: 108.Convert Sorte

[LeetCode] Excel Sheet Column Number

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 实现代码: /*******************************

【LeetCode】9. Palindrome Number

题目: Determine whether an integer is a palindrome. Do this without extra space. 思考过程: 题目很简单,要求是判断一个数是否是回文数,回文数的定义就是数字翻转之后与原先的数一样的话就是回文数,比如 101 , 22, 1 等,所以要处理这个问题的话,只需要将一个数的最高位换到最低位,次高位换到第二低位,依次全部换好之后,得到一个新的数,判断新的数是否与原先的数相等,如果相等,就是回文数,如果不等,就不是,所以解答代码如

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

PHP+JS+rsa数据加密传输实现代码

JS端代码: 复制代码 代码如下: //文件base64.js: var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var b64pad="="; function hex2b64(h) { var i; var c; var ret = ""; for(i = 0; i+3 <= h.length; i+=3) { c = pars

Path to a Trusted Front-end Code Protection

Introduction As an appealing objective in the information security field, a trusted system refers to a system that achieves a certain degree of trustworthiness by implementing specific security policies. For computers, the Trusted Platform Module (TP

PHP+JS+rsa数据加密传输实现代码_php技巧

JS端代码: 复制代码 代码如下: //文件base64.js: var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var b64pad="="; function hex2b64(h) { var i; var c; var ret = ""; for(i = 0; i+3 <= h.length; i+=3) { c = pars

Python之路【2】:Python基础

入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈有点区别) 对于变量的作用域,执行声明并在内存中存在,如果变量在内存中存在就可以被调用. 1 if 1==1: 2 name = 'tianshuai' 3 print name 所以下面的说法是不对的: 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 1 result = 值1 if 条件 else 值2 例子: 1 name = raw_input("please input your name: &q