使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode
LeetCode learning records based on Java,Kotlin,Python...Github 地址
序号对应 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
}
使用Java+Kotlin双语言的LeetCode刷题之路(二)的更多相关文章
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- LeetCode刷题预备知识(二)
Python四大数据结构的属性及方法 在LeetCode刷题预备知识一中我们掌握了常见的内置函数,和四大数据结构的基本概念: 但只掌握这些还远远不够,我们还需了解四大数据结构的属性及方法才能更高效快速 ...
- #leetcode刷题之路28-实现 strStr() 函数
实现 strStr() 函数.给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回 ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- #leetcode刷题之路3-无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- #leetcode刷题之路50-Pow(x, n)
实现 pow(x, n) ,即计算 x 的 n 次幂函数.示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100 #inclu ...
- #leetcode刷题之路49-字母异位词分组
给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串.示例:输入: ["eat", "tea", "tan" ...
随机推荐
- vue+webpack安装sass过程中遇到权限不够,直接删除node_modus文件夹重新安装,node_modus先取得管理员权限才能删
vue vue-style-loader !css-loader错误 最近在学习vue框架,使用webpack打包vue项目,在执行npm run start的时候 出现如下错误: This depe ...
- 用scrapy爬取亚马逊网站项目
这次爬取亚马逊网站,用到了scrapy,代理池,和中间件: spiders里面: # -*- coding: utf-8 -*- import scrapy from scrapy.http.requ ...
- 采用spring的schedule注解配置定时任务
1 在springmvc配置文件中新增以下配置 <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 --> <task:scheduled-tasks s ...
- 2017-2018-2 20155314《网络对抗技术》Exp4 恶意代码分析
2017-2018-2 20155314<网络对抗技术>Exp4 恶意代码分析 目录 实验要求 实验内容 实验环境 基础问题回答 预备知识 实验步骤 1 静态分析 1.1 使用virsca ...
- 理解ES7中的async/await
理解ES7中的async/await 优势是:就是解决多层异步回调的嵌套 从字面上理解 async/await, async是 "异步"的含义,await可以认为是 async w ...
- 动态分析Android程序
快速定位程序关键点 代码注入法 插入log函数,输出调试信息. const-string v0,"TAG" const-string v1,"info" inv ...
- 学习CSS布局 - dispaly属性
"display"属性 display 是CSS中最重要的用于控制布局的属性. 每个元素都有一个默认的 display 值,这与元素的类型有关. 对于大多数元素它们的默认值通常是 ...
- kubespray -- k8s集群dashboard 访问方式
1.参考这篇文章: https://github.com/kubernetes/dashboard/wiki/Creating-sample-user 创建用户 2.获取token 3.kubectl ...
- 前端知识点总结(html+css)部分
HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...
- 【小程序】模拟数据支持(mockjs配置模拟服务器接口数据)
utils目录 ①下载mockjs(地址)放置utils目录中 ②新建api.js :配置模拟数据以及后台接口,通过DEBUG=ture; //切换数据接口 配置如下: let API_HOST = ...