更新 Solution.java

This commit is contained in:
2023-09-12 16:24:57 +08:00
parent 64af01a85d
commit d38af5164a

View File

@@ -132,6 +132,12 @@ public class Solution {
// return "";
// }
/**
* 编写一个函数来查找字符串数组中的最长公共前缀。
* 如果不存在公共前缀,返回空字符串 ""。
* @param strs 数组
* @return 最长公共前缀
*/
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
@@ -148,9 +154,11 @@ public class Solution {
}
return strs[0];
}
//给你一个数组 nums 和一个值 val你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
//不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
//元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
/**
给你一个数组 nums 和一个值 val你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
*/
public static int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
@@ -161,6 +169,14 @@ public class Solution {
}
return i;
}
/**
* 给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。
* 元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。
*/
public static int removeDuplicates(int[] nums) {
int i = 0;
int[] newNums = new int[nums.length];
Map<Integer,Integer> duplicates = new HashMap<>();
}
}