LeetCode排序算法 1~300Easy
88.合并两个有序数组(数字)
给你两个按非递减顺序排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2中的元素数目。请你合并nums2 到 nums1 中,使合并后的数组同样按非递减顺序排列。
注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
方法1(直接使用sort函数进行排序)
- 创建缓存数组
- 将两个非递减顺序的数组中的所有元素不安递增顺序进行存储,存储至缓存数组
- 对缓存数组中的所有元素进行排序
- 再将缓存数组中的所有元素存储至数组nums1
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int length = m + n;
int[] temp = new int[length]; //创建缓存数组temp
//将数组nums1和数组nums2中的各个元素不按照递增排序进行存储 //直接将数组nums2放到数组nums1后面
int k = 0; for(int i = 0; i != n; i++){
for(int i = 0; i < m; i++) temp[k++] = nums1[i]; nums1[m + i] = nums2[i];
for(int i = 0; i < n; i++) temp[k++] = nums2[i]; }
//对数组temp进行排序 //对数值nums1进行排序
Arrays.sort(temp); sort(nums1.begin(), nums1.end());
//将数组temp中的元素存储至数组nums1中
for(int i = 0; i < length; i++) nums1[i] = temp[i];
}
}
时间复杂度:O((m+n)log(m+n)) 1ms
空间复杂度:O(log(m+n))O(log(m+n)) 41.3MB
因此,此方法不是最优方法!
方法2(针对两个数组的递增特性,使用双指针算法)
- 创建缓存数组
- 设置两个指针
- 基于两个数组长度最短的数组,将两个数组中的元素按照递增顺序存储至缓存数组
- 判断还有剩余元素的数组,将数组中的所有剩余元素存储至存缓数组的最后
- 将缓存数组中的所有元素存储至数组nums1
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
//创建缓存数组temp
int length = m + n;
int[] temp = new int[length];
int k = 0;
int i = 0, j = 0; //设置两个指针
while(i < m && j < n){
if(nums1[i] <= nums2[j]) temp[k++] = nums1[i++];
else temp[k++] = nums2[j++];
}
while(i < m) temp[k++] = nums1[i++];
while(j < n) temp[k++] = nums2[j++];
for(k = 0; k < length; k++) nums1[k] = temp[k];
}
}
时间复杂度:O(m+n) 0ms
空间复杂度:O(m+n) 41.2MB
方法3(针对两个数组的递增特性,使用逆向双指针算法)
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int length = m + n;
int k = length - 1;
int i = m - 1, j = n - 1; //设置两个指针
while(i >= 0 && j >= 0){
if(nums1[i] <= nums2[j]) nums1[k--] = nums2[j--];
else nums1[k--] = nums1[i--];
}
while(i >= 0) nums1[k--] = nums1[i--];
while(j >= 0) nums1[k--] = nums2[j--];
}
}
169.多数元素(数字)
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
方法1(HashMap数组)
- 建立键值对的哈希表HashMap
- 遍历数组nums中的所有元素,并将元素以及元素出现的次数依次存储至HashMap中
- 输出元素次数>n / 2的元素
Java HashMap getOrDefault()方法
getOrDefault() 方法获取指定键对值,如果没有键,返回设置的默认值。
getOrDefault() 方法的语法为:
hashmap.getOrDefault(key, defaultValue)
Java get()方法
get()方法获取指定键的值
class Solution {
public int majorityElement(int[] nums) {
Map<Integer, Integer> count_map = new HashMap<>();
int res = 0, n = nums.length;
for(int num : nums){
count_map.put(num, count_map.getOrDefault(num, 0) + 1);
if(count_map.get(num) > n / 2){
res = num;
break;
}
}
return res;
}
}
15ms 46.2Mb
方法2(中位数一定是众数)
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}
}
方法3(摩尔投票法)
摩尔投票算法思想:适用于在数组中找出出现次数大于数组长度一半的元素
候选人:空
得票数:0
class Solution {
public int majorityElement(int[] nums) {
int answer = 0, count = 0;
int n = nums.length;
for(int i = 0; i < n; i++){
if(count == 0){
answer = nums[i];
count++;
}
else{
count = (answer == nums[i]) ? count + 1 : count - 1;
}
}
return answer;
}
}
217.存在重复元素(数字)
给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。
方法1(HashMap数组)
同169题的方法1
class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> count_map = new HashMap<>();
int n = nums.length;
for(int i = 0; i < n; i++){
count_map.put(nums[i], count_map.getOrDefault(nums[i], 0) + 1);
if(count_map.get(nums[i]) == 2 || count_map.get(nums[i]) > 2){
return true;
}
}
return false;
}
}
19ms 52.9MB
方法2(HashSet数组)
HashSet是一个不允许有重复元素的集合
Java add()方法
true并添加对象 | false |
---|---|
如果Set集合中不包含要添加的对象 | 如果Set集合中包含要添加的对象 |
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int x : nums) {
if (!set.add(x)) {
return true;
}
}
return false;
}
}
4ms 49.5MB
方法3(排序)
排序后,数组中相邻的两个元素相同,即该元素出现次数至少为2
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
for(int i = 1; i < n; i++){
if(nums[i] == nums[i - 1]) return true;
}
return false;
}
}
20ms 55MB
242.有效的字母异位词(字符串)
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
方法1(排序)
Java toCharArray() 方法:将字符串转换为字符数组
- 将字符串转换成字符数组,再对字符数组进行排序
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}
}
2ms 41.7MB
方法2(数组)
Java charAt() 方法
应用于String类
charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
获取指定字符在字符串中的索引: table[s.charAt(i) - 'a']
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] table = new int[26];
for(int i = 0; i < s.length(); i++){
table[s.charAt(i) - 'a']++;
}
for(int i = 0; i < t.length(); i++){
table[t.charAt(i) - 'a']--;
if(table[t.charAt(i) - 'a'] < 0) return false;
}
return true;
}
}
268.丢失的数字(数字)
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
方法1(HashSet数组)
class Solution {
public int missingNumber(int[] nums) {
Set<Integer> set = new HashSet<>();
int n = nums.length;
for(int num : nums){
set.add(num);
}
for(int i = 0; i <= n; i++){
if(set.add(i)) return i;
}
return 0;
}
}
5ms 41.6MB
方法2(异或方法)
异或方法应用于找缺失数/出现一次数
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 0; i <= n; i++) ans ^= i;
for (int i : nums) ans ^= i;
return ans;
}
}