使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode
LeetCode learning records based on Java,Kotlin,Python...Github 地址
序号对应 LeetCode 中题目序号
14 编写一个函数来查找字符串数组中最长的公共前缀字符串
- Java 语言实现
public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0] == null ? "" : strs[0];
}
//找出数组中字符长度最短的字符串的长度(决定最多进行轮循比较的次数或者说是位数)
int minLength = Integer.MAX_VALUE;
for (String s : strs) {
if (minLength > s.length()) {
minLength = s.length();
}
}
boolean isCharEquals = true;
int i;//记录最后一位相等字符的位置
for (i = 0; i < minLength; i++) {
for (int j = 0; j < strs.length; j++) {
if (strs[0].charAt(i) != strs[j].charAt(i)) {
isCharEquals = false;
break;
}
}
if (!isCharEquals) {
break;
}
}
return strs[0].substring(0, i);
}
- Kotlin 语言实现
20 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效
利用栈存放左括号,出现右括号时判断栈顶的左括号是否跟当前右括号相匹配,匹配则移除栈中元素;最后检测栈中元素是否全部被移除;
- Java 语言实现
public boolean isValid(String s) {
Stack<Character> characterStack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
characterStack.push('(');
}
if (s.charAt(i) == '[') {
characterStack.push('[');
}
if (s.charAt(i) == '{') {
characterStack.push('{');
}
if (s.charAt(i) == ')') {
if (!characterStack.isEmpty() && characterStack.pop() == '(') {
continue;
} else {
return false;
}
}
if (s.charAt(i) == ']') {
if (!characterStack.isEmpty() && characterStack.pop() == '[') {
continue;
} else {
return false;
}
}
if (s.charAt(i) == '}') {
if (!characterStack.isEmpty() && characterStack.pop() == '{') {
continue;
} else {
return false;
}
}
}
return characterStack.isEmpty();
}
- Kotlin 语言实现
fun isValid(s: String): Boolean {
val charStack = ArrayList<Char>()
for (i in s.indices) {
if (s[i] == '(') {
charStack.add('(')
}
if (s[i] == '[') {
charStack.add('[')
}
if (s[i] == '{') {
charStack.add('{')
}
if (s[i] == ')') {
if (!charStack.isEmpty() && charStack[charStack.lastIndex] == '(') {
charStack.removeAt(charStack.lastIndex)
continue
} else {
return false
}
}
if (s[i] == ']') {
if (!charStack.isEmpty() && charStack[charStack.lastIndex] == '[') {
charStack.removeAt(charStack.lastIndex)
continue
} else {
return false
}
}
if (s[i] == '}') {
if (!charStack.isEmpty() && charStack[charStack.lastIndex] == '{') {
charStack.removeAt(charStack.lastIndex)
continue
} else {
return false
}
}
}
return charStack.isEmpty()
}
26 删除排序数组中的重复项
题目详细描述:给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
思路:从角标2开始向后遍历,如果当前元素和前一个元素不相同,就把角标为index的值赋值为当前元素,index自增
- Java 语言实现
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return 1;
}
int index = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[index++] = nums[i];
}
}
return index;
}
- Kotlin 语言实现
fun removeDuplicates(nums: IntArray): Int {
if (nums.isEmpty()) return 0
if (nums.size == 1) return 1
var index = 1
for (i in 1 until nums.size) {
if (nums[i] != nums[i - 1]) {
nums[index++] = nums[i]
}
}
return index
}
使用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刷题之路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" ...
- #leetcode刷题之路48-旋转图像
给定一个 n × n 的二维矩阵表示一个图像.将图像顺时针旋转 90 度.说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像.示例 1:给定 matrix ...
随机推荐
- arcgis javascript api 事件的监听及移除
On Style Events 方式 var mapExtentChange = map.on("extent-change", changeHandler); function ...
- C#的list和arry相互转化
,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- 双目SLAM(1) 总配置
kitti 数据集 图像+相机参数 sgbm gpu 算深度 cuda sifi 算匹配点 rabsac 随机筛选 1)CUDA配置(自己配置)8.0 参考网页 ...
- ethereum/EIPs-1102 Opt-in provider access metamask不再默认直接连入网页
eip title author discussions-to status type category created 1102 Opt-in provider access Paul Boucho ...
- mysql各数据类型的存储范围
文章转自 https://www.cnblogs.com/web21/p/6016120.html mysql整型bigint.int.mediumint.smallint 和 tinyint的语法介 ...
- C++STL二维vector指定位置排序
自己一直用vector 二维的存储变量 有时候需要进行排序 在此 为记录一下方法 废话少说直接上代码 #include <QCoreApplication> #include <io ...
- 学习CSS布局 - max-width
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- eclpse安装jetty插件
公司不用tomcat,使用的是jetty,那么学习一下如何在eclipse中安装jetty插件.
- ShellExecute使用详解
ShellExecute命令 ⑴ 函数原型: HINSTANCE ShellExecute(HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTST ...
- vba报表制作
Option Explicit Dim sql, tj As String, rnum As Double, r As Integer Private Sub CommandButton1_Clic ...