給你乙個陣列 nums,對於其中每個元素 nums[i],請你統計陣列中比它小的所有數字的數目。
換而言之,對於每個 nums[i] 你必須計算出有效的 j 的數量,其中 j 滿足 j != i 且 nums[j] < nums[i] 。
以陣列形式返回答案
第一次解題用的暴力解法
用時:24ms
public
static
int[
]smallernumbersthancurrent
(int
nums)}}
count[i]
= counts;
}return count;
}
看了解題思慮之後寫的排序解法
寫的複雜了點,但是用時時間少了很多
用時:5ms
public
int[
]smallernumbersthancurrent
(int
nums)
arrays.
sort
(numss)
;for
(int i =
0; i < count.length;i++
)for
(int i =
0;i}return counts;
}
有多少小於當前數字的數字
2 nums.length 500 0 nums i 100 方法一 暴力法 class solution return res 方法二 快速排序 我們也可以將陣列排序,並記錄每乙個數在原陣列中的位置。對於排序後的陣列中的每乙個數,我們找出其左側第乙個小於它的數,這樣就能夠知道陣列中小於該數的數量。...
有多少小於當前數字的數字
給你乙個陣列 nums,對於其中每個元素 nums i 請你統計陣列中比它小的所有數字的數目。換而言之,對於每個 nums i 你必須計算出有效的 j 的數量,其中 j 滿足 j i 且 nums j nums i 以陣列形式返回答案。示例 1 輸入 nums 8,1,2,2,3 輸出 4,0,1,...
1365 有多少小於當前數字的數字
題目 給你乙個陣列 nums,對於其中每個元素 nums i 請你統計陣列中比它小的所有數字的數目。換而言之,對於每個 nums i 你必須計算出有效的 j 的數量,其中 j 滿足 j i 且 nums j nums i 以陣列形式返回答案。示例 輸入 nums 8,1,2,2,3 輸出 4,0,1...