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. unittest自动化测试框架

    目录 框架的概念 Unittest单元测试框架 常用的assert语句 unittest创建测试代码的方式: unittest构建测试套件(测试用例集合): unittest忽略测试用例: 运行测试集 ...

  2. vue 组件传递值以及获取DOM元素的位置信息

    1.父组件 select_li.vue 1.1 父组件模板 <template> <div id='selectLi' ref="selectLi"> &l ...

  3. 详解 TCP 超时与重传机制——长文预警

    上一篇介绍 TCP 的文章「TCP 三次握手,四次挥手和一些细节」反馈还不错,还是蛮开心的,这次接着讲一讲关于超时和重传那一部分. 我们都知道 TCP 协议具有重传机制,也就是说,如果发送方认为发生了 ...

  4. 【HttpClient】使用学习

    HttpClient使用学习 HttpClient Tutorial:http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/in ...

  5. SpringBoot电商项目实战 — 前后端分离后的优雅部署及Nginx部署实现

    在如今的SpringBoot微服务项目中,前后端分离已成为业界标准使用方式,通过使用nginx等代理方式有效的进行解耦,并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构.多端化服务(多 ...

  6. java嵌套接口

    java嵌套接口 package object; class A { //嵌套在类中的接口,可以被private,protected,default和public四种权限修饰 interface B ...

  7. DOM事件流的三个阶段

    事件发生时会在元素节点之间按照特定的顺序传播,这个传播过程即DOM事件流. DOM事件流分为三个阶段,分别为: 捕获阶段:事件从Document节点自上而下向目标节点传播的阶段: 目标阶段:真正的目标 ...

  8. 关于EXIT和BADI增强的查找

    EXIT出口的查找: 方法一: 第一步:通过SE30,输入TCODE(例如ME21N),执行EXCUTE,前台创建一张采购订单.点击TIMES页签,查找EXIT开头的SAP程序.  第二步:这些fun ...

  9. linux globbing文件名通配

    globbing:文件名通配 元字符: *:匹配任意长度的任意字符 ?:匹配任意单个字符 []:匹配指定范围内的任意单个字符 [a-z]或者[A-Z]或者[[:alpha:]]:匹配任意一个字母 [[ ...

  10. CMake工程找不到相应的cuDNN版本的问题

    (1) 去官网下载相应的版本,因为电脑之前安装的是 CUDA8. ,找跟 CUDA 版本兼容的 cuDNN 下载即可,我选择的是 cuDNN v7.(Deb) 和  cuDNN v7.1.4 Deve ...