当前位置:网站首页>LeetCode-高度检查器
LeetCode-高度检查器
2022-06-21 12:06:00 【尖兵果子】
学校打算为全体学生拍一张年度纪念照。根据要求,学生需要按照 非递减 的高度顺序排成一行。
排序后的高度情况用整数数组 expected 表示,其中 expected[i] 是预计排在这一行中第 i 位的学生的高度(下标从 0 开始)。
给你一个整数数组 heights ,表示 当前学生站位 的高度情况。heights[i] 是这一行中第 i 位学生的高度(下标从 0 开始)。
返回满足 heights[i] != expected[i] 的 下标数量 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/height-checker
分析
题目说的复杂,用自己话说就是看这个队伍里有多少人没有按序排队,要求的就是这些人的数量
那么解题步骤如下
- 将待排序数组存入新数组
- 对待排序数组排序
- 将排序后的数组与新数组比较
- 返回不同数的数量
C代码
void sort(int heightsSize,int *heights)
{
for(int i=0;i<heightsSize;i++)
{
for(int j=0;j<heightsSize-i-1;j++)
{
if(heights[j]>heights[j+1])
{
int temp=heights[j];
heights[j]=heights[j+1];
heights[j+1]=temp;
}
}
}
}
int heightChecker(int* heights, int heightsSize){
int res[heightsSize];
int index=0,count=0;
while(index<heightsSize)
{
res[index]=heights[index];
index++;
}
sort(heightsSize,heights);
index=0;
while(index<heightsSize)
{
if(res[index]!=heights[index])
{
count++;
}
index++;
}
return count;
}
JS代码
/** * @param {number[]} heights * @return {number} */
var heightChecker = function(heights) {
let res=JSON.parse(JSON.stringify(heights));
let count=0;
heights.sort((num1,num2)=>num1-num2)
res.forEach((item,index)=>{
if(item!=heights[index])
{
count++;
}
})
return count;
};
let res=JSON.parse(JSON.stringify(heights));写这一句话是因为直接将数组赋给新数组,在JS中属于浅拷贝,也就是两个数据连体,一变都变.这一句话的作用是将其变成深拷贝的形式,也就是两个不相干的数组
有关JS深浅拷贝内容,本人也不是很了解,待学习后定来完善
边栏推荐
- Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!
- Nanjing University static program analyses -- Introduction learning notes
- External-Attention-tensorflow(更新中)
- Use huggingface to quickly load pre training models and datasets in the moment pool cloud
- Tensorflower使用指定的GPU和GPU显存
- tensorflow中使用的一些函数
- The k-th small__
- 巨头局终战:即时零售
- i. MX - rt1052 boot start
- 事务Transaction
猜你喜欢

Jenkins 通过Build periodically配置定时任务

Chapter VIII web project testing

STM32笔记之 SWJ(JTAG-DP和 SW-DP)

20n10-asemi medium and low voltage MOS tube 20n10

Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关

MySQL 5.6.49 enterprise version setting password complexity policy
![[yolov5s target detection] opencv loads onnx model for reasoning on GPU](/img/87/49a54dfaf325fe104287235fe533c5.png)
[yolov5s target detection] opencv loads onnx model for reasoning on GPU

Sdcc compiler + vscode to develop 8-bit microcontroller

Customization of power aging test system | overview of charging pile automatic test system nsat-8000

马斯克的“好朋友”,冲击2022港股最大IPO
随机推荐
Ansible 配置首次ssh免认证的操作说明
Jenkins 通过Build periodically配置定时任务
i.MX - RT1052 脉宽调制(PWM)
【云原生 | Devops篇】Jenkins安装与实战(二)
A recovery solution of system paralysis caused by upgrading glibc of VMware virtual machine
浅论OCA\UV-OCA LOCA\SLOCA 四种全贴合工艺
Introduction to CPU, MPU, MCU, SOC and MCM
Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway
Compilation de l'environnement vs Code + GCC développé par stm32
Summary of UART problems in stm32cubemx
i. MX - rt1052 clock and phase locked loop (PLL) analysis
Record the errors encountered in the pytorch training model once
2-zabbix automatically add hosts using autodiscover
Centos7 升级MySQL5.6.40至企业版5.6.49
这3个后生,比马化腾、张一鸣还狠
Tensorflower uses the specified GPU and GPU video memory
CPU、MPU、MCU、SoC、MCM介绍
I would like to ask you guys, the flick CDC will add a table level exclusive lock before extracting the full amount of Oracle data
Is the Huatai Securities account given by qiniu school true? Is it safe to open an account
What are the precautions for PCB design?