当前位置:网站首页>LeetCode1051(C#)

LeetCode1051(C#)

2022-07-07 19:13:00 Just be interesting

List of articles

subject

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 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 .


Code

  • Linq One sentence sweeping the world
public class Solution 
{
    
    public int HeightChecker(int[] heights) 
    {
    
        return heights.OrderBy(h => h).
        Zip(heights, (e,h) => (first : e, second : h)).
        Count(eh => eh.first != eh.second);
    }
}
原网站

版权声明
本文为[Just be interesting]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071515233852.html