博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Java+Kotlin双语言的LeetCode刷题之路(二)
阅读量:6852 次
发布时间:2019-06-26

本文共 3836 字,大约阅读时间需要 12 分钟。

BasedLeetCode

LeetCode learning records based on Java,Kotlin,Python...

序号对应 LeetCode 中题目序号

9 判断一个整数是否是回文数。不能使用辅助空间

什么是回文数:“回文”是指正读反读都能读通的句子;如:"123321","我为人人,人人为我"等

  • Java 语言实现
public static boolean isPalindrome(int x) {        if (x < 0) {//负数不是回文数            return false;        } else if (x < 10) {            return true;        } else {            int highLength = 1;            //获取 x 的最高位数量级,如 x=123,highLength=100,x=23451,highLength=10000...            while ((x / highLength) >= 10) {                highLength = highLength * 10;            }            int lowLength = 10;            //(x / highLength) % 10 取待比较数的最高位数            //(x % lowLength) / (lowLength / 10) 取待比较数的最低位            while ((x / highLength) % 10 == (x % lowLength) / (lowLength / 10)) {                if (highLength < lowLength) {                    return true;                }                //相等时,最高位左移,最低位右移                lowLength = lowLength * 10;                highLength = highLength / 10;                if (highLength < lowLength) {                    return true;                }            }            return false;        }    }
  • Kotlin 语言实现
fun isPalindrome(x: Int): Boolean {        if (x < 0)            return false        if (x < 10)            return true        var highLength = 1        var lowLength = 10        while (x / highLength >= 10) {            highLength *= 10        }        while (x / highLength % 10 == x % lowLength / (lowLength / 10)) {            if (highLength < lowLength) return true            highLength /= 10            lowLength *= 10            if (highLength < lowLength) return true        }        return false    }

13 给定一个罗马数字,将其转换成整数

  • 1.对应罗马——整数的转换关系:I(1),V(5),X(10),L(50),C(100),D(500),M(1000)
  • 2.规则:
    • 2.1:相同的数字连写, 所表示的数等于这些数字相加得到的数。如 XXX表示 30;
    • 2.2:小的数字在大的数字的右边, 所表示的数等于这些数字相加得到的数 如VIII 表示8;
    • 2.3:小的数字(限于I, X, C)在大的数字的左边, 所表示的数等于大数减去小的数: 如IV 表示4;
    • 2.4:在一个数的上面画一条横线, 表示这个数增值1000倍(由于题目只考虑4000以内的数,所以这条规则不用考虑);
  • 3.组数规则:
    • 3.1:I, X, C: 最多只能连用3个, 如果放在大数的左边,只能用1个;
    • 3.2:V, L, D: 不能放在大数的左边,只能使用一个;
    • 3.3:I 只能用在V和X的左边。 IV表示4, IX表示9;
    • 3.4:X只能放在L,C左边。 XL 表示40, XC表示90;
    • 3.5:C只能用在D, M左边。 CD 表示400, CM表示900
  • Java 语言实现
public int romanToInt(String s) {        if (s.length() == 0) {            return 0;        }        int sum = 0;        for (int i = 0; i < s.length() - 1; i++) {            if (singleRomanToInt(s.charAt(i)) >= singleRomanToInt(s.charAt(i + 1))) {                sum += singleRomanToInt(s.charAt(i));            } else {                sum -= singleRomanToInt(s.charAt(i));            }        }        sum += singleRomanToInt(s.charAt(s.length() - 1));        return sum;    }    private int singleRomanToInt(char c) {        switch (c) {            case 'I':                return 1;            case 'V':                return 5;            case 'X':                return 10;            case 'L':                return 50;            case 'C':                return 100;            case 'D':                return 500;            case 'M':                return 1000;            default:                return 0;        }    }
  • Kotlin 语言实现
fun romanToInt(s: String): Int {        if (s.isEmpty()) {            return 0        }        var sum = 0        for (i in 0 until s.lastIndex) {            if (singleRomanToInt(s[i]) >= singleRomanToInt(s[i + 1]))                sum += singleRomanToInt(s[i])            else                sum -= singleRomanToInt(s[i])        }        sum += singleRomanToInt(s[s.lastIndex])        return sum    }    private fun singleRomanToInt(char: Char): Int {        if ('I' == char) return 1        if ('V' == char) return 5        if ('X' == char) return 10        if ('L' == char) return 50        if ('C' == char) return 100        if ('D' == char) return 500        if ('M' == char) return 1000        return 0    }

转载于:https://www.cnblogs.com/shen-hua/p/8776608.html

你可能感兴趣的文章
云上游戏数据分析实践
查看>>
前端如何实现数据双向绑定
查看>>
视频码率那些事
查看>>
Android仿网易云音乐:留声机效果
查看>>
vue-cli项目升级webpack4踩坑
查看>>
Python爬虫框架,内置微博、自如、豆瓣图书、拉勾、拼多多等规则
查看>>
android View 的绘制流程
查看>>
怎么实现mybatis半自动化解耦!看看资深程序员怎么说
查看>>
一个能拖动,能调整大小,能更新bind值的vue指令-vuedragx
查看>>
记一次基于vue-cli的多页面应用配置
查看>>
适用于小程序的 ES6
查看>>
Ribbon使用方法
查看>>
【译】将 Android 项目迁移到 Kotlin 语言
查看>>
vue 项目打包部署,通过nginx 解决跨域问题
查看>>
LightKV-高性能key-value存储组件
查看>>
小程序
查看>>
ES6变量的解构赋值
查看>>
ansible自动化运维详细教程及playbook详解
查看>>
快速解决Dev c++无法调试
查看>>
自学算法笔记
查看>>