leetcode-math
Reverse Integer
/**
* Given a 32-bit signed integer, reverse digits of an integer.
*
*/
public class Lc7 {
public static int reverse(int x) {
boolean negativeNumberFlag = false;
if (x < 0) {
negativeNumberFlag = true;
x *= -1;
}
int len = 0;
int temp = x;
while (temp > 0) {
temp /= 10;
len++;
}
int[] array = new int[len];
for (int i = 0; i < len; i++) {
array[i] = (int) (x % Math.pow(10, i + 1) / Math.pow(10, i));
}
long digital = 0;
for (int i = array.length - 1, j = 0; i >= 0; i--, j++) {
digital += array[i] * Math.pow(10, j);
}
digital = negativeNumberFlag == true ? digital * -1 : digital;
return digital > Integer.MAX_VALUE || digital < Integer.MIN_VALUE ? 0 : (int) digital;
}
public static void main(String[] args) {
int digit = 1534236469;
System.out.println(reverse(digit));
}
}
Compare Version Numbers
/**
*
* Compare two version numbers version1 and version2. If version1 > version2
* return 1; if version1 < version2 return -1;otherwise return 0.
*
* You may assume that the version strings are non-empty and contain only digits
* and the . character.
*
* The . character does not represent a decimal point and is used to separate
* number sequences.
*
* For instance, 2.5 is not "two and a half" or "half way to version three", it
* is the fifth second-level revision of the second first-level revision.
*
* You may assume the default revision number for each level of a version number
* to be 0. For example, version number 3.4 has a revision number of 3 and 4 for
* its first and second level revision number. Its third and fourth level
* revision number are both 0.
*/
public class Lc165 {
public static int compareVersion(String version1, String version2) {
int p1 = 0, p2 = 0;
while (p1 < version1.length() || p2 < version2.length()) {
int num1 = 0, num2 = 0;
while (p1 < version1.length() && version1.charAt(p1) != '.')
num1 = num1 * 10 + (version1.charAt(p1++) - '0'); // get number in version1..
while (p2 < version2.length() && version2.charAt(p2) != '.')
num2 = num2 * 10 + (version2.charAt(p2++) - '0'); // get number in version2.
if (num1 != num2)
return num1 > num2 ? 1 : -1;
p1++;
p2++;
}
return 0;
}
public static void main(String[] args) {
String str1 = "1.0";
String str2 = "1";
System.out.println(compareVersion(str1, str2));
}
}
leetcode-math的更多相关文章
- leetcode math类型题目解题总结
2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution { publi ...
- [leetcode][math] Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- leetcode@ [343] Integer Break (Math & Dynamic Programming)
https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...
- 2016.08.02 math(leetcode) 小结
math(leetcode) 小结 在leetcode中有些知识点(套路) 判断一个数是不是能被某些数整除,可以用 n%x == 0,循环除的话,就将while(n%x == 0)的循环条件设置判断整 ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- [LeetCode] 492. Construct the Rectangle_Easy tag: Math
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- [LeetCode] 441. Arranging Coins_Easy tag: Math
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- [LeetCode] 258. Add Digits_Easy tag: Math
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Leetcode Tags(6)Math
一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...
- [LeetCode] 504. Base 7_Easy tag: Math
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
随机推荐
- Python流程控制之循环结构
目录 while循环 for循环 嵌套循环 break.continue.pass 练习 当出现有规律或者是重复的事情就可以使用循环. 1.循环变量初始化 2.循环条件 3.循环体 4.改变循环变量 ...
- harbor部署常见的错误
总结部署harbor过程所遇到的一些坑 1:在使用docker push镜像的时候提示:denied: requested access to the resource is denied,用户和 ...
- OPPO R11刷机初体验
刷机目的 最初打算是用旧手机搭一个服务器,首先想到的是刷一个Linux系统,但这太难了,我搞不定,然后就想着可以用一些软件比如KSWEB之类的来代替,但是想要访问80端口的话还是需要root,但是普通 ...
- Net Core 基于AngleSharp的HTML转实体工具
最近这几天在采集一些房产信息网站的二手房产数据.采用的是.net core 2.2+AngleSharp做的,放在自己服务器上跑着玩.写着写着,发现好麻烦.原因如下 部分代码如下图 1.每个节点都要手 ...
- SSM非springboot配置swagger2
前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...
- C语言中表达n次方
C语言中表达n次方可以用pow函数. 函数原型:double pow(double x, double y) 功 能:计算x^y的值 返 回 值:计算结果 举例: double a; a = p ...
- 建议2:注意Javascript数据类型的特殊性---(1)防止浮点数溢出
1.防止浮点数溢出 二级制的浮点数不能正确地处理十进制的小数,因此 0.1+0.2不等于0.3 num = 0.1+0.2; //0.30000000000000004 这是JavaScript中经常 ...
- 《Dotnet9》建站-记录建站过程中使用的一些网址
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- RabbitMQ之交换机及spring整合
交换机 交换机属性: Name:交换机名称 Type:交换机类型 direct.topic.fanout.headers Durability:是否需要持久化,true为持久化 Auto Delete ...
- 升级sharepoint2013遇到的坑
现在要将sharepoint2010,ProjectServer2010升级到2016的版本,需要先升级到2013的版本. 按照官方文档,瞎搞将sharepoint2010升级到2013的版本,中间出 ...