当前位置:网站首页>Force deduction solution summary 1051 height checker
Force deduction solution summary 1051 height checker
2022-07-22 19:30:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
The school plans to take an annual commemorative photo for all the students . According to the requirements , Students need to follow The decreasing Line up in order of height .
The sorted height is an integer array expected Express , among expected[i] It's expected to be number... In this line i The height of a student ( Subscript from 0 Start ).
Give you an array of integers heights , Express Current student position The height of .heights[i] It's the... In this line i The height of a student ( Subscript from 0 Start ).
Return to satisfaction heights[i] != expected[i] Of Subscript quantity .
Example :
Input :heights = [1,1,4,2,1,3]
Output :3
explain :
Height :[1,1,4,2,1,3]
expect :[1,1,1,2,3,4]
Subscript 2 、4 、5 The height of the students at does not match .
Example 2:
Input :heights = [5,1,2,3,4]
Output :5
explain :
Height :[5,1,2,3,4]
expect :[1,2,3,4,5]
The corresponding student heights of all subscripts do not match .
Example 3:
Input :heights = [1,2,3,4,5]
Output :0
explain :
Height :[1,2,3,4,5]
expect :[1,2,3,4,5]
The corresponding student heights of all subscripts match .
Tips :
1 <= heights.length <= 100
1 <= heights[i] <= 100
source : Power button (LeetCode)
link :https://leetcode.cn/problems/height-checker
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Sort array after copying , Then compare the different quantity with the original array . * <p>
Code :
public int heightChecker(int[] heights) {
int[] copy = new int[heights.length];
System.arraycopy(heights, 0, copy, 0, heights.length);
Arrays.sort(copy);
int sum = 0;
for (int i = 0; i < heights.length; i++) {
int height1 = heights[i];
int height2 = copy[i];
if (height1 != height2) {
sum++;
}
}
return sum;
}
边栏推荐
猜你喜欢
Prototype object
Grafana panel - modify visual text and background colors
arguments
[FatFs] porting FatFs file system based on STM32 SD card
Rongyun x Xingzhi: exclusive Post-00 self social plot (including lottery)
Matlab plot子图的间距和边缘距离如何调整(已解决)
Programmer interview golden code interview question 01.05. primary editing
Help brand insight -- Analysis of consumers' emotional behavior
JS advanced - understanding of functions
Programmer interview golden code interview question 01.03. URL
随机推荐
Image quality evaluation
grafana面板-修改可视化文本和背景颜色
The difference between shell $* and [email protected]
Leetcode daily question 2022/1/31-2022/2/6
Leetcode daily question 2022/3/28-2022/4/3
4G工业路由器大气环境监测方案
Flutter 第一個程序Hello World!
LeetCode 每日一题 2021/12/27-2022/1/2
MFC dialog program only runs a simple example of a single instance
CentOS7安装Mysql5.7解压版&Navicat连接Mysql&防火墙设置——亲测有效
Leetcode daily question 2022/1/3-2022/1/9
Flutter 通过拖拽调整绘制图形
Industrial router oilfield wireless monitoring
Leetcode daily question 2022/3/14-2022/3/20
A practical example of the go pipeline pattern -- calculating the MD5 value of a series of files
融云办政事: “小网格”也能实现“大治理”
LeetCode 每日一题 2022/2/7-2022/2/13
"35 years old, I retired": This is the most reliable answer to the midlife crisis
Server disk IO performance tuning
Kindling the Darkness: A Practical Low-light Image Enhancer