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 ...
随机推荐
- Wireshark数据包分析入门
Wireshark数据包分析(一)——使用入门 Wireshark简介: Wireshark是一款最流行和强大的开源数据包抓包与分析工具,没有之一.在SecTools安全社区里颇受欢迎,曾一度超越 ...
- 还不知道如何实践微服务的Java程序员,这遍文章千万不要错过!
作者:古霜卡比 前言 本文将介绍微服务架构和相关的组件,介绍他们是什么以及为什么要使用微服务架构和这些组件.本文侧重于简明地表达微服务架构的全局图景,因此不会涉及具体如何使用组件等细节. 要理解微服务 ...
- 通过url返回的状态来抢注好的用户名
之前在注册很多网站时都想取一个好的用户名,但是不知道那些被注册了没有,通常时一个一个测试,但是很慢当时就想过这个思路,由于懒并没有去搞 主要思路就是:很多网站的用户主页的Url都存在用户名,替换为自己 ...
- 关于eclipse码代码时光标自动消失要重新点击输入框的问题
前几天码代码时在两个电脑都出现了同样的问题,就是在输入的时候,输入法突然从程序框切换到某不可名状的位置,要重新点击输入框才能解决.(后发现不但是eclipse,任何带有输入框的都会出现此问题) 经排查 ...
- SpringBoot整合axis1.4后,@Autowired注入失败,使用工具类注入
问题描述: 费劲心思搭建好webservices服务端后,没想到客户端调用失败,查看日志文件,发现报空指针异常,debug代码后,发现sql查询的值都是null.通常情况下,我们将Dao注入Servi ...
- Maven pom.xml 全配置(一)常用配置
Maven pom.xml 全配置(一)常用配置 这里贴出一个Maven中出现频率较高的配置参数注释,方便理解项目中Maven的配置具体的作用.如果在此博文中没有找到你想看到的参数,可以移步Maven ...
- abp模块化开发之通用树1:基本使用
一.概述 有些功能在单个项目或多个项目被重复使用,比如:附件,同一个系统中的多个模块都可能使用到,不同项目也有需要.再比如:有无限级分类的树形功能,区域.产品分类.数据字典等.最简单粗暴的办法是直接复 ...
- leaflet-webpack 入门开发系列六矢量瓦片(附源码下载)
前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...
- windows下MySQL解压版安装
MySQL的安装 一.前期准备 获取MySQL解压版安装包(本文使用的是 [mysql-5.7.28-winx64.zip]版本) 获取方式: 通过官网下载,官方下载地址:“https://dev.m ...
- opencv检测图像直线
#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...