LeetCode OJ 405. Convert a Number to Hexadecimal

Rocky Linux 8.10下用cephadm快速搭建Ceph集群(附华为云镜像源配置) 本文详细介绍了在Rocky Linux 8.10操作系统上使用cephadm快速部署Ceph集群的实战指南,包括环境准备、系统配置、华为云镜像源优化、集群初始化及节点扩展等关键步骤。通过配置华为云镜像源,显著提升部署效率,适合企业级存储解决方案的需求。 阅读详情

LeetCode OJ 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.

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.

Example1

Input:
26

Output:
“1a”

Example2

Input:
-1

Output:
“ffffffff”

解题思路

首先想到的处理方法就是对数进行位操作(bit manipulation)。要转换为16进制数,那么每次处理的就是低位的4bit,要取到低位的4bit可以用&操作得到,然后对取到的数进行判断,从而转换为‘0’~‘9’字符或‘a’~‘f’字符加入到结果字符串的末尾,然后将原来的数字右移四位。执行循环,直到正数变为0或者负数显示到32bit。

Note:

负数在计算机内部采用2的补码形式来存储,就是每一个二进制位都取反,得到的结果再加1。这样我们在处理负数时直接右移4bit时就可以了。(其实一开始也没注意到这一点,所以一开始是将负数转换为正数进行处理,后来才想起来计算机中负数的存储方式。)但是我们需要做一个限制:32bit,不然会产生死循环。

代码

个人github代码链接


class Solution {
public:
    string toHex(int num) {
        if(num == 0)
            return "0";
        string ans = "";
        int bit = 8;
        while (num && bit) {
            int tmp = num & 15;
            if (tmp < 10)
                ans = to_string(tmp) + ans;
            else
                ans = (char)('a' + tmp - 10) + ans;
            num = num >> 4;
            bit--;
        }
        return ans;
    }
};
【开关电源篇】开关电源——只需这三个阶段,从电源小白到维修大神 🔌 该篇为的原创文章跳转目录,小二持续更新跳转链接,尽情关注! 阅读详情

相关推荐

离散系统傅里叶变换的常用结论证明

本文介绍了离散系统傅里叶变换的三个重要结论:1)时域卷积对应频域相乘(公式1-3);2)时域相乘对应频域卷积(公式4-6);3)帕塞瓦尔定理(公式7)及其广义形式,证明信号能量在变换域保持不变。文末提供了MATLAB卷积运算演示程序,通过动画展示离散信号卷积过程(翻转、平移、相乘、累加),并以方波信号为例给出运行结果图。该程序直观呈现了卷积运算的步骤与结果。

newtton的博客 1312

LeetCode OJ 451. Sort Characters By Frequency

LeetCode OJ 451. Sort Characters By FrequencyDescriptionGiven a string, sort it in decreasing order based on the frequency of characters.Example1 Input: “tree” Output: “eert” Explanat

jan_deng的博客 507

NXP ChessDE的安装和激活

如果客户申请的是试用版,那么一份为期 30 天的 License 将会发送申请人的账号或邮箱中,客户使用该文件进行安装即可。② 将“VENDOR snpslmd /path/to/snpslmd”中的“/path/to/snpslmd”替换成 snpslmd.exe 实际存放路径(位于 SCL 安装目录下的 win32\bin 文件夹中,默认安装的情况下就是“VENDOR snpslmd C:\Synopsys\SCL\<version>\win32\bin\snpslmd.exe”)。

weixin_47351160的博客 2054

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder TraversalDescriptionGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do n

jan_deng的博客 488

交换机救命命令手册:华为 & 思科平台最全运维指令速查表

很多时候,真正拉你一把的不是“高深理论”,而是你手里有没有一份靠得住的命令速查表。这篇文章,希望能成为你“进可调试,退可排障”的一份救命卡片。

HuaweiLab_Peach的博客 1528

uniapp无感刷新token实现过程

可以看到已经触发了刷新token接口,并且重新请求上次失败的列表接口,无感刷新token逻辑已经正常,但为什么列表接口请求成功,还一直loading没关闭捏,就是因为后续代码未执行,await接口的返回没有成功resolve。路漫漫其修远兮,前端道路逐渐迷茫,时隔好久好久终于想起了我还有一个小博客,最近在一直在弄uniapp,属实有被恶心到,但也至少会用了,最近实现了一个比较通用的功能,就是无感刷新token,但在过程中发现一个问题就是。3、排查await的列表接口为何没有成功resolve。

weixin_40618068的博客 3604

LeetCode OJ 12. Integer to Roman

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路整数转换为罗马数字,用一个二维数组保存1-9,10-90,100-900

jan_deng的博客 441

Python结合海康威视工业相机SDK实现高效图像采集与处理

本文详细介绍了如何使用Python结合海康威视工业相机SDK实现高效的图像采集与处理。内容涵盖从环境搭建、SDK核心模块理解,到完整的图像采集流程拆解,包括设备枚举、参数设置、主动取流以及关键的像素格式转换。同时,文章深入探讨了触发模式、参数控制等高级应用,并提供了性能优化建议和可复用的相机操作类封装,帮助开发者快速构建稳定的机器视觉应用。

weixin_29043895的博客 361

LeetCode OJ 515. Find Largest Value in Each Tree Row

LeetCode OJ 515. Find Largest Value in Each Tree RowDescriptionYou need to find the largest value in each row of a binary tree.Example Input: 1 / \ 3 2 / \ \ 5 3 9 O

jan_deng的博客 432

Hierarchical Flow Partition(Top-down&&Bottom-up)

hierarchy flow是将设计切分成多个block,分别处理单个block,然后完成后,将它们进行拼接以完成最终的chip。一般划分为三个阶段:chip planning;physical budget就是pin assignmenthierarchy flow实现的方法有两种,一种是top-down,另一种是bottom-up,无论是哪种,都是都要通过flatten的方法分别全局范围进行优化实现。

qq_39493558的博客 3110

别再手动下单了!用Python+EasyTrader+同花顺,5分钟搞定你的第一个自动化交易机器人

本文详细介绍了如何使用Python和EasyTrader结合同花顺客户端快速构建自动化交易机器人。从环境配置到实战脚本编写,涵盖自动打新、条件单、跟单策略等核心功能,并分享性能优化与风险控制技巧,帮助投资者高效实现自动化交易。

weixin_30381317的博客 408

LeetCode OJ 207. Course Schedule

LeetCode OJ 207. Course Schedule典型的拓扑排序算法。谨以此题纪念期末机考时没AC这么简单的拓扑排序。upset……ingDescriptionThere are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for

jan_deng的博客 373

关于动态存储器dram中rank,bank,device之间的关系

DEVICE_WIDTH表示设备的带宽(单位bit),每个列(column)包含DEVICE_WIDTH 位(bit),每一行(row)包含NUM_COLS个column。   而每一个bank包含NUM_ROWS个行(row)。所以一个设备(device)的存储容量为PER_DEVICE_STORAGE =NUM_ROWS*NUM_COLS*DEVICE_WIDTH*NUM_BANKS (i

wyj7260的专栏 4742

LeetCode OJ 240. Search a 2D Matrix II

LeetCode OJ 240. Search a 2D Matrix IIDescriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in

jan_deng的博客 363

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

LeetCode OJ 13. Roman to Integer

LeetCode OJ 13. Roman to IntegerDescriptionGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.解题思路罗马数字转换为整数:我们把1,10,100,1000,5,500,500和对应的罗马数字做映射。

jan_deng的博客 356

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode OJ 566. Reshape the Matrix

LeetCode OJ 566. Reshape the MatrixDescriptionIn MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’r

jan_deng的博客 332

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LeetCode OJ 155. Min Stack

LeetCode OJ 155. Min StackDescriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element o

jan_deng的博客 330

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder TraversalDescriptionGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not

jan_deng的博客 324

LeetCode OJ 496. Next Greater Element I

LeetCode OJ 513. Find Bottom Left Tree ValueDescriptionYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nu

jan_deng的博客 313

LeetCode OJ 141. Linked List Cycle

LeetCode OJ 141. Linked List CycleDescriptionGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解题思路对于判断环的问题,可以采用双指针(快慢指针)的方法来判断。另外,因为题目中给的Lis

jan_deng的博客 308

LeetCode OJ 399. Evaluate Division

LeetCode OJ 399. Evaluate DivisionDescriptionEquations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some

jan_deng的博客 300

LeetCode OJ 226. Invert Binary Tree

LeetCode OJ 226. Invert Binary TreeDescriptionInvert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1方法一Recursion: 递归,每一次翻转左右子树。这种方法要每次

jan_deng的博客 301

LeetCode 热题 100-普通数组

因为nums[i]应该在的位置索引是nums[i]-1,所以如果nums[nums[i]-1](按索引找出来的值)不是nums[i],那就换位置。lambda是匿名函数,lambda表达式本身产生一个函数对象,可以赋值给变量,传给 函数,立即调用。找半天错误,发现不该和intervals前一个区间做对比,应该和list1的最后一个区间做对比。我原本的想法是创建一个新列表,把轮转后的数字放在新列表中,然后把新列表赋值给nums。列表原地排序,排序依据是每个元素(一个区间)的第 0 个值,即左端点。

m0_74153997的博客 168

LeetCode100 22.括号生成

该解法通过深度优先搜索生成所有合法括号组合。使用递归时传入剩余左括号数 left 和右括号数 right,剪枝条件为 left > right(防止右括号多于左括号),非法则返回。每次递归尝试添加左括号或右括号,当两者均为0时,得到一组合法结果。因字符串不可变,无需回溯,直接传递新串。最终返回所有合法括号组合。

Shaoxingzong的博客 162

LeetCode Hot 100 二分查找专题 34 / 33 / 153(Java)

专题主线:三道题共用同一副二分骨架,差别只在循环条件和收缩方向: 34.在排序数组中查找元素的第一个和最后一个位置:用 left <= right 练「边界二分」, 153.寻找旋转排序数组中的最小值:用 left < right + right = mid 练「下界模板」, 33.搜索旋转排序数组:是二者的组合应用。

2401_88612756的博客 571

LeetCode //C - 1325. Delete Leaves With a Given Value

This problem involves removing all leaf nodes from a binary tree that have a given target value, including cases where parent nodes become new leaves with the target value after deletion. The solution uses a postorder traversal approach (left-right-root) t

Made in Code 405
下一篇: LeetCode OJ 451. Sort Characters By Frequency
jan_deng
博客等级 码龄11年 1粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值