第二题:[169. 多数元素](()
class Solution {
// 摩尔投票法
public int majorityElement(int[] nums) {
int res = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
if (count == 0) {
res = nums[i];
count++;
}
else {
if (res == nums[i]) count++;
else count–;
}
}
return res;
}
}
第三题:[217. 存在重复元素](()
class Solution {
public boolean containsDuplicate(int[] nums) {
//比较相邻的元素是否相同即可
Arrays.sort(nums);
for(int i=0;i<nums.length-1;i++){
if(nums[i]==nums[i+1]){
return true;
}
}
return false;
}}
第四题:[164. 最大间距](()
class Solution {
public int maximumGap(int[] nums) {
Arrays.sort(nums);
int max=0;
if(nums.length==1){return 0;}
for(int i=0;i<nums.length-1;i++){
int count=nums[i+1]-nums[i];//一组一组赋值一组一组的比
max=max>count?max:count;
}
return max;
}
}
第五题:[905. 按奇偶排序数组](()
class Solution {
public int[] sortArrayByParity(int[] nums) {
int len=nums.length;
int []arr=new int[len];
for(int i=0,j=0,k=0;i<len;i++){//奇数从后面加,偶数从前加
if(nums[i]%2==0){
arr[j]=nums[i];
j++;
}
else {
arr[len-1- 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 k]=nums[i];
k++;
}
}
return arr;
}
}
第六题:[539. 最小时间差](()
这题不会…看评论区的
class Solution {
public int findMinDifference(List timePoints) {
int n=timePoints.size();
if(n>1440){
return 0;
}