Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

    • 一维模拟
      • 189. 轮转数组
      • 238. 除自身以外数组的乘积
      • 414. 第三大的数
      • 495. 提莫攻击
      • 628. 三个数的最大乘积
      • 274. H 指数
      • 275. H 指数 II
      • 453. 最小操作次数使数组元素相等
      • 665. 非递减数列
    • 前缀和
    • 差分数组
    • 排序
    • 滚动数组
    • 二维数组
    • 区间问题
    • 摩尔投票法
  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 数组
Nreal
2023-11-01
目录

一维模拟

# 189. 轮转数组 (opens new window)

方法一:数学

class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k %= n;
        int cnt = gcd(k,n);
        for(int i=0;i<cnt;i++){
            int cur = i;
            int anchor = nums[i];
            do{
                int nxt = (cur+k)%n;
                int tmp = nums[nxt];
                nums[nxt] = anchor;
                anchor = tmp;
                cur = nxt;
            }while(i!=cur);
        }
    }
    int gcd(int x,int y){
        return y>0?gcd(y,x%y):x;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

方法二:反转三次

nums = "----->-->"; k =3
result = "-->----->";

reverse "----->-->" we can get "<--<-----"
reverse "<--" we can get "--><-----"
reverse "<-----" we can get "-->----->"
1
2
3
4
5
6
class Solution {
    public void rotate(int[] nums, int k) {
        k=k%nums.length;
        reverse(nums,0,nums.length-1);
        reverse(nums,0,k-1);
        reverse(nums,k,nums.length-1);
    }
    void reverse(int[] nums,int a,int b){
        int i=a,j=b;
        while(i<j){
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            i++;
            j--;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 238. 除自身以外数组的乘积 (opens new window)

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int l=1,r=1;
        int[] ret = new int[nums.length];
        for(int i=0;i<nums.length;i++){
            ret[i] = l;
            l *= nums[i];
        }
        for(int j=nums.length-1;j>=0;j--){
            ret[j] *= r;
            r *= nums[j];
        }
        return ret;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 414. 第三大的数 (opens new window)

class Solution {
    public int thirdMax(int[] nums) {
        Arrays.sort(nums);
        int cnt = 1;
        int n = nums.length-1;
        for(int i=n;i>0;i--){
            if(nums[i]!=nums[i-1] && ++cnt==3){
                return nums[i-1];
            }
        }
        return nums[n];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 495. 提莫攻击 (opens new window)

class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int ans=0,end=0;
        for(int i=0;i<timeSeries.length;i++){
            ans += duration;
            if(timeSeries[i]<=end){
                ans -= end-timeSeries[i];//去重复时间
            }
            end = timeSeries[i]+duration;
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 628. 三个数的最大乘积 (opens new window)

class Solution {
    public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length-1;
        return Math.max(nums[n]*nums[n-1]*nums[n-2],
        nums[0]*nums[1]*nums[n]);
    }
}
1
2
3
4
5
6
7
8

# 274. H 指数 (opens new window)

方法一:排序

class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int h = 0;
        int i = citations.length-1;
        while(i>=0 && citations[i]>h){
            h++;
            i--;
        }
        return h;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

方法二:计数排序

class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int tot = 0;
        // 记录当前引用次数的论文有几篇
        int[] cnt = new int[n+1];
        for(int i=0;i<n;i++){
            if(citations[i]>=n)
                cnt[n]++;
            else
                cnt[citations[i]]++;
        }
        for(int i=n;i>=0;i--){
            tot += cnt[i];
            if(tot >= i)
                return i;
        }
        return 0;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 275. H 指数 II (opens new window)

class Solution {
    public int hIndex(int[] citations) {
        int h=0,i=citations.length-1;
        while(i>=0 && citations[i]>h){
            h++;
            i--;
        }
        return h;
    }
}
1
2
3
4
5
6
7
8
9
10

# 453. 最小操作次数使数组元素相等 (opens new window)

class Solution {
    public int minMoves(int[] nums) {
        int minNum = Arrays.stream(nums).min().getAsInt();
        int res = 0;
        for(int num:nums){
            res += num-minNum;
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10

# 665. 非递减数列 (opens new window)

class Solution {
    public boolean checkPossibility(int[] nums) {
        int cnt = 0;
        for(int i=1;i<nums.length;i++){
            if(nums[i]<nums[i-1]){
                if(i==1 || nums[i]>=nums[i-2]){
                    nums[i-1] = nums[i];
                }else{
                    nums[i] = nums[i-1];
                }
                if(++cnt>1){
                    return false;
                }
            }
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
分组循环
前缀和

← 分组循环 前缀和→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式