LeetCode刷題:有多少小於當前數字的數字(C#)

給你一個數組 nums,對於其中每個元素 nums[i],請你統計數組中比它小的所有數字的數目。

換而言之,對於每個 nums[i] 你必須計算出有效的 j 的數量,其中 j 滿足 j != i 且 nums[j] < nums[i] 。

以數組形式返回答案。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

public class Solution {
    public int[] SmallerNumbersThanCurrent(int[] nums) {
        int []array=new int [nums.Length];
        int flag=0;
        for(int i=0;i<nums.Length;i++){
            for(int j=0;j<nums.Length;j++){
                if(nums[i]>nums[j]){
                    flag++;
                }
            }
            array[i]=flag;
            flag=0;
        }
        return array;
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章