leetcode405
public class Solution
{
string ConverToHex(string bit)
{
var s = "";
switch (bit)
{
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "";
break;
case "":
s = "a";
break;
case "":
s = "b";
break;
case "":
s = "c";
break;
case "":
s = "d";
break;
case "":
s = "e";
break;
case "":
s = "f";
break;
}
return s;
} public string ToHex(int num)
{
var fuhao = ;
if (num < )
{
fuhao = -;
num = num * (-);
} //10进制转16进制,负数使用补码
var bitAry = new string[];
for (int i = ; i < bitAry.Length; i++)
{
bitAry[i] = "";
}
//STEP1:十进制转二进制
int index = ;
do
{
var x = num % ;
bitAry[index++] = x.ToString();
num = num / ;
} while (num != ); var list = bitAry.ToList(); //STEP2:正数使用原码,负数转补码
if (fuhao == -)
{
list.Clear(); bitAry[bitAry.Length - ] = "";//符号为设置为0 //计算反码
StringBuilder sbFan = new StringBuilder();
for (int i = ; i < bitAry.Length - ; i++)
{
if (bitAry[i] == "")
{
sbFan.Append("");
}
else
{
sbFan.Append("");
}
}
sbFan.Append("");//index=31的是符号位 //反码加1 var fanma = sbFan.ToString(); var step = ;
var first = true;
for (int i = ; i < fanma.Length - ; i++)
{
var x = Convert.ToInt32(fanma[i].ToString());
if (first)
{
first = false;
x = (x + ) % ;
list.Add(x.ToString());
if (x == )
{
step = ;
}
else
{
step = ;
}
}
else
{
list.Add(((x + step) % ).ToString());
if (x == && step == )
{
step = ;
}
else
{
step = ;
}
}
}
list.Add("");
} //STEP3:二进制转十六进制
string result = ""; if (fuhao == -)//负数
{
var temp = "";
for (int i = ; i < list.Count; i++)
{ if ((i + ) % != )//0,1,2
{
temp = list[i] + temp;
}
else
{
temp = list[i] + temp;
var hex = ConverToHex(temp);
result = hex + result;
temp = "";
}
}
}
else//正数
{
var temp = "";
for (int i = ; i < list.Count; i++)
{
if ((i + ) % != )
{
temp = list[i] + temp;
}
else
{
temp = list[i] + temp;
var hex = ConverToHex(temp);
result = hex + result;
temp = "";
} } } var notZeroIndex = -;
for (int i = ; i < result.Length; i++)
{
if (result[i] != '')
{
notZeroIndex = i;
break;
}
}
if (notZeroIndex > )
{
result = result.Substring(notZeroIndex);
} if (result == "")
{
result = "";
} return result;
}
}
https://leetcode.com/problems/convert-a-number-to-hexadecimal/#/description
leetcode405的更多相关文章
- [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
随机推荐
- 《DSP using MATLAB》Problem 3.5
定义为: 如果序列绝对可和,其DTFT就存在.
- Hadoop之HDFS
摘要:HDFS是Hadoop的核心模块之一,围绕HDFS是什么.HDFS的设计思想和HDFS的体系结构三方面来介绍. 关键词:Hadoop HDFS 分布式存储系统 HDFS是Hadoop的核心 ...
- nexage video asset tag
video ad can't show InLine must match the example ,and xml content is Case Sensitive https:/ ...
- BKL 大内核锁
BKL 大内核锁 BKL是一种递归锁.一个进程可以多次请求一个锁,并不会像自旋锁那么产生死锁. BKL可以在进程上下文中. BKL是有害的. 在内核中不鼓励使用BKL.一个执行线程可以递归的请求锁lo ...
- 升级CentOS 7.4内核版本的三种方案
https://blog.csdn.net/breeze915/article/details/79243673 在实验环境下,已安装了最新的CentOS 7.4操作系统,现在需要升级内核版本. 实验 ...
- ThinkPHP 5 insertall 只插入最后一条数据的问题
问题来源: Steed 2018/1/5 11:30:25 @流年 我用fetchsql查看的sql,发现数据都是最后一条 Steed 2018/1/5 11:30:39 我也不知道是什么问题,我打印 ...
- 洛谷4059找爸爸(Code+第一次月赛)
题目:https://www.luogu.org/problemnew/show/P4059 dp. 1.看出-A-B(k-1)可以理解成连续空格的第一个 -A,其余 -B: 2.把会干扰的“上一步右 ...
- jsp渲染
SP与Servlet什么关系?JSP和ASP什么关系?下面我们一一来探讨. 第一个.jsp文件:<html> <head> <title> ...
- Dubbo配置优化
来自于:https://blog.csdn.net/youaremoon/article/details/51884644 感谢博主,收藏一下 dubbo作为一个服务治理框架,功能相对比较完善,性能也 ...
- 执行Chrome自动化时--正在受到自动软件的控制的显示屏蔽
用selenium启动,浏览器出现‘Chrome正在受到自动软件的控制’ 屏蔽的方法: # coding:utf-8 from selenium import webdriver # 加启动配置 op ...