[Swift]LeetCode493. 翻转对 | Reverse Pairs
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10348715.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an array nums
, we call (i, j)
an important reverse pair if i < j
and nums[i] > 2*nums[j]
.
You need to return the number of important reverse pairs in the given array.
Example1:
Input: [1,3,2,3,1] Output: 2
Example2:
Input: [2,4,3,5,1] Output: 3
Note:
- The length of the given array will not exceed
50,000
. - All the numbers in the input array are in the range of 32-bit integer.
给定一个数组 nums
,如果 i < j
且 nums[i] > 2*nums[j]
我们就将 (i, j)
称作一个重要翻转对。
你需要返回给定数组中的重要翻转对的数量。
示例 1:
输入: [1,3,2,3,1] 输出: 2
示例 2:
输入: [2,4,3,5,1] 输出: 3
注意:
- 给定数组的长度不会超过
50000
。 - 输入数组中的所有数字都在32位整数的表示范围内。
1 class Solution { 2 func reversePairs(_ nums: [Int]) -> Int { 3 var res:Int = 0 4 var copy:[Int] = nums.sorted(by:<) 5 var bit:[Int] = [Int](repeating:0,count:copy.count + 1) 6 for ele in nums 7 { 8 res += search(&bit, index(copy, 2 * ele + 1)) 9 insert(&bit, index(copy, ele)) 10 } 11 return res 12 } 13 14 func index(_ arr:[Int],_ val:Int) -> Int 15 { 16 var l:Int = 0 17 var r:Int = arr.count - 1 18 var m:Int = 0 19 while(l <= r) 20 { 21 m = l + ((r - l) >> 1) 22 if arr[m] >= val 23 { 24 r = m - 1 25 } 26 else 27 { 28 l = m + 1 29 } 30 } 31 return l + 1 32 } 33 34 func search(_ bit:inout [Int],_ i:Int) -> Int 35 { 36 var i = i 37 var sum:Int = 0 38 while(i < bit.count) 39 { 40 sum += bit[i] 41 i += i & -i 42 } 43 return sum 44 } 45 46 func insert(_ bit:inout [Int],_ i:Int) 47 { 48 var i = i 49 while(i > 0) 50 { 51 bit[i] += 1 52 i -= i & -i 53 } 54 } 55 }
转载于:https://www.cnblogs.com/strengthen/p/10348715.html
[Swift]LeetCode493. 翻转对 | Reverse Pairs相关推荐
- [LintCode] Reverse Pairs 翻转对
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return t ...
- Reverse Pairs
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return t ...
- leetcode练习 Reverse Pairs
几次练手之后,决定进军hard难度 最近的课程主要讲的是分治,就选择了相关的题目. Given an array nums, we call (i, j) an important reverse p ...
- c++内置函数实现字符串翻转(reverse,strrev,string 构造函数)
c++内置函数实现字符串翻转(reverse,strrev,string 构造函数) 在写程序的时候,我们经常需要将字符串进行翻转.c++中内置的函数有不少个可以实现该功能. 1.strrev函数.( ...
- [Swift]LeetCode832. 翻转图像 | Flipping an Image
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 翻转函数 reverse
数字翻转 给定一个整数,请将该数各个位上数字反转得到一个新数. 新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零. 输入格式 输入共 1 行,1 个整数 N. ...
- [Swift]LeetCode206. 反转链表 | Reverse Linked List
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Mysql翻转字符串reverse
颠倒字符串 SELECT reverse('chinese'); # esenihc SELECT reverse('pep'); # pep SELECT reverse(1234567); # 7 ...
- [Swift]LeetCode293. 翻转游戏 $ Flip Game
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
最新文章
- MySQL 5.7 vs 8.0,哪个性能更牛?
- java读取本地文件下载_java 读取本地的json文件
- python 类可以调用实例变量_python中的实例方法、静态方法、类方法、类变量和实例变量浅析...
- EL 表达式 JSTL 标签库
- 【软考-软件设计师】计算机系统基础知识
- ECCV 2020 Spotlight | 图像定位上的细粒化区域相似性自监督
- 如何通俗理解拉格朗日对偶问题(part2)
- 超声换能器的原理及设计_超声波发生器、变幅杆、焊头的匹配介绍
- dotNET Core实现分布式环境下的流水号唯一
- linux硬盘为啥分区,Linux下为什么要进行磁盘的分区
- 导入要素类到要素数据集当中(C++)(ArcObject)史上最快
- linux刷新profile文件,Linux下修改.bash_profile 文件改变PATH变量的值
- 联想笔记本大写提示软件_联想威6 2021款怎么样?值得买吗?下面几点或许可以帮到您...
- angularjs学习:通信
- ADS2020 Crack使用教程
- Oracle数据库之日期查询
- 计算机网络本地连接,电脑本地连接受限制或无连接怎么办
- 关闭win10任务视图功能
- java消息平台_Java微信公众平台之消息管理
- WiFi大师专业版SAAS小程序+强强联合2.0模式正式开启