华为官方网站进入,开发网站公司收费,怎么建设一个网站并顺利打开浏览,赤峰微网站建设文章目录 刷题前唠嗑题目#xff1a;统计和小于目标的下标对数目题目描述代码与解题思路 结语 刷题前唠嗑 LeetCode#xff1f;启动#xff01;#xff01;#xff01;
题目#xff1a;统计和小于目标的下标对数目
题目链接#xff1a;2824. 统计和小于目标的下标对数… 文章目录 刷题前唠嗑题目统计和小于目标的下标对数目题目描述代码与解题思路 结语 刷题前唠嗑 LeetCode启动
题目统计和小于目标的下标对数目
题目链接2824. 统计和小于目标的下标对数目
题目描述 代码与解题思路
一眼顶真鉴定为暴力启动
func countPairs(nums []int, target int) (ans int) {for i : 0; i len(nums); i {for j : i1; j len(nums); j {if nums[i]nums[j] target {ans}} }return ans
}
不过暴力并不是最优解所以 . . . 双指针启动
func countPairs(nums []int, target int) (ans int) {sort.Ints(nums)for left, right : 0, len(nums)-1; left right; left {for right 0 nums[left]nums[right] target { right-- }if left right {ans right-left}}return ans
}也许还能用二分来做不过我就摸了。。。
结语
今天睡懒觉了每日一题晚了点儿