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 ...
随机推荐
- Centos 8 安装 Nginx
安装步骤: /* 1. 安装编译工具及库文件 */ yum -y install gcc automake autoconf libtool make && yum install g ...
- 【Java Web开发学习】Spring MVC异常统一处理
[Java Web开发学习]Spring MVC异常统一处理 文采有限,若有错误,欢迎留言指正. 转载:https://www.cnblogs.com/yangchongxing/p/9271900. ...
- 整理了2019年上千道Java面试题,近500页文档,用了1个月时间!
spring 面试题 1.一般问题 1.1.不同版本的 spring Framework 有哪些主要功能? 1.2.什么是 spring Framework? 1.3.列举 spring Framew ...
- 阿里云https证书Apache配置
最近玩小程序,只支持https协议,于是给网站添加https支持.手上没钱,自己生成的证书不受信任,找到了阿里云的免费证书(PS:证书可以用在其他云主机上,不一定是阿里云的主机).如何购买下载不多说, ...
- matplotlib可视化最全指南
1. 折线图:plt.plot 设置数据:plt.plot(x,y),单列数据传入默认y轴,此时x轴数据默认从0逐渐对应递增 设置颜色:plt.plot(x,y,color/c=" &quo ...
- PAT1057 Stack(树状数组+倍增)
目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...
- hdu 6308 Time Zone (模拟+字符串处理)
Time Zone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- SpringBoot微服务电商项目开发实战 --- 模块版本号统一管理及Redis集成实现
上一篇文章总结了基于SpringBoot实现分布式微服务下的统一配置.分环境部署配置.以及服务端模块的分离(每一个提供者就是一个独立的微服务).微服务落地.Dubbo整合及提供者.消费者的配置实现.本 ...
- .NET Core Razor Pages中ajax get和post的使用
ASP.NET Core Razor Pages Web项目大部分情况下使用继承与PageModel中的方法直接调用就可以(asp-page),但是有些时候需要使用ajax调用,更方便些.那么如何使用 ...
- Cesium 本地部署案例
众所周知,cesium的服务器是搭建在国外的,所以我们国内的用户访问的时候贼慢.有时想查个api或者看个案例半天都进不去,今天我来说一下傻瓜式搭建本地的cesium环境,用于大家没事查资料用!步骤:1 ...