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的更多相关文章

  1. leetcode math类型题目解题总结

    2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution { publi ...

  2. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  3. 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 ...

  4. 2016.08.02 math(leetcode) 小结

    math(leetcode) 小结 在leetcode中有些知识点(套路) 判断一个数是不是能被某些数整除,可以用 n%x == 0,循环除的话,就将while(n%x == 0)的循环条件设置判断整 ...

  5. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  6. [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 ...

  7. [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 ...

  8. [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. ...

  9. Leetcode Tags(6)Math

    一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...

  10. [LeetCode] 504. Base 7_Easy tag: Math

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

随机推荐

  1. Docker下载tomcat

    命令 下载tomcat docker pull tomcat //默认是latest版本具体可以到 hub.docker.com上查询 //如果想下其他版本以9.0.16示例那么: docker pu ...

  2. SSM-配置tkmybatis

    引言 Mybatis 与 Hibernate的一个很大的区别就是Mybatis所有的数据库操作语句都需要自己写,对于简单的单表操作来说是比较烦琐的.因此有人就开发了tk.mybatis插件,通过这个插 ...

  3. 在MSSQL中的简单数据类型递归

    在某些特定的项目需求中,我们需要实现树状数据结构, 由此,我们需要用递归将数据查询出来. WITH T AS ( SELECT ID,PID FROM TableName WHERE ID=1 UNI ...

  4. re常用模块

    re模块:从字符串里面找到特定的字符串 re的基本语法(匹配规则) import re s = '王大炮打炮被大炮打死了 王大炮打炮被大炮打死了' ^:开头 print(re.findall('^王大 ...

  5. 《Java基础知识》流程控制

    流程控制分类: 一.顺序结构如果没有流程控制(即没有分支结构和循环结构),Java方法里面的语句是一个顺序执行流,从上到下依次执行每条语句. 二.分支结构1.if语句if语句使用布尔表达式或者布尔值作 ...

  6. 《Java基础知识》Java内部类及其实例化

    在 Java 中,允许在一个类(或方法.语句块)的内部定义另一个类,称为内部类(Inner Class),有时也称为嵌套类(Nested Class). 内部类和外层封装它的类之间存在逻辑上的所属关系 ...

  7. Nginx安装(我觉得我这篇可能是全网最清晰的一篇安装步骤了)

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/46aadb8f-5 ...

  8. Android获取ROOT权限的通用方法

    背景 自从Android问世以后,给手机获取ROOT权限变成了玩机爱好者老生常谈的话题.拥有手机,却不能拥有操作手机的最高权限,这对于手机爱好者而言,这怎么可以忍?所以无论Android升到什么什么版 ...

  9. 高精度模板 val.1

    目录 高精构造 结构体 char数组转高精: 高精加高精 高精乘单精 高精除单精 同样搬以前初三写的... 其实还有个val.2,搬到文章里去了 @ 在做一道斯特林数的时候被卡高精...于是滚来写一些 ...

  10. mysql与python的交互

    mysql是一种关系型数据库,是为了表示事物与事物之间的关系,本身存于数据库中的内容意义并不大,所以广泛应用于编程语言中,python中九含有与MySQL交互的模块 pymysql 编程对mysql的 ...