Conclusion
Height Checker
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
public int heightChecker(int[] heights) {
int[] heigth=new int[heights.length];
for (int i=0;i<heights.length;i++){
heigth[i]=heights[i];
}
int[] res=new int[heights.length];
for (int i=0;i<heights.length;i++) {
int min=100;
int mark=0;
for (int j= 0; j < heights.length; j++) {
if (min > heights[j]) {
min = heights[j];
mark=j;
}
}
heights[mark] = 101;
res[i]=min;
}
int count=0;
for (int j=0;j<heigth.length;j++){
if (res[j]!=heigth[j]) count++;
}
return count;
}
思路:将待排序数组进行复制,两个for循环进行排序,将排序后的数组与之进行比较
public int heightChecker(int[] heights) {
int[] heightToFreq = new int[101];
for (int height : heights) {
heightToFreq[height]++;
}
int result = 0;
int curHeight = 0;
for (int i = 0; i < heights.length; i++) {
while (heightToFreq[curHeight] == 0) {
curHeight++;
}
if (curHeight != heights[i]) {
result++;
}
heightToFreq[curHeight]--;
}
return result;
}
大佬思路:以原数组元素中值作为索引值,元素个数为值,建立一个数组,遍历该数组中值非0的数,将此索引值与源数组中元素进行比较
Third Maximum Number
Given integer array nums, return the third maximum number in this array. If the third maximum does not exist, return the maximum number.
public int thirdMax(int[] nums) {
Arrays.sort(nums);
int[] arr=new int[nums.length];
int j=0;
for(int i=nums.length-1;i>0;i--){
if(nums[i]!=nums[i-1]) arr[j++]=nums[i];
}
arr[j]=nums[0];
if(j<2) return arr[0];
return arr[2];
}
思路:先排序,后去重
public int thirdMax(int[] nums) {
Integer max1 = null;
Integer max2 = null;
Integer max3 = null;
for (Integer n : nums) {
if (n.equals(max1) || n.equals(max2) || n.equals(max3)) continue;
if (max1 == null || n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (max2 == null || n > max2) {
max3 = max2;
max2 = n;
} else if (max3 == null || n > max3) {
max3 = n;
}
}
return max3 == null ? max1 : max3;
}
大佬思路:对数组中每个元素进行遍历,存放在三个变量中
Find All Numbers Disappeared in an Array
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
public List<Integer> findDisappearedNumbers(int[] nums) {
ArrayList<Integer> res = new ArrayList<>();
int[] arr=new int[nums.length];
for(int n:nums){
arr[n-1]++;
}
for (int i=0;i<arr.length;i++){
if(arr[i]==0) res.add(i+1);
}
return res;
}
思路:使用计数器,Height Checker中的算法一样
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for (int i = 0; i < nums.length; i++) {
while (nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]) {
int tmp = nums[i];
nums[i] = nums[tmp - 1];
nums[tmp - 1] = tmp;
}
}
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
res.add(i + 1);
}
}
return res;
}
}
大佬思路:The idea is simple, if nums[i] != i + 1 and nums[i] != nums[nums[i] - 1], then we swap nums[i] with nums[nums[i] - 1], for example, nums[0] = 4 and nums[3] = 7, then we swap nums[0] with nums[3]. So In the end the array will be sorted and if nums[i] != i + 1, then i + 1 is missing.其实不太懂为什么要交换?
public List<Integer> findDisappearedNumbers(int[] nums) {
for (int i : nums) {
int index = Math.abs(i);
if (nums[index - 1] > 0) {
nums[index - 1] *= -1;
}
}
List<Integer> res = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
res.add(i + 1);
}
}
return res;
}
大佬思路:其实原理和我差不多,不过它将重复出现的元素标记成了负数
Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
class Solution {
public int[] sortedSquares(int[] A) {
int n = A.length;
int[] result = new int[n];
int i = 0, j = n - 1;
for (int p = n - 1; p >= 0; p--) {
if (Math.abs(A[i]) > Math.abs(A[j])) {
result[p] = A[i] * A[i];
i++;
} else {
result[p] = A[j] * A[j];
j--;
}
}
return result;
}
}
大佬思路:two-pointer