【算法练习】leetcode算法题合集之数组和哈希表篇

重建数组(高频)

LeetCode283.移动零

LeetCode283.移动零

双指针,记录已经处理好的序列的尾部

class Solution {
   
    public void moveZeroes(int[] nums) {
   
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
   
            if (nums[i] != 0) {
   
                swap(nums, i, k);
                k++;
            }
        }

    }

    private void swap(int[] nums, int i, int k) {
   
        int tmp = nums[i];
        nums[i] = nums[k];
        nums[k] = tmp;
    }
}

Leetcode26. 删除有序数组中的重复项

Leetcode26. 删除有序数组中的重复项

思路是一样的,序列中是不重复的有序序列。

class Solution {
   
    public int removeDuplicates(int[] nums) {
   
        int k = 1;
        for (int i = 1; i < nums.length; i++) {
   
            if (nums[i] != nums[k - 1]) {
   
                swap(nums, i, k);
                k++;
            }
        }
        return k;
    }

    private void swap(int[] nums, int i, int k) {
   
        int tmp = nums[i];
        nums[i] = nums[k];
        nums[k] = tmp;
    }
}

双指针

剑指offer21.调整数组顺序使奇数位于偶数前面

剑指offer21.调整数组顺序使奇数位于偶数前面

一个指针向后,一个指针从尾部向前。i之前的元素都是奇数,j之后的元素都是偶数。

class Solution {
   
    public int[] trainingPlan(int[] actions) {
   
        int length = actions.length;
        int i = 0, j = length - 1;

        while (i < j) {
   
            while (i < j && actions[i] % 2 != 0) {
   
                i++;
            }
            while (i < j && actions[j] % 2 == 0) {
   
                j--;
            }
            swap(actions, i, j);
        }
        return actions;

    }

    private void swap(int[] actions, int i, int j) {
   
        int tmp = actions[i];
        actions[i] = actions[j];
        actions[j] = tmp;
    }
}

LeetCode11. 盛最多水的容器

LeetCode11. 盛最多水的容器

使用双指针。更高的一方不动,调整矮的一方,使得整体的高度在上升。

class Solution {
   
    public int maxArea(int[] height) {
   
        int i = 0, j = height.length - 1;
        int res = 0;
        while (i < j) {
   
            if (height[i] < height[j]) {
   

                res = Math.max(res, height[i] * (j - i));
                i++;
            } else {
   

                res = Math.max(res, height[j] * (j - i));
                j--;
            }
        }
        return res;
    }
}

LeetCode1.两数之和

1. 两数之和

不能重复使用当下元素,有一个测试用例是这样的[3,2,4],6,由于索引为3的元素重复使用可以得到结果6.

class Solution {
   
    public int[] twoSum(int[] nums, int target) {
   
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
   
            if (map.containsKey(target - nums[i])) {
   
                Integer index = map.get(target - nums[i]);
                res[0] = i;
                res[1] = index;
                break;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}

LeetCode167. 两数之和 II - 输入有序数组

LeetCode167. 两数之和 II - 输入有序数组

class Solution {
   
    public int[] twoSum(int[] numbers, int target) {
   
        int i = 0, j = numbers.length - 1;
        int[] res = new int[2];
        while (i < j) {
   
            int sum = numbers[i] + numbers[j];
            if (sum < target) {
   
                i++;
            } else if (sum > target) {
   
                j--;
            } else {
   
                res[0] = i + 1;
                res[1] = j + 1;
                break;
            }
        }
        return res;
    }
}

在这里插入图片描述

相关推荐

  1. 算法练习leetcode算法动态规划

    2024-01-11 20:00:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-11 20:00:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-11 20:00:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-11 20:00:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-11 20:00:03       18 阅读

热门阅读

  1. K8S如何扩展副本集

    2024-01-11 20:00:03       25 阅读
  2. 如何在前端优化中处理大量的图像资源?

    2024-01-11 20:00:03       37 阅读
  3. KAFKA高级应用

    2024-01-11 20:00:03       31 阅读
  4. Golang 并发编程详解

    2024-01-11 20:00:03       31 阅读
  5. 苹果电脑安装office,如何在mac上安装office软件

    2024-01-11 20:00:03       44 阅读
  6. golang学习-map

    2024-01-11 20:00:03       35 阅读
  7. Vue3前端 响应式数据 知识点

    2024-01-11 20:00:03       30 阅读
  8. 力扣_数组24—搜索旋转排序数组II

    2024-01-11 20:00:03       30 阅读
  9. 【LeetCode2696】删除子串后的字符串最小长度

    2024-01-11 20:00:03       37 阅读
  10. 47.解释一下Spring AOP里面的几个名词

    2024-01-11 20:00:03       36 阅读
  11. 解密TF-IDF:打开文本分析的黑匣子

    2024-01-11 20:00:03       23 阅读
  12. Unity中打印信息的两种方式

    2024-01-11 20:00:03       33 阅读
  13. 橘子学K8S03之容器的理解

    2024-01-11 20:00:03       27 阅读