1. 程式人生 > >LeetCode 405. Convert a Number to Hexadecimal (進位制轉換,移位運算)

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:

  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"
輸入一個整數,輸出其十六進位制表示的字串。負數使用補碼錶示。

思路:用十進位制的15(二進位制的1111)作為掩碼,獲取最低的4位,直接查表對映成十六進位制。然後右移4位,進行下次對映。

在右移位時,注意要轉換成無符號整數,這樣移位時才能補0,否則將新增符號位。

    string toHex(int num) {
        if(num==0)return "0";
        string ans = "";
        string hexChar = "0123456789abcdef";

        while (num) {
            ans = hexChar[num & 15] + ans;
            num = (unsigned)num >> 4;
        }

        return ans;
    }


相關推薦

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 l

leetcode 405. Convert a Number to Hexadecimal

AD etc turn eth blank back ise LG round Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s co

LeetCode#405: Convert a Number to Hexadecimal

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

python leetcode 405. Convert a Number to Hexadecimal

涉及到進位制和加減乘除 很多時候考察的是位運算 注意的是 要先把負數轉為正數 class Solution: def toHex(self, num): """ :type num: int :rtype: str

LeetCode演算法題-Convert a Number to HexadecimalJava實現

這是悅樂書的第219次更新,第231篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第86題(順位題號是405)。給定一個整數,寫一個演算法將其轉換為十六進位制。對於負整數,使用二進位制補碼方法。例如: 輸入:26 輸出:“1a” 輸入:-1 輸出:“ffffffff”

資料結構與演算法 -- 棧的應用轉換、括號匹配

棧的應用 ps:用棧很簡單實現的應用有很多,比如說進位制轉換,括號匹配等。學計算機的都知道,2進位制,8進位制,10進位制,16進位制等,進位制之間的轉換也是需要掌握的,以備不時之需,所以我們可以自己寫一段程式如果會android的話,可以直接打包成APK。下面就按照這兩個應用稍微寫一點C語言的程式碼。 進

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

PAT 甲級 1019 General Palindromic Number轉換

1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For

洛谷p1582倒水思維好題數學2問題程式碼實現

題目連結:https://www.luogu.org/problemnew/show/P1582   題目猛一看挺難想,但想通了加的原理和合並的原理後就好說了。 肯定和2進位制是緊密相連的,每個瓶子的水升數一定是2的倍數(因為每次合的都是一樣的且都是2的倍數) 看透了這題後本質就是:將一個整

P1206 [USACO1.2]迴文平方數 Palindromic Squares轉換

題目描述 迴文數是指從左向右念和從右向左念都一樣的數。如12321就是一個典型的迴文數。 給定一個進位制B(2<=B<=20,由十進位制表示),輸出所有的大於等於1小於等於300(十進位制下)且它的平方用B進製表示時是迴文數的數。用’A’,’B’……表示10,

NOIP模擬 K轉換+快速冪

【題目描述】 給定一個K(2<=K<=16)進位制數a,判斷a是否能被K-1整除。 【輸入格式】 第一行是一個整數t(1<=t<=50),表示測試點數量。 對於每組資料,第一行一個整數K,表示進位制。 第二行一個K進位制數,表示a。保證a是合

noip初賽整理1.6&1.7&1.9轉換&資訊編碼表示&原碼補碼反碼

進位制轉換  基數與權         基數:某進位制計數制允許的基本數學符號的個數。一般而言,J進位制數的基數是J。         位權(權):...(無聊定義賊長)。如 11010 B 的權從高到低為16,8,4,2,1。 字尾字母          B:二進

資料結構之-鏈式棧及其常見應用轉換、括號匹配、行編輯程式、表示式求值等

1、棧的概念 棧(stack)又名堆疊,它是一種運算受限的線性表。其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧、入棧或壓棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素;從一個棧刪除元素又稱作出棧或退棧,它是把棧

學以致用——VBA實現十進位制數字轉換為字母二十六Convert a number to letters (Excel Column number to name) with VBA

Excel列標與列名轉換問題,本質上是一個十進位制數和二十六進位制數的轉換問題。記得以前學C、C#、JAVA等程式語言時,已經做過一些相關練習了。但是,老實說,在用公式法解決這個十進位制轉二十六進位制的問題時還真是難到我了,花了好幾個小時也沒有解決。於是,求助網路,在一篇文章

學以致用——使用VBA函式將十進位制數字轉換為字母二十六Convert a number to letters (Excel Column number to name)

利用空閒時間,開發了一個十進位制轉換為字母(相當於26進位制)的函式。功能有:1. 給定Excel列標,返回對應的列名(如,第677列對應的列名為“ZA”)(但是,Excel中的列最多為16384列,對應的列名為XFD)2. 給定任意正長整形數值(即,1到2147483647

LeetCode 171. Excel Sheet Column Number 字串、轉換

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2

杭電acm 1230 火星a+b

                          

HDU-4054.Hexadecimal View模擬十六轉換

M - M Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Hexadecimal is very important and

LeetCode 171. Excel Sheet Column Number(轉換)

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2

數論轉換a換成b

  數制轉換這類題解法很固定,常見的就兩種,昨天是第一種,今天是第二種。 題目:進位制轉換 時間限制:1 秒 記憶體限制:32 兆 題目描述:     求任意兩個不同進位制非負整數的轉換(2進位制~16進位制),所給整數在long所能表達的範圍之內。     不同進位制的表