一、前言
九日集训第五天。
二、题目
1)912. 排序数组
给你一个整数数组 n u m s nums nums,请你将该数组升序排列。
1.a)题目分析:
在Java中有类,可以直接调用。
1.b)代码:
class Solution {
public int[] sortArray(int[] nums) {
Arrays.sort(nums);
return nums;
}
}
2)169. 多数元素
给定一个大小为 n n n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n / 2 ⌋ ⌊ n/2 ⌋ ⌊n/2⌋ 的元素。你可以假设数组是非空的,并且给定的数组总是存在多数元素。
2.a)题目分析:
由于存在多数元素,对数组进行排序之后,多数元素一定在 n / 2 n/2 n/2这个位置。
2.b)代码:
class Solution {
public int majorityElement(int[] nums) {
int n =nums.length;
Arrays.sort(nums);
return nums[n/2];
}
}
3)217. 存在重复元素
给你一个整数数组 n u m s nums nums 。如果任一值在数组中出现 至少两次 ,返回 t r u e true true ;如果数组中每个元素互不相同,返回 f a l s e false false。
3.a)题目分析:
可以将数组排序,判断相邻两个数是否相等,若相等返回 t r u e true true,反之返回 f a l s e false false。
3.b)代码:
class Solution {
public boolean containsDuplicate(int[] nums) {
int n =nums.length;
Arrays.sort(nums);
for(int i =0;i<n-1;i++){
if(nums[i]==nums[i+1]){
return true;
}
}
return false;
}
}
4)164. 最大间距
给定一个无序的数组 n u m s nums nums,返回 数组在排序之后,相邻元素之间最大的差值 。如果数组元素个数小于 2 2 2,则返回 0 0 0 。您必须编写一个在「线性时间」内运行并使用「线性额外空间」的算法。
4.a)题目分析:
先对数组进行排序,然后比较相邻元素。
4.b)代码:
class Solution {
public int maximumGap(int[] nums) {
int n =nums.length;
if(n==1){
return 0;
}
Arrays.sort(nums);
int ans =0;
for(int i =0;i<n-1;i++){
if(nums[i+1]-nums[i]>ans){
ans=nums[i+1]-nums[i];
}
}
return ans;
}
}
5)905. 按奇偶排序数组
给定一个非负整数数组 A A A,返回一个数组,在该数组中, A A A 的所有偶数元素之后跟着所有奇数元素。你可以返回满足此条件的任何数组作为答案。
5.a)题目分析:
可以扫描两遍数组,第一遍返回偶数,第二遍返回奇数。
5.b)代码:
class Solution {
public int[] sortArrayByParity(int[] A) {
int[] ans = new int[A.length];
int t = 0;
for (int i = 0; i < A.length; ++i){
if (A[i] % 2 == 0)
ans[t++] = A[i];
}
for (int i = 0; i < A.length; ++i){
if (A[i] % 2 == 1)
ans[t++] = A[i];
}
return ans;
}
}
6)539. 最小时间差
给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
6.a)题目分析:
将时间转化为分钟形式,然后进行排序,遍历数组,得到最小时间差,但是要注意循环时间,即1440分钟。
6.b)代码:
class Solution {
public int findMinDifference(List<String> timePoints) {
int n = timePoints.size();
if (n > 1440) {
return 0;
}
Collections.sort(timePoints);
int ans = Integer.MAX_VALUE;
int t0Minutes = getMinutes(timePoints.get(0));
int preMinutes = t0Minutes;
for (int i = 1; i < n; ++i) {
int minutes = getMinutes(timePoints.get(i));
ans = Math.min(ans, minutes - preMinutes); // 相邻时间的时间差
preMinutes = minutes;
}
ans = Math.min(ans, t0Minutes + 1440 - preMinutes); // 首尾时间的时间差
return ans;
}
public int getMinutes(String t) {
return ((t.charAt(0) - '0') * 10 + (t.charAt(1) - '0')) * 60 + (t.charAt(3) - '0') * 10 + (t.charAt(4) - '0');
}
}
7)976. 三角形的最大周长
给定由一些正数(代表长度)组成的数组 n u m s nums nums ,返回 由其中三个长度组成的、面积不为零的三角形的最大周长 。如果不能形成任何面积不为零的三角形,返回 0 0 0。
7.a)题目分析:
将数组排序,从后面历遍数组,用三角形判定定理来验证,第一个能够满足三角形成立的三个数即为最大三角形的三个边。
7.b)代码:
class Solution {
public int largestPerimeter(int[] nums) {
int n =nums.length;
Arrays.sort(nums);
int ans=0;
for(int i=n-1;i>=2;--i){
if(nums[i]<nums[i-1]+nums[i-2]){
ans=nums[i]+nums[i-1]+nums[i-2];
return ans;
}
}
return 0;
}
}
8)881. 救生艇
给定数组 p e o p l e people people 。 p e o p l e [ i ] people[i] people[i]表示第 i i i 个人的体重 ,船的数量不限,每艘船可以承载的最大重量为 l i m i t limit limit。每艘船最多可同时载两人,但条件是这些人的重量之和最多为 l i m i t limit limit。返回 承载所有人所需的最小船数 。
8.a)题目分析:
排列数组,如果只有一个人,那只需要一只船即可,如果排列后的最轻的人和最重的人大于 l i m i t limit limit,则最重的人需要一个人坐船,如果小于,那他们两个都可以坐一个船。
8.b)代码:
class Solution {
public int numRescueBoats(int[] people, int limit) {
int i ;
int l=0,r=people.length-1;
int ans =0;
Arrays.sort(people);
while(l<=r){
if(l==r){
++ans;
break;
}
else if(people[l]+people[r]>limit){
++ans;
r--;
}
else{
++ans;
++l;
--r;
}
}
return ans;
}
}