算法题--(i & -i)的特性(Leetcode No.315)

博客探讨了LeetCode第315题的高效解决方案,该方案未使用分治策略,而是利用了位操作`i & -i`的特性。代码分析揭示了`i & -i`与负数在计算机底层表示的关系,并解释了如何利用这一特性在O(N logN)的时间复杂度内解决问题。
摘要由CSDN通过智能技术生成

最近在leetcode上做了一道题(No.315),查看一份耗时较少的代码时,发现其思路与其他的做法不同,没有采用“分治”的思想,但同样达到了较高的效率。

 

题目:315. Count of Smaller Numbers After Self

 

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Given nums = [5, 2, 6, 1]

 

To the right of 5 there are 2 smaller elements (2 and 1).

To the right of 2

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值