mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-10-19 22:12:33 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
#### [leetcode 493 翻转对](https://leetcode-cn.com/problems/reverse-pairs/)
|
||||
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
**题目描述**
|
||||
|
||||
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对。
|
||||
给定一个数组 nums ,如果 i < j 且 nums[i] > 2\*nums[j] 我们就将 (i, j) 称作一个重要翻转对。
|
||||
|
||||
你需要返回给定数组中的重要翻转对的数量。
|
||||
|
||||
@@ -30,11 +30,11 @@
|
||||
|
||||

|
||||
|
||||
此时我们发现 6 > 2 * 2,所以此时是符合情况的,因为小数组是单调递增的,所以 6 后面的元素都符合条件,所以我们 count += mid - temp1 + 1;则我们需要移动紫色指针,判断后面是否还存在符合条件的情况。
|
||||
此时我们发现 6 > 2 \* 2,所以此时是符合情况的,因为小数组是单调递增的,所以 6 后面的元素都符合条件,所以我们 count += mid - temp1 + 1;则我们需要移动紫色指针,判断后面是否还存在符合条件的情况。
|
||||
|
||||

|
||||
|
||||
我们此时发现 6 = 3 * 2,不符合情况,因为小数组都是完全有序的,所以我们可以移动红色指针,看下后面的数有没有符合条件的情况。这样我们就可以得到翻转对的数目啦。下面我们直接看动图加深下印象吧!
|
||||
我们此时发现 6 = 3 \* 2,不符合情况,因为小数组都是完全有序的,所以我们可以移动红色指针,看下后面的数有没有符合条件的情况。这样我们就可以得到翻转对的数目啦。下面我们直接看动图加深下印象吧!
|
||||
|
||||

|
||||
|
||||
@@ -45,7 +45,7 @@ Java Code:
|
||||
```java
|
||||
class Solution {
|
||||
private int count;
|
||||
|
||||
|
||||
public int reversePairs(int[] nums) {
|
||||
count = 0;
|
||||
merge(nums, 0, nums.length - 1);
|
||||
@@ -128,7 +128,7 @@ class Solution:
|
||||
temp2 += 1
|
||||
else:
|
||||
temp1 += 1
|
||||
|
||||
|
||||
# 记得归位,我们还要继续使用
|
||||
temp1 = left
|
||||
temp2 = mid + 1
|
||||
@@ -145,8 +145,7 @@ class Solution:
|
||||
# 照旧
|
||||
if temp1 <= mid:
|
||||
temparr[index: index + mid - temp1 + 1] = nums[temp1: temp1 + mid - temp1 + 1]
|
||||
if temp2 <= right:
|
||||
if temp2 <= right:
|
||||
temparr[index: index + right - temp2 + 1] = nums[temp2: temp2 + right - temp2 + 1]
|
||||
nums[left: left + right- left + 1] = temparr[0: right - left + 1]
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user