当前位置:网站首页>Leetcode-1051: height checker
Leetcode-1051: height checker
2022-06-24 10:24:00 【Ugly and ugly】
Title Description
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 , Indicates the height of the current student position .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] Number of subscripts for .
Example
Example 1:
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 .
The problem solving process
Ideas and steps
(1) Simple questions . Consider space for time .
(2) New development data , And sort in ascending order ;
(3) Traverse two arrays , Count the number of different elements in the same position between the sorted array and the original array , It's the end result .
Code display
public class HeightChecker {
public int heightChecker(int[] heights) {
// Open up new data , Sort , Compare the new array data with the original array data
int[] tempArray = Arrays.copyOf(heights, heights.length);
Arrays.sort(tempArray);
int count = 0;
for (int i = 0; i < heights.length; i++) {
if (tempArray[i] != heights[i]) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] heights = {
5,1,2,3,4,6};
System.out.println(new HeightChecker().heightChecker(heights));
}
}
边栏推荐
猜你喜欢

Record the range of data that MySQL update will lock

机器学习——感知机及K近邻

Leetcode - 498: traversée diagonale

3. addition, deletion, modification and query of employees

简单的价格表样式代码

消息队列的作用

分布式系统你必须了解的点-CAP

5.菜品管理业务开发

Go language development environment setup +goland configuration under the latest Windows

uniapp 开发微信公众号,下拉框默认选中列表第一个
随机推荐
Leetcode - 498: traversée diagonale
牛客-TOP101-BM29
为什么 JSX 语法这么香?
SQL sever试题求最晚入职日期
分布式系统你必须了解的点-CAP
Groovy obtains Jenkins credentials through withcredentials
SQL Server AVG function rounding
numpy. logical_ and()
413 binary tree Foundation
Learning to organize using kindeditor rich text editor in PHP
Difference between package type and basic type
numpy.logical_and()
3. addition, deletion, modification and query of employees
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
numpy.linspace()
canvas无限扫描js特效代码
uniapp实现点击拨打电话功能
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance