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 ...
随机推荐
- JS基础-事件
事件机制 事件触发三阶段 事件触发有三个阶段: window 往事件触发处传播,遇到注册的捕获事件会触发 传播到事件触发处时触发注册的事件 从事件触发处往 window 传播,遇到注册的冒泡事件会触发 ...
- 【hibernate】存储图片
[hibernate]存储图片 转载: package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import j ...
- flask之分析线程和协程
flask之分析线程和协程 01 思考:每个请求之间的关系 我们每一个请求进来的时候都开一个进程肯定不合理,那么如果每一个请求进来都是串行的,那么根本实现不了并发,所以我们假定每一个请求进来使用的是线 ...
- Educational Codeforces Round 78 (Rated for Div. 2)
A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...
- Mysql服务彪高排查方式及索引的正确使用步骤
原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/12af580d-1 ...
- python 正则表达式re使用模块(match()、search()和compile())
摘录 python核心编程 python的re模块允许多线程共享一个已编译的正则表达式对象,也支持命名子组.下表是常见的正则表达式属性: 函数/方法 描述 仅仅是re模块函数 compile(patt ...
- Python面向对象-获取对象信息type()、isinstance()、dir()
type() type()函数用于判断对象类型: >>> type(11) <class 'int'> >>> type('abc') <clas ...
- 基于Vue的前后端分离项目实践
一.为什么需要前后端分离 1.1什么是前后端分离 前后端分离这个词刚在毕业(15年)那会就听说过,但是直到17年前都没有接触过前后端分离的项目.怎么理解前后端分离?直观的感觉就是前后端分开去做,即功 ...
- HA: Infinity Stones Vulnhub Walkthrough
下载地址: https://www.vulnhub.com/entry/ha-infinity-stones,366/ 主机扫描: 目录枚举 我们按照密码规则生成字典:gam,%%@@2012 cru ...
- spring boot 2 + shiro 实现简单的身份验证例子
Shiro是一个功能强大且易于使用的Java安全框架,官网:https://shiro.apache.org/. 主要功能有身份验证.授权.加密和会话管理.其它特性有Web支持.缓存.测试支持.允许一 ...