[学习报告]《LeetCode零基础指南》(第六讲) C排序API

第二题:[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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值