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刷题之路(二)的更多相关文章

  1. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  3. LeetCode刷题预备知识(二)

    Python四大数据结构的属性及方法 在LeetCode刷题预备知识一中我们掌握了常见的内置函数,和四大数据结构的基本概念: 但只掌握这些还远远不够,我们还需了解四大数据结构的属性及方法才能更高效快速 ...

  4. #leetcode刷题之路28-实现 strStr() 函数

    实现 strStr() 函数.给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回 ...

  5. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  6. #leetcode刷题之路3-无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...

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

  8. #leetcode刷题之路50-Pow(x, n)

    实现 pow(x, n) ,即计算 x 的 n 次幂函数.示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100 #inclu ...

  9. #leetcode刷题之路49-字母异位词分组

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串.示例:输入: ["eat", "tea", "tan" ...

随机推荐

  1. 【C++学习笔记】变量初始化规则

    全局变量和静态变量会自动初始化为0,堆和栈中的局部变量不会初始化而拥有不可预测的值. C++保证了所有对象与对象成员都会初始化,但其中基本数据类型的初始化还得依赖于构造函数(或初始化列表). 成员变量 ...

  2. File类_常见的方法(获取目录中指定规则的内容)

    首先定义过滤器 import java.io.File; import java.io.FilenameFilter; public class FileByJava implements Filen ...

  3. golang []byte和string相互转换

    测试例子 package main   import (     "fmt" )   func main() {     str2 := "hello"     ...

  4. 【转】BAT批处理中的字符串处理详解(字符串截取)

    下面对这些功能一一进行讲解. 1.截取字符串 截取字符串可以说是字符串处理功能中最常用的一个子功能了,能够实现截取字符串中的特定位置的一个或多个字符.举例说明其基本功能: @echo off set ...

  5. k8s搭建问题(1)--OOMKilled

    kubectl describe pods ****** --namespace=****** 现象 Host Port: /TCP State: Waiting Reason: CrashLoopB ...

  6. MyBatis之反射技术+JDK动态代理+cglib代理

    一.反射 引用百度百科说明: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功 ...

  7. PAT A1028 List Sorting (25 分)——排序,字符串输出用printf

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  8. 201904:Action recognition based on 2D skeletons extracted from RGB videos

    论文标题:Action recognition based on 2D skeletons extracted from RGB videos 发表时间:02 April 2019 解决问题/主要思想 ...

  9. Linux加密、安全版块、root密码破解

    当一个入侵者进入了你的系统并且种植了木马,通常会想办法来隐蔽这个木马(除了木马自身的一些隐蔽特性外,他会尽量给你检查系统的过程设置障碍),通常入侵者会修改一些文件,比如管理员通常用ps -aux来查看 ...

  10. 【Codeforces 113B】Petr#

    Codeforces 113 B 题意:有一个母串\(S\)以及两个串\(S_{begin}\)和\(S_{end}\),问\(S\)中以\(S_{begin}\)为开头并且以\(S_{end}\)为 ...