力扣题分类

文章预览

输入输出

输入输出

一、 数组

1 二维数组

矩阵中的路径(是否包含)

输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
输出:true
思路:回溯,DFS

class Solution {
    public boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length;j++){
                if(dfs(board,words,i,j,0)){
                    return true;
                }
            }
        }
        return false;
    }    
    public boolean dfs(char[][] board, char[] words, int i, int j, int p){
        //是否越界或者字符不匹配
        if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != words[p]){
            return false;
        }
        if(p == words.length - 1){
            return true;
        }       
        char tmp = board[i][j];
        board[i][j] = '#'; //标记已访问过
        //上下左右找
        boolean res = dfs(board,words,i-1,j,p+1) || dfs(board,words,i+1,j,p+1)
                        || dfs(board,words,i,j-1,p+1) || dfs(board,words,i,j+1,p+1);
        board[i][j] = tmp;//去除标记
        return res;
    }
}

搜索二维矩阵 √

题目:
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。

思路:从右上角开始找

    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length==0 || matrix[0].length==0){
            return false;
        }
        //从矩阵右上角开始搜索
        int col = matrix[0].length - 1;//列
        int row = 0;//行

        while (col >= 0 && row <= matrix.length - 1) {
            if (target == matrix[row][col]) {
                //如果找到就直接返回
                return true;
            } else if (target < matrix[row][col]) {
                //如果查找的值大了,下一步往左找
                col--;
            } else if (target > matrix[row][col]) {
                //如果查找的值小了,下一步往下找
                row++;
            }
        }
        return false;
    }

顺时针打印矩阵

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        //特判
        if(matrix == null ||matrix.length == 0){
            return new int[0];
        }
        
        //初始化
        int left = 0, top = 0;
        int right = matrix[0].length-1;
        int bottom = matrix.length - 1;
        int[] res = new int[(right+1)*(bottom+1)];
        int k = 0;
        
        //循环打印
        while(top <= bottom && left <= right){
            for(int i = left; i <= right; i++){ //从左到右
                res[k++] = matrix[top][i];
            }
            top ++;
            for(int i = top; i <= bottom; i++){ //从上到下
                res[k++] = matrix[i][right];
            }
            right --;
            for(int i = right; i >= left && top <= bottom; i--){    //从右到左
                res[k++] = matrix[bottom][i];
            }
            bottom --;
            for(int i = bottom; i >= top && left <= right; i--){    //从下到上
                res[k++] = matrix[i][left];
            }
            left ++;
        }
        return res;
    }
}

2. 排序数组

合并排序的数组

class Solution {
public void merge(vector<int>& A, int m, vector<int>& B, int n) {
        int l=m+n-1;
        int a=m-1;
        int b=n-1;
        while(a>=0&&b>=0){
            if(A[a]>=B[b])
                A[l--]=A[a--];
            else
                A[l--]=B[b--];
        }
        while(b>=0)
            A[l--]=B[b--];
    }
}

有重复数字的升序数组的二分查找(二分)

给定一个 元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的第一个出现的target,如果目标值存在返回下标,否则返回 -1

import java.util.*;

public class Solution {
    public int search (int[] nums, int target) {
        int n = nums.length;
        if (n == 0) return -1;
        int l = 0, r = n - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        return nums[r] == target ? r : -1;
    }
}

在排序数组中查找元素的第一个和最后一个位置(重复)(二分)

题目:给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。如果数组中不存在目标值 target,返回 [-1, -1]。
思路二分法,找target第一次出现的位置,和最后一次出现的位置

public int[] searchRange(int[] nums, int target) {
    int first = searchFirst(nums, target);
    //判断有没有查找到
    if (first < nums.length && nums[first] == target) {
        int last = searchLast(nums, target);
        return new int[]{first, last};
    } else {
        //没有找到这个值,直接返回{-1, -1}
        return new int[]{-1, -1};
    }
}
//如果数组nums中有多个target,那么返回值就是第一个出现的target的下标
//如果数组nums中没有target,那么返回的就是第一个大于target的下标
public static int searchFirst(int[] nums, double target) {
    int low = 0, high = nums.length - 1;
    while (low <= high) {
        int m = low + (high - low) / 2;
        if (target > nums[m])
            low = m + 1;
        else
            high = m - 1;
    }
    return low;
}

public static int searchLast(int[] nums, double target) {
    int low = 0, high = nums.length - 1;
    while (low <= high) {
        int m = low + (high - low) / 2;
        if (target >= nums[m])
            low = m + 1;
        else
            high = m - 1;
    }
    return high;
}

时间复杂度: O(logn) ,其中 n 为数组的长度。二分查找的时间复杂度为 O(logn),一共会执行两次,因此总时间复杂度为 O(logn)。
空间复杂度:O(1)。只需要常数空间存放若干变量。

x的平方根(二分)

public int mySqrt(int x) {
        //逼近法 二分查找
        int l = 0, r = x;
        int ans = 0;
        while(l<=r){
            int mid = l +(r-l)/2;
            if((long)mid*mid <= x){
                ans = mid;
                l = mid+1;
            }
            else r = mid-1;
        }
        return ans;
    }

在排序数组中查找数字出现的次数(二分)

class Solution {
    public int search(int[] nums, int target) {
        // 搜索右边界 right  即最后一次出现的下标
        int i = 0, j = nums.length - 1;
        while(i <= j) {
            int m = (i + j) / 2;
            if(nums[m] <= target) i = m + 1;
            else j = m - 1;
        }
        int right = i;
        // 若数组中无 target ,则提前返回
        if(j >= 0 && nums[j] != target) return 0;
        // 搜索左边界 right   第一次出现的下标
        i = 0; j = nums.length - 1;
        while(i <= j) {
            int m = (i + j) / 2;
            if(nums[m] < target) i = m + 1;
            else j = m - 1;
        }
        int left = j;
        return right - left - 1;
    }
}

奇前偶后

public static int[] exchange2(int[] nums) {
		int left = 0,right = nums.length-1;
		while(left<right) {
			if(nums[left]%2==0&&nums[right]%2==1) {
				int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
			}else if (nums[left]%2==1) {
				left++;
			}else if (nums[right]%2==0) {
				right--;
			}
		}
		return nums;
	}

4. 全排列

下一个排列

题目:实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。必须 原地 修改,只允许使用额外常数空间。
思路:
1.先找出最大的索引 k 满足 nums[k] < nums[k+1],如果不存在,就翻转整个数组;
2.再找出另一个最大索引 l 满足 nums[l] > nums[k];
3.交换 nums[l] 和 nums[k];
4.最后翻转 nums[k+1:]

class Solution {
    public void nextPermutation(int[] nums) {
        if (nums == null || nums.length == 0) return;
        int firstIndex = -1;
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i] < nums[i + 1]) {
                firstIndex = i;
                break;
            }
        }
        if (firstIndex == -1) {
            reverse(nums, 0, nums.length - 1);
            return;
        }
        int secondIndex = -1;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] > nums[firstIndex]) {
                secondIndex = i;
                break;
            }
        }
        swap(nums, firstIndex, secondIndex);
        reverse(nums, firstIndex + 1, nums.length - 1);
        return;
    }
    private void reverse(int[] nums, int i, int j) {
        while (i < j) {
            swap(nums, i++, j--);
        }
    }

全排列

题目:给定一个 没有重复 数字的序列,返回其所有可能的全排列。
思路:回溯

class Solution {
    public List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        int[] visited = new int[nums.length];
        backtrack(res, nums, new ArrayList<Integer>(), visited);
        return res;

    }

    private void backtrack(List<List<Integer>> res, int[] nums, ArrayList<Integer> tmp, int[] visited) {
        if (tmp.size() == nums.length) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i] == 1) continue;
            visited[i] = 1;
            tmp.add(nums[i]);
            backtrack(res, nums, tmp, visited);
            visited[i] = 0;
            tmp.remove(tmp.size() - 1);
        }
    }
}

3. 旋转数组

旋转数组的最小数字

注意

  • 应当将Mid值与right值比较
  • >>的优先级低于±

思考两个问题
满足什么条件可以确定最小值在右边?
如果索引mid处的值比right处的值要大,说明mid在左半边的升序数组,最小值在mid右边,令left = mid + 1

满足什么条件可以确定最小值在左边?
如果索引mid处的值比right处的值要小,说明mid在右半边的升序数组,最小值可能就在mid处,也可能在mid左边,令right =mid 同样,如果索引mid处的值比left处的值要小,也可以说明mid在右半边的升序数组,同样令right = mid

这两个问题解决后,还要解决最后一个问题,因为数组中可以有重复元素,如果mid处的值和right处的值一样怎么办?这种情况下,mid和right所指的数都有可能是最小值,既然两个都可能是最小值,只要保留一个在查找区间内就可以了,把mid保留,令right = right - 1

class Solution {
    public int minArray(int[] numbers) {
        //注意mid应与right值比较
        // 二分目的是判断Mid在哪个排序数组中(左还是右)
        //当mid值大于left值时,不能判断mid在左升序序列还是右升序序列
        int left = 0;
        int right = numbers.length-1;
        while(left<=right){
            int mid = left + (right-left>>1);
            if(numbers[mid]>numbers[right]){
                //则Mid在左升序序列中
                //所以最小值在右边
                left = mid+1;
            }else if(numbers[mid]<numbers[right]){
                //则mid在右升序序列中
                //则最小值在mid或小于mid
                right = mid;
            }else{
                //相等
                right-=1;
            }
        }
        return numbers[left];
    }
}

搜索旋转排序数组(不重复)(二分查找)

题目:给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1 。
思路二分查找

public int search(int[] nums, int target) {
        //二分查找
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(nums[mid]==target) return mid;
            if(nums[0]<=nums[mid]) { //左边有序
                if (nums[left]<=target && target<nums[mid])
                    right = mid - 1;
                else left = mid+1;
            }
            else{//右边有序
                if(nums[mid]<target && target<=nums[nums.length-1])  left = mid+1;
                else right = mid-1;
            }
        }
        return -1;
    }

时间复杂度:O(logN),这里 N 是数组的长度,在循环中一次排除一半,因此时间复杂度是对数级别的。
空间复杂度:O(1),使用到的临时变量的个数是常数。

搜索旋转排序数组(重复)

题目:若存在target返回true , 否则返回false。
思路: 对于数组中有重复元素的情况,二分查找时可能会有 a[l]=a[mid]=a[r],此时无法判断区间 [l,mid]和区间[mid+1,r] 哪个是有序的。
例如nums=[3,1,2,3,3,3,3],target=2,首次二分时无法判断区间 [0,3]和区间 [4,6]哪个是有序的。
对于这种情况,我们只能将当前二分区间的左边界加一,右边界减一,然后在新区间上继续二分查找。

class Solution {
    public boolean search(int[] nums, int target) {
        int n = nums.length;
        if (n == 0) {
            return false;
        }
        if (n == 1) {
            return nums[0] == target;
        }
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return true;
            }
            if (nums[left] == nums[mid] && nums[mid] == nums[right]) {
                ++l;
                --r;
            } else if (nums[left] <= nums[mid]) {
                if (nums[left] <= target && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[n - 1]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return false;
    }
}

字母异位词分组

题目:给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出:
[
[“ate”,“eat”,“tea”],
[“nat”,“tan”],
[“bat”]
]

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

时间复杂度:O(nklogk),其中 n 是 strs 中的字符串的数量,k 是 strs 中的字符串的的最大长度。需要遍历 n 个字符串,对于每个字符串,需要O(klogk) 的时间进行排序以及 O(1) 的时间更新哈希表,因此总时间复杂度是 O(nklogk)。
空间复杂度:O(nk),其中 n 是 strs 中的字符串的数量,k 是 strs 中的字符串的的最大长度。需要用哈希表存储全部字符串。

合并区间

题目:以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

public class Solution {
    public int[][] merge(int[][] intervals) {
        int len = intervals.length;
        if (len < 2) {
            return intervals;
        }

        // 按照起点排序
        Arrays.sort(intervals, (v1, v2) -> v1[0] - v2[0]);

        List<int[]> res = new ArrayList<>();
        res.add(intervals[0]);
        for (int i = 1; i < len; i++) {
            int[] curInterval = intervals[i];

            // 每次新遍历到的列表与当前结果集中的最后一个区间的末尾端点进行比较
            int[] peek = res.get(res.size() - 1);

            if (curInterval[0] > peek[1]) {
                res.add(curInterval);
            } else {
                // 注意,这里应该取最大
                peek[1] = Math.max(curInterval[1], peek[1]);
            }
        }
        return res.toArray(new int[res.size()][]);
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
        int[][] res = solution.merge(intervals);
        for (int i = 0; i < res.length; i++) {
            System.out.println(Arrays.toString(res[i]));
        }
    }
}

时间复杂度:O(nlogn),其中 n 为区间的数量。除去排序的开销,我们只需要一次线性扫描,所以主要的时间开销是排序的O(nlogn)。
空间复杂度:O(logn),其中 n 为区间的数量。这里计算的是存储答案之外,使用的额外空间。O(logn) 即为排序所需要的空间复杂度。

编辑距离

题目:给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
思路:动态规划

class Solution {
    public int minDistance(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        int[][] dp = new int[n1 + 1][n2 + 1];
        //第一行,是 word1 为空变成 word2 最少步数,就是插入操作
		//第一列,是 word2 为空,需要的最少步数,就是删除操作
		
        // 第一行
        for (int j = 1; j <= n2; j++) dp[0][j] = dp[0][j - 1] + 1;
        // 第一列
        for (int i = 1; i <= n1; i++) dp[i][0] = dp[i - 1][0] + 1;

        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1];
                else dp[i][j] = Math.min(Math.min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]) + 1;
            }
        }
        return dp[n1][n2];  
    }
}

(一)、当word1[i]==word2[j]时,由于遍历到了i和j,说明word1的0i-1和word2的0j-1的匹配结果已经生成,由于当前两个字符相同,因此无需做任何操作,dp[i][j]=dp[i-1][j-1]
(二)、当word1[i]!=word2[j]时,可以进行的操作有3个:
① 替换操作:可能word1的0i-1位置与word2的0j-1位置的字符都相同,
只是当前位置的字符不匹配,进行替换操作后两者变得相同,
所以此时dp[i][j]=dp[i-1][j-1]+1(这个加1代表执行替换操作)
②删除操作:若此时word1的0i-1位置与word2的0j位置已经匹配了,
此时多出了word1的i位置字符,应把它删除掉,才能使此时word1的0~i(这个i是执行了删除操作后新的i)
和word2的0~j位置匹配,因此此时dp[i][j]=dp[i-1][j]+1(这个加1代表执行删除操作)
③插入操作:若此时word1的0i位置只是和word2的0j-1位置匹配,
此时只需要在原来的i位置后面插入一个和word2的j位置相同的字符使得
此时的word1的0i(这个i是执行了插入操作后新的i)和word2的0j匹配得上,
所以此时dp[i][j]=dp[i][j-1]+1(这个加1代表执行插入操作)
④由于题目所要求的是要最少的操作数:所以当word1[i] != word2[j] 时,
需要在这三个操作中选取一个最小的值赋格当前的dp[i][j]
(三)总结:状态方程为:
if(word1[i] == word2[j]):
dp[i][j] = dp[i-1][j-1]
else:
min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1
PS:大佬的代码中word1.charAt(i-1)==word2.charAt(j-1)的原因是:
初始化DP Table时dp[i][0]和dp[0][j]已经填写完成,所以接下来填表需要从1开始,
但是字符的比较需要从0开始,因此才这样子写

时间复杂度 :O(mn),其中 m 为 word1 的长度,n 为 word2 的长度。
空间复杂度 :O(mn),我们需要大小为O(mn) 的 D 数组来记录状态值。

子集

题目:给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
思路:递归(回溯算法)

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        backtrack(0, nums, res, new ArrayList<Integer>());
        return res;

    }

    private void backtrack(int i, int[] nums, List<List<Integer>> res, ArrayList<Integer> tmp) {
        res.add(new ArrayList<>(tmp));
        for (int j = i; j < nums.length; j++) {
            tmp.add(nums[j]);
            backtrack(j + 1, nums, res, tmp);
            tmp.remove(tmp.size() - 1);
        }
    }
}
  • 最长连续序列

乘积最大子数组

题目:给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

思路:动态规划
累乘的乘积等于 0,就要重新开始
累乘的乘积小于 0,要找到前面最大的负数,这样才能保住从 i 到 j 最大
累乘的乘积大于 0,要找到前面最小的正数,同理!

class Solution {
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int res = nums[0];
        int pre_max = nums[0];
        int pre_min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int cur_max = Math.max(Math.max(pre_max * nums[i], pre_min * nums[i]), nums[i]);
            int cur_min = Math.min(Math.min(pre_max * nums[i], pre_min * nums[i]), nums[i]);
            res = Math.max(res, cur_max);
            pre_max = cur_max;
            pre_min = cur_min;
        }
        return res;
    }
}

5. TopK问题

数组中找到第k小的元素(快速选择)

  • 思想:该算法基于快速排序算法,与快速排序算法类似,但每次只需要一次递归调用,如果支点的位置刚好为k,则返回该支点元素,如果小于k,则递归调用右子数组,如果大于k,则递归调用左子数组。
  • 时间复杂度:O(n)
// find the kth **smallest** element in an unsorted array
public int quickSelect(int[] nums, int k) {
    int i = 0;
    int j = nums.length - 1;
    
    while(i <= j) {
        int partitionIdx = partition(nums, i, j);
        
        if((k - 1) == partitionIdx) {
            return nums[partitionIdx];
        }
        else if((k - 1) < partitionIdx) {
            j = partitionIdx - 1;
        }
        else {
            i = partitionIdx + 1;
        }
    }
    
    return 0;
}
 
// same as qucik sort
public int partition(int[] nums, int start, int end) {
    if(start == end) {
        return start;
    }
    
    int pivot = nums[start];
    
    while(start < end) {
        // find first smaller element from right
        while(start < end && nums[end] >= pivot) {
            end--;
        }
        nums[start] = nums[end];
        
        // find first larger element from left
        while(start < end && nums[start] <= pivot) {
            start++;
        }
        nums[end] = nums[start];
    }
    
    nums[start] = pivot;
                         
    return start;
}

频率最高的K个数

1、小根堆(利用优先级队列,也就是java中定义好的堆)

class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        //首先遍历整个数组,使用哈希表记录每个元素在数组中出现的次数
        Map<Integer,Integer> map = new HashMap<>();
        for(int num: nums){
            map.put(num,map.getOrDefault(num,0)+1);
        }
        //利用小根堆
        //待插入的大于堆顶,则进堆;否则,不进
        //优先级队列默认是一个小根堆
        //此处需要改写comparator
        PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));
      
        //直接将元素插入,然后弹出堆顶。无需与堆顶比较。
        for(int key: map.keySet()){
            queue.offer(key);
            if(queue.size()>k){
                queue.poll();
            }
        }

        int[] res = new int[k];
        for(int i=0;i<k;i++){
            res[i]=queue.poll();
        }
        return res;
    }
}

2、统计频率——>排序——>选择前k个

class Solution {
    public List<Integer> topKFrequent(int[] nums, int k) {
        HashMap<Integer,Integer> Freq=new HashMap<Integer,Integer>();//构建频率表
        for(int i=0;i<nums.length;i++){
            if(Freq.containsKey(nums[i])){
                int tmp=Freq.get(nums[i]);
                Freq.remove(nums[i]);
                Freq.put(nums[i],tmp+1);//频率加一
            }
            else Freq.put(nums[i],1);
        }
        List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(Freq.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {//对map进行排序
            public int compare(Map.Entry<Integer, Integer> o1,
                    Map.Entry<Integer, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        int i=0;
        List<Integer> topk=new ArrayList<Integer>();
        for(Map.Entry<Integer,Integer> mapping:list){ //输出频率最高的k个数
               if(i==k) break;
               topk.add(mapping.getKey()); 
               i++;
        }
        return topk;
    }
}

最小的K个数(优先级队列/ 封装好的堆)

由于每次从堆顶弹出的数都是堆中最大的,最小的 k 个元素一定会留在堆里

public int[] getLeastNumbers(int[] arr, int k) {
    if (k == 0) {
        return new int[0];
    }
    // 使用一个最大堆(大顶堆)
    // Java 的 PriorityQueue 默认是小顶堆,添加 comparator 参数使其变成最大堆
    Queue<Integer> heap = new PriorityQueue<>(k, (i1, i2) -> Integer.compare(i2, i1));

    for (int e : arr) {
        // 当前数字小于堆顶元素才会入堆
        if (heap.isEmpty() || heap.size() < k || e < heap.peek()) {
            heap.offer(e);
        }
        if (heap.size() > k) {
            heap.poll(); // 删除堆顶最大元素
        }
    }

    // 将堆中的元素存入数组
    int[] res = new int[heap.size()];
    int j = 0;
    for (int e : heap) {
        res[j++] = e;
    }
    return res;
}

数组中的第K个最大元素(小根堆、大根堆!!!自建堆)

思路:

  1. 建个数为k的小顶堆
    这k个数是数组中最大的k个数,则堆顶就是第K个最大元素
    2.大根堆:建立一个大根堆,做 k - 1k−1 次删除操作后堆顶元素就是我们要找的答案
/**
小根堆
 */
class Solution2 {
    public int findKthLargest(int[] nums, int k) {
        //前K个元素原地建小顶堆
        buildHeap(nums, k);
        //遍历剩下元素,比堆顶小,跳过;比堆顶大,交换后重新堆化
        for (int i = k; i < nums.length; i++) {
            if (nums[i] < nums[0]) continue;
            swap(nums, i, 0);
            heapify(nums, k, 0);
        }
        //K个元素的小顶堆的堆顶即是第K大元素
        return nums[0];
    }
    /**
        * 建堆函数
        * 从倒数第一个非叶子节点开始堆化,倒数第一个非叶子节点下标为 K/2-1
        */
    public void buildHeap(int[] a, int k) {
        for (int i = k/2 - 1; i >= 0; i--) {
            heapify(a, k, i);
        }
    }
    /**
        * 堆化函数
        * 父节点下标i,左右子节点的下标分别为 2*i+1 和 2*i+2
        */
    public void heapify(int[] a, int k, int i) {
        //临时变量 minPos 用于存储最小值的下标,先假设父节点最小
        int minPos = i;
        while (true) {
            //和左子节点比较
            if (i*2+1 < k && a[i*2+1] < a[i]) minPos = i*2+1;
            //和右子节点比较
            if (i*2+2 < k && a[i*2+2] < a[minPos]) minPos = i*2+2;
            //如果minPos没有发生变化,说明父节点已经是最小了,直接跳出
            if (minPos == i) break;
            //否则交换
            swap(a, i, minPos);
            //父节点下标进行更新,继续堆化
            i = minPos;
        }
    }
    public void swap(int[] a, int n, int m) {
        int tmp = a[n];
        a[n] = a[m];
        a[m] = tmp;
    }
}
//大根堆
class Solution {
    public int findKthLargest(int[] nums, int k) {
        if(nums == null){
            return 0;
        }
        // 建立一个大顶堆
        for (int i = nums.length - 1; i >= 0; i--){
            heapFiy(i, nums, nums.length);
        }
        int heapSize = nums.length;
        swap(nums,0, --heapSize);
        int kCount = 1;
        if(k==1){
            //heapFiy(0,nums,heapSize);
            return nums[heapSize];
        }
        while(heapSize>0){
            kCount++;
            heapFiy(0,nums,heapSize);
            if(kCount>=k){
                return nums[0];
            }
            swap(nums,0, --heapSize);
        }
        return 0;
    }
    public void heapFiy(int index,int [] nums,int heapSize){
        int left = index*2 +1;
        while(left < heapSize){
            int largest = ((left+1) < heapSize && nums[left+1] > nums[left]) ? left+1:left;
            largest = nums[largest] > nums[index] ? largest : index;
            if (largest == index){
                break;
            }
            swap(nums,largest,index);
            index = largest;
            left = index*2 + 1;
        }
    }
    public void swap(int[] nums,int left,int right){
        int temp =  nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}
  • 完全平方数

题目:给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
给你一个整数 n ,返回和为 n 的完全平方数的 最少数量 。

6. 重复数

删除有序数组中的重复数(双指针)

  • 题目:给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度
    不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
  • 举例
    输入:nums = [1,1,2]
    输出:2, nums = [1,2]
    解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
  • 思路:一个指针 i 进行数组遍历,另外一个指针 j 指向有效数组的最后一个位置。
    只有当 i 所指向的值和 j 不一致(不重复),才将 i 的值添加到 j 的下一位置。
class Solution {
    public int removeDuplicates(int[] nums) {
        int n = nums.length;
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] != nums[j]) {
                nums[++j] = nums[i];
            }
        }
        return j + 1;
    }
}

寻找重复数

题目:给定一个包含 n + 1 个整数的数组 nums ,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。
假设 nums 只有 一个重复的整数 ,找出 这个重复的数 。

class Solution {
    //leecode 287 寻找一个重复的数
    //思想用一个额外的空间进行记录
    public int findDuplicate(int[] nums) {
        int[] dp = new int[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            if (dp[nums[i]] == 1) {
                return nums[i];
            }
            dp[nums[i]]++;
        }
        return -1;
    }
}
class Solution {
    public int findRepeatNumber(int[] nums) {
        //原地置换
        //交换索引为 i 和 nums[i] 的元素值,
        //一直交换,直到将此数字交换至对应索引位置
        int i=0;
        while(i<nums.length){
            if(nums[i]==i) { i++; continue;}
            if(nums[i]==nums[nums[i]]) return nums[i];
            //否则,交换
            int temp = nums[i];
            nums[i] = nums[temp];
            nums[temp] = temp;
        }
        return -1;
    }
}

最长递增子序列

题目:给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
思路:动态规划

class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length]; //记录到每个数时的最长子序列
        int maxLen = 1;
        for (int i = 0; i < nums.length; i++) {
            dp[i]=1;
            for (int j = 0; j < i; j++) {
                if(nums[i]>nums[j]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                    maxLen = maxLen>dp[i] ? maxLen:dp[i];
                }
            }
        }
        return maxLen;
    }
}
// Dynamic programming.
class Solution {
    public int lengthOfLIS(int[] nums) {
        if(nums.length == 0) return 0;
        int[] dp = new int[nums.length];
        int res = 0;
        Arrays.fill(dp, 1);  //初始化为1
        for(int i = 0; i < nums.length; i++) {
            for(int j = 0; j < i; j++) {
                if(nums[j] < nums[i]) dp[i] = Math.max(dp[i], dp[j] + 1);
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}
  • 零钱兑换

6. 和问题

两数之和

题目:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

思路:使用哈希表,对于每一个x,我们首先查询哈希表中是否存在 target - x,然后将 x 插入到哈希表中,即可保证不会让 x 和自己匹配。

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

时间复杂度:O(n)
空间复杂度:O(n)

三数之和

题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组
思路:首先对数组进行排序,排序后固定一个数 nums[i]nums[i],再使用左右指针指向 nums[i]nums[i]后面的两端,数字分别为 nums[L]nums[L] 和 nums[R]nums[R],计算三个数的和 sumsum 判断是否满足为 00,满足则添加进结果集

import java.util.ArrayList;
import java.util.Arrays;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    //排序+双指针
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int len = nums.length;
        if(nums==null || len<3){
            return ans;
        }
        Arrays.sort(nums);//排序
        //固定第i个数,L和R去选择剩下两个数
        for (int i = 0; i < len; i++) {
            if(nums[i]>0) break;
            if(i>0 && nums[i]==nums[i-1]) continue;
            int L = i+1;
            int R = len-1;
            while(L<R){
                int sum = nums[i]+nums[L]+nums[R];
                if(sum==0){
                    ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while(L<R && nums[L]==nums[l+1]) L++;//去重
                    while(L<R && nums[R]==nums[R-1]) R--;
                    L++;
                    R--;
                }
                else if(sum<0) L++;
                else if(sum>0) R--;
            }
        }
        return ans;
    }

}

时间复杂度 O(N^2) .其中固定指针k循环复杂度 O(N),双指针 i,j 复杂度 O(N)O(N)。
空间复杂度 O(1):指针使用常数大小的额外空间。

字符串相加

给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。

class Solution {
    public String addStrings(String num1, String num2) {
        StringBuilder res = new StringBuilder("");
        int i = num1.length()-1;
        int j = num2.length()-1;
        int carry = 0;
        while(i>=0 || j>=0){
            int n1 = i>=0 ? num1.charAt(i)-'0' : 0;
            int n2 = j>=0 ? num2.charAt(j)-'0': 0;
            int tmp = carry+n1+n2;
            carry = tmp/10;
            res.append(tmp%10);
            i--;j--;
        }
        if(carry==1) res.append(1);
        return res.reverse().toString();
    }
}

和为k的子数组

题目:给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。
输入:nums = [1,1,1], k = 2
输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。
思路:前缀和

    public int subarraySum(int[] nums, int k) {
        Map <Integer,Integer> map = new HashMap<>();
        //map保存映射 序列和 : 等于该序列和的数量

        int sum = 0;//计算求和的值
        int ans=0;
        map.put(0,1);//初始化,代表:序列和为0的值 初始只有1个。
        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
            //存在从某个子序列和 presum 到 当前的序列和 sum 差值刚好为k的值,
            //中间的值必然是连续的 
            if(map.containsKey(sum-k)){
                ans+=map.get(sum-k);
            }
            //序列和存在的话,设置为原始数量+1,不存在则初始设为0
            map.put(sum,map.getOrDefault(sum,0)+1);
        } 
        return ans;
    }

组合总和

题目:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。
输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
思路:回溯+剪枝

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        List<Integer> combine = new ArrayList<Integer>();
        dfs(candidates, target, ans, res, 0);
        return ans;
    }

    public void dfs(int[] candidates, int target, List<List<Integer>> ans, List<Integer> combine, int index) {
        if (index == candidates.length) {
            return;
        }
        if (target == 0) {
            ans.add(new ArrayList<Integer>(combine));
            return;
        }
        // 直接跳过
        dfs(candidates, target, ans, combine, index + 1);
        // 选择当前数
        if (target - candidates[index] >= 0) {
            combine.add(candidates[index]);
            dfs(candidates, target - candidates[index], ans, combine, index);
            combine.remove(combine.size() - 1);
        }
    }
}

n个骰子点数和

题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。

public double[] dicesProbability(int n) {
        //因为最后的结果只与前一个动态转移数组有关,所以这里只需要设置一个一维的动态转移数组
        //原本dp[i][j]表示的是前i个骰子的点数之和为j的概率,现在只需要最后的状态的数组,所以就只用一个一维数组dp[j]表示n个骰子下每个结果的概率。
        //初始是1个骰子情况下的点数之和情况,就只有6个结果,所以用dp的初始化的size是6个
        double[] dp = new double[6];
        //只有一个数组
        Arrays.fill(dp,1.0/6.0);
        //从第2个骰子开始,这里n表示n个骰子,先从第二个的情况算起,然后再逐步求3个、4个···n个的情况
        //i表示当总共i个骰子时的结果
        for(int i=2;i<=n;i++){
        //每次的点数之和范围会有点变化,点数之和的值最大是i*6,最小是i*1,i之前的结果值是不会出现的;
        //比如i=3个骰子时,最小就是3了,不可能是2和1,所以点数之和的值的个数是6*i-(i-1),化简:5*i+1
            //当有i个骰子时的点数之和的值数组先假定是temp
            double[] temp = new double[5*i+1];
            //从i-1个骰子的点数之和的值数组入手,计算i个骰子的点数之和数组的值
            //先拿i-1个骰子的点数之和数组的第j个值,它所影响的是i个骰子时的temp[j+k]的值
            for(int j=0;j<dp.length;j++){
            //比如只有1个骰子时,dp[1]是代表当骰子点数之和为2时的概率,它会对当有2个骰子时的点数之和为3、4、5、6、7、8产生影响,因为当有一个骰子的值为2时,另一个骰子的值可以为1~6,产生的点数之和相应的就是3~8;比如dp[2]代表点数之和为3,它会对有2个骰子时的点数之和为4、5、6、7、8、9产生影响;所以k在这里就是对应着第i个骰子出现时可能出现六种情况,这里可能画一个K神那样的动态规划逆推的图就好理解很多
                for(int k=0;k<6;k++){
                    //这里记得是加上dp数组值与1/6的乘积,1/6是第i个骰子投出某个值的概率
                    temp[j+k]+=dp[j]*(1.0/6.0);
                }
            }
            //i个骰子的点数之和全都算出来后,要将temp数组移交给dp数组,dp数组就会代表i个骰子时的可能出现的点数之和的概率;用于计算i+1个骰子时的点数之和的概率
            dp = temp;
        }
        return dp;
    }   

最大子序和(连续子数组的最大和)

题目: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。

class Solution {
    public int maxSubArray(int[] nums) {
        int tmpSum = 0, res = nums[0];
        for(int num : nums){
            tmpSum = Math.max(tmpSum + num, num);
            res = Math.max(res, tmpSum);
        }
        return res;
    }
}

时间复杂度:O(N)
空间复杂度:O(1)

数组中的逆序对(归并排序)

  • 归并排序
//归并排序:分治法。将序列分为两半,分别进行归并排序,再合并
    void merge(int[] arr, int start, int end) {
        if (start == end) return;
        int mid = (start + end) / 2;
        merge(arr, start, mid);
        merge(arr, mid + 1, end);

        int[] temp = new int[end - start + 1];
        int i = start, j = mid + 1, k = 0;
        while(i <= mid && j <= end)
            temp[k++] = arr[i] < arr[j] ? arr[i++] : arr[j++];
        while(i <= mid)
            temp[k++] = arr[i++];
        while(j <= end)
            temp[k++] = arr[j++];
        System.arraycopy(temp, 0, arr, start, end);
    }
  • 此题
    public int reversePairs(int[] nums) {
        return merge(nums, 0, nums.length - 1);
    }

    int merge(int[] arr, int start, int end) {
        if (start == end) return 0;
        int mid = (start + end) / 2;
        int count = merge(arr, start, mid) + merge(arr, mid + 1, end);

        int[] temp = new int[end - start + 1];
        int i = start, j = mid + 1, k = 0;
        while (i <= mid && j <= end) {
            count += arr[i] <= arr[j] ? j - (mid + 1) : 0;
            temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
        }
        while (i <= mid) {
            count += j - (mid + 1);
            temp[k++] = arr[i++];
        }
        while (j <= end)
            temp[k++] = arr[j++];
        System.arraycopy(temp, 0, arr, start, end - start + 1);
        return count;
    }

数组中第一个比它大的元素距离

题目:例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
思想:单调栈。数组从左向右遍历,依次将元素下标压栈
压栈时需要与栈顶元素相比较,如果当前元素比栈顶元素大,则弹出栈顶,相减,即当前元素的距离;否则压栈。

class Solution {
    public int[] dailyTemperatures(int[] T) {
        Stack<Integer> stack = new Stack<>();
        int n = T.length;
        int[] index = new int[n];
        for(int i = 0;i < n;i++){
           while(!stack.isEmpty() && T[stack.peek()] < T[i]){
               int res = stack.pop();
               index[res] = i - res;
           }
           stack.push(i);
        }
        return index;
    }
}

圆圈中最后剩下的数字(约瑟夫环)

题目: 总共是n个数字,每次删除第m个数字
思路
最后只剩下一个元素,假设这个最后存活的元素为 num, 这个元素最终的的下标一定是0 (因为最后只剩这一个元素),所以如果我们可以推出上一轮次中这个num的下标,然后根据上一轮num的下标推断出上上一轮num的下标,直到推断出元素个数为n的那一轮num的下标,那我们就可以根据这个下标获取到最终的元素了。推断过程如下:

  • 首先最后一轮中num的下标一定是0, 这个是已知的。
  • 那上一轮应该是有两个元素,此轮次中 num 的下标为 (0 + m)%n = (0+3)%2 = 1; 说明这一轮删除之前num的下标为1;
  • 再上一轮应该有3个元素,此轮次中 num 的下标为 (1+3)%3 = 1;说明这一轮某元素被删除之前num的下标为1;
  • 再上一轮应该有4个元素,此轮次中 num 的下标为 (1+3)%4 = 0;说明这一轮某元素被删除之前num的下标为0;
  • 再上一轮应该有5个元素,此轮次中 num 的下标为 (0+3)%5 = 3; 说明这一轮某元素被删除之前num的下标为3; 因为我们要删除的序列为0-n-1, 所以求得下标其实就是求得了最终的结果。
  • 比如当n为5的时候,num的初始下标为3, 所以num就是3,也就是说从0-n-1的序列中,
  • 经过n-1轮的淘汰,3这个元素最终存活下来了,也是最终的结果。 总结一下推导公式:
    (此轮过后的num下标 + m) % 上轮元素个数 = 上轮num的下标
class Solution {
    public int lastRemaining(int n, int m) {
        int x = 0; //元素初始的下标
        for (int i = 2; i <= n; i++) {//倒数第二轮是2个元素
            x = (x + m) % i;
        }
        return x;
    }
}

7. 数组中的数的次数

多数元素(摩尔投票)

题目:给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
思路:摩尔投票法

    public int majorityElement(int[] num) {
        int major = num[0];
        int count = 1;
        for (int i = 1; i < num.length; i++) {
            if (count == 0) {
                //前面都消完了,在重新赋值
                count++;
                major = num[i];
            } else if (major == num[i]) {
                //自己人,count就加1
                count++;
            } else {
                //不是自己人就同归于尽,消掉一个
                count--;
            }
        }
        return major;
    }

只出现一次的数字

问题:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

思路:数组中的全部元素的异或运算结果即为数组中只出现一次的数字。

class Solution {
    public int singleNumber(int[] nums) {
        int ans = 0;
        for (int num : nums) {
            ans ^= num;
        }
        return ans;
    }
}

0-n~1中缺失的数字

题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。
思路:二分法

class Solution {
    public int missingNumber(int[] nums) {
        int i = 0, j = nums.length - 1;
        while(i <= j) {
            int m = (i + j) / 2;
            if(nums[m] == m) i = m + 1;
            else j = m - 1;
        }
        return i;
    }
}

8. 场景

岛屿数量

题目:给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
思路:DFS

public int numIslands(char[][] grid) {
        //深度优先遍历
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j]=='1'){
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid,int i,int j){
        if(i<0 || j<0 || i>grid.length-1 || j>grid[0].length-1 || grid[i][j]=='0') return;
        grid[i][j] = '0';
        dfs(grid,i+1,j);
        dfs(grid,i,j+1);
        dfs(grid,i-1,j);
        dfs(grid,i,j-1);
    }

接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

  • 思路:比较当前左边最大高度和右边最大高度,若左边低,则当前柱子可接雨水=左边最大高度-当前柱子高度
public int trap(int[] height) {
        //双指针
        int maxRight = 0;
        int maxLeft = 0;
        int left = 0;
        int right = height.length-1;
        int ans = 0;

        while(left<right){
            //提前更新
            maxLeft = maxLeft>height[left]?maxLeft:height[left];
            maxRight = maxRight>height[right]?maxRight:height[right];

            if(maxLeft<=maxRight){
                ans+=maxLeft-height[left];
                left++;
            }else{
                ans+=maxRight-height[right];
                right--;
            }
        }
        return ans;
    }

买卖股票的最佳时机

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

思想:动态规划

public class Solution {
    public int maxProfit(int prices[]) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice) {
                minprice = prices[i];//日子一天天过去,但是我始终在今天之前(包括今天)的最低点买入
            } else if (prices[i] - minprice > maxprofit) {//今天是个高点,那我来看看卖的话可以赚多少钱,比之前卖会多还是少呢
                maxprofit = prices[i] - minprice;//如果多,那我就卖,不然我还是不卖
            }
        }
        return maxprofit;
    }
}

买卖股票的最佳时期(多次买卖)

题目:输入: prices = [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。

  • 思想:对于每一天我们都有三种选择:
    买入股票; 什么也不做; 卖出股票。
    可得状态转化方程:
    buy = max(buy, sell - p)
    sell = max(sell, buy + p)
  • 第一天的买入收益即为第一天股票价格的相反数(买入价格为 p 的股票,花费 p ,收益为 -p)
    buy = -prices[0]; 第一天卖出一定是在第一天买入的基础上进行的,同一天内股票价格相同,收益为0 sell = 0 ;
class Solution {
    public int maxProfit(int[] prices) {
        int buy = -prices[0], sell = 0;
        for(int p: prices){
            /*每一天都可以选择买或者卖*/
            //若不买,则当前收益延续上次买入后的状态值;若买,则由上一次卖出股票后的当前总收益减去当日买入股票的花费转换而来;
            buy = Math.max(buy, sell - p);
            //若不卖,则当前收益延续上次卖出后的状态值;若卖,则由上一次买入股票后的当前总收益加上当日卖出股票的收益转换而来。
            sell = Math.max(sell, buy + p);
        }
        return sell;
    }
}

颜色分类

题目: 给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
输入:nums = [2,0,2,1,1,0]
输出:[0,0,1,1,2,2]

思路双指针 :left 指针指向数组的开始;right 指针指向数组的结尾。 若 index 位置上的元素值为 0,则说明是红色,要放在最前面,即和 left 指针指向位置上的元素进行交换;
若 index 位置上的元素值为1,则说明是白色(本来就是要放在中间)不需要进行交换,直接将 index 指针后移; 若 index 位置上的元素值为2,则说明是蓝色,要放在最后面,即和 right 指针指向位置上的元素进行交换。

class Solution {
    public void sortColors(int[] nums) {
        int n = nums.length;
        int left = 0, right = n - 1;
        for (int i = 0; i <= right; ++i) {
            while (i <= right && nums[i] == 2) { //遇到2交换到最后
                int temp = nums[i];
                nums[i] = nums[right];
                nums[right] = temp;
                --right;
            }
            if (nums[i] == 0) {
                int temp = nums[i];
                nums[i] = nums[left];
                nums[left] = temp;
                ++left;
            }
        }
    }
}

时间复杂度:O(n),其中 n 是数组 nums 的长度。
空间复杂度:O(1)。

爬楼梯(青蛙跳台阶)

题目:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
思路:动态规划

class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
}

跳跃游戏

题目:给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。

public class Solution {
    public boolean canJump(int[] nums) {
        int n = nums.length;
        int rightmost = 0;
        for (int i = 0; i < n; ++i) {
            if (i <= rightmost) {
                rightmost = Math.max(rightmost, i + nums[i]);
                if (rightmost >= n - 1) {
                    return true;
                }
            }
        }
        return false;
    }
}

时间复杂度:O(n)O(n),其中 nn 为数组的大小。只需要访问 nums 数组一遍,共 nn 个位置。
空间复杂度:O(1)O(1),不需要额外的空间开销。

扑克牌中的顺子

输入: [1,2,3,4,5]
输出: True
输入: [0,0,1,2,5]
输出: True
思路:注意数组中最大值和最小值(除0之外)相差要小于数组长度

 public boolean isStraight(int[] nums) {
        int max = 0;
        int min = 14;
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                continue;
            }
            if (set.contains(nums[i])) {
                return false;
            }
            set.add(nums[i]);
            max = Math.max(max, nums[i]);
            min = Math.min(min, nums[i]);
        }
        return max - min < nums.length;
    }

机器人的运动范围

地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?

//统计到达的数量
int counts=0;
public int movingCount(int m, int n, int k) {
    //辅助数组 用来标记是否统计过
    int[][] visited = new int[m][n];
    //从 0,0位置开始统计
    helper(visited,0,0,m-1,n-1,k);
    return counts;
}

/**
*传入i,j两点 判断当前点是否符合规则 符合规则下继续对下右两个方向递归判断
*/
private void helper(int[][] visited,int i,int j,int m,int n,int k){
    if(i<=m&&j<=n&&visited[i][j]!=1&&(indexSum(i)+indexSum(j))<=k){
        counts++;
        visited[i][j]=1;
        helper(visited,i+1,j,m,n,k);
        helper(visited,i,j+1,m,n,k);
    }
}

/**
*根据传入的数 求出各位上的数字累加和
*/
private int indexSum(int index){
    int sum = index%10;
    int tmp = index/10;
    while(tmp>0){
        sum+=tmp%10;
        tmp/=10;
    }
    return sum;
}

剪绳子

题目:给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0]k[1]…*k[m-1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1

class Solution {
    public int cuttingRope(int n) {
        //动态规划
        //dp[i] 表示将长度为i的绳子拆分,得到的最大乘积
        //边界条件: dp[0]=dp[1]=0
        //状态转移方程:
        // 当 i≥2 时,假设对正整数 i 拆分出的第一个正整数是 j(1≤j<i),则有以下两种方案:
        //     将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j×(i−j);
        //     将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j×dp[i−j]。

        //由下而上求解,从i=2开始,计算i长度的最大乘积,直到最后i=n
        int[] dp = new int[n+1]; //初始值默认为0
        for(int i=2;i<=n;i++){
            for(int j = 1;j<i;j++){
                dp[i] = Math.max(dp[i],Math.max(j*(i-j),j*dp[i-j]));
            }
        }
        return dp[n];
    }
}

二进制中1的个数

思路:位运算。每次将n右移一位就可以更新得到新的最后一位,继续和1做与运算
如果n & 1 = 0, 则n的最后一位是0
如果n & 1 = 1, 则n的最后一位是1

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        // 返回结果
        int res = 0;
        while(n != 0){
            res = res + (n & 1);
            // 无符号右移1位
            n = n >>> 1;
        }
        return res;
    }
}

数值的整数次方

递归是比较好理解的

如果n == 0,返回1 如果n < 0,最终结果为 1/x^{-n}
如果n为奇数,最终结果为 x * x ^ {n - 1}
如果n为偶数,最终结果为 x ^ {2*(n/2)}
Java中因为n的最小值可以取到Integer.MIN_VALUE,如果直接取它的相反数的话还是它自己,会导致堆栈溢出,因此提一个x出来

class Solution {
    public double myPow(double x, int n) {
        if(n == 0){
            return 1;
        }else if(n < 0){
            return 1 / (x * myPow(x, - n - 1));
        }else if(n % 2 == 1){
            return x * myPow(x, n - 1);
        }else{
            return myPow(x * x, n / 2);
        }     
    }
}

二、链表

两数相加

题目:给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

思路:同时遍历两个链表,逐位计算它们的和,并与当前位置的进位值相加。
如果两个链表的长度不同,则可以认为长度短的链表的后面有若干个 00 。
此外,如果链表遍历结束后,还有进位,还需要在答案链表的后面附加一个节点。

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode tmp = new ListNode(-1);
        ListNode head = tmp;
        int jinWei = 0;
        //先计算,在反转链表
        while(l1!=null && l2!=null){
            int num = (l1.val+l2.val+jinWei)%10;
            jinWei = (l1.val+l2.val+jinWei)/10;
            tmp.next = new ListNode(num);
            tmp = tmp.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        while(l1!=null){
            int num = (l1.val+jinWei)%10;
            jinWei = (l1.val+jinWei)/10;
            tmp.next = new ListNode(num);
            tmp = tmp.next;
            l1 = l1.next;
        }
        while(l2!=null){
            int num = (l2.val+jinWei)%10;
            jinWei = (l2.val+jinWei)/10;
            tmp.next = new ListNode(num);
            tmp = tmp.next;
            l2 = l2.next;
        }
        if(jinWei>0){
            tmp.next = new ListNode(jinWei);
        }
        return head.next;
    }
public class AddTwoNum {
    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int x) {val=x;}
    }
    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        {
            ListNode head = null, tail = null;
            int jinWei = 0; //进位
            while (l1!=null || l2!=null ) {
                int n1 = l1 != null ? l1.val : 0;
                int n2 = l2 != null ? l2.val : 0;
                int sum = n1 + n2 + jinWei;
                if (head == null) {
                    head = tail = new ListNode(sum % 10);
                } else {
                    tail.next = new ListNode(sum % 10);
                    tail = tail.next;
                }
                jinWei = sum / 10;
                if (l1 != null) {
                    l1 = l1.next;
                }
                if (l2 != null) {
                    l2 = l2.next;
                }
            }
            if (jinWei > 0) {
                tail.next = new ListNode(jinWei);
            }
            return head;
        }
    }

    public static void main(String[] args) {
        ListNode a1 = new ListNode(2);
        ListNode a2 = new ListNode(4);
        ListNode a3 = new ListNode(3);
        a1.next = a2;
        a2.next = a3;

        ListNode b1 = new ListNode(5);
        ListNode b2 = new ListNode(6);
        ListNode b3 = new ListNode(4);
        b1.next = b2;
        b2.next = b3;
        ListNode resultNode = addTwoNumbers(a1, b1);
        System.out.println(resultNode.val);  //返回的是头结点
    }
}

时间复杂度:O(max(m,n)),其中 m 和 n 分别为两个链表的长度。我们要遍历两个链表的全部位置,而处理每个位置只需要O(1) 的时间。
空间复杂度:O(1)。注意返回值不计入空间复杂度。

排序链表

题目:给你链表的头结点 head ,请将其按 升序 排列并返回排序后的链表 。
你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null || head.next==null){
            return head;
        }
        //利用快慢指针找到中间位置
        ListNode fast = head.next;
        ListNode slow = head;
        while(fast.next!=null && fast.next.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode tmp = slow.next;
        slow.next = null;  //切断操作
        ListNode left = sortList(head);
        ListNode right = sortList(tmp);

		//一分为二的排序
        ListNode res = new ListNode(); 
        ListNode ans = res;
        while(left!=null && right!=null){
            if(left.val<right.val){
                res.next = left;
                left = left.next;
            }
            else{
                res.next = right;
                right = right.next;
            }
            res = res.next;
        }
        res.next = left!=null ? left:right;
        return ans.next;
    }
}

从尾打印链表

//栈
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        LinkedList<Integer> stack = new LinkedList<>();
        ListNode p = head;

        while (p != null) {
            stack.push(p.val);
            p = p.next;
        }
        int[] res = new int[stack.size()];
        int i = 0;
        while (!stack.isEmpty()) {
            res[i] = stack.pop();
            i++;
        }
        return res;
    }
}

输出链表中倒数第K个几点

题目:给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
思路:快慢指针

class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head, slow = head;
        for(int i = 0; i < k; i++)
            fast = fast.next;
        while(former != null) {
            fast = fast.next;
            low = low.next;
        }
        return low;
    }
}

删除链表的倒数第 N 个结点

思路:快慢指针,让前面的指针先移动n步,之后前后指针共同移动直到前面的指针到尾部为止

public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
 }
 public class Solution{
 	public ListNode removeNthFromEnd(ListNode head, int n) {    
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode fast = pre;
        ListNode slow = pre;
        while(n != 0) {
            fast = fast.next;
            n--;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return pre.next;
    }
}

时间复杂度:O(n)

合并两个有序链表

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode prehead = new ListNode(-1);
        ListNode prev = prehead;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                prev.next = l1;
                l1 = l1.next;
            } else {
                prev.next = l2;
                l2 = l2.next;
            }
            prev = prev.next;
        }
        // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
        prev.next = l1 == null ? l2 : l1;
        return prehead.next;
    }

时间复杂度:O(n + m),其中 n 和 m 分别为两个链表的长度。因为每次循环迭代中,l1 和 l2 只有一个元素会被放进合并链表中, 因此 while 循环的次数不会超过两个链表的长度之和。所有其他操作的时间复杂度都是常数级别的,因此总的时间复杂度为 O(n+m)。
空间复杂度:O(1)。我们只需要常数的空间存放若干变量。

环形链表

题目:给定一个链表,判断链表中是否有环。
思路:快慢指针

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) return true;
        }
        return false;
    }
}

环形链表(返回入口节点)

import java.util.HashSet;

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode cur = head;
        Set<ListNode> set = new HashSet<>();
        while(cur!=null){
            if(set.contains(cur)){
                return cur;
            }else{
                set.add(cur);
            }
            cur = cur.next;
        }
        return null;
    }
}
  • LRU缓存

排序链表

题目:给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表

思路:归并排序 o(nlogn)
利用归并的思想,递归地将当前链表分为两段,然后 merge。
分两段的方法是使用快慢指针,fast 一次走两步,slow 一次走一步。因为 fast 指针走的遍历的节点数是 slow 指针遍节点数的两倍,所以当 fast 指针遍历到链表末尾时,此时 slow 指针所在位置就是链表的中间位置,这样就将当前链表分成了两段。
merge 时,把两段头部节点值比较,定义一个 p 指针指向较小的节点,且记录第一个节点,然后两段链表从头一步一步向后走,p 也一直向后走,总是指向较小节点,直至其中一个头为 NULL,继续处理剩下的元素,最后返回记录的头即可。

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {        
        if (head == null) return null;
        return mergeSort(head);
    }
    private ListNode mergeSort(ListNode head) {
        if (head == null || head.next == null) return head;
        //利用快慢指针来找到链表的中点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }     
        ListNode right = mergeSort(slow.next);
        slow.next = null;
        ListNode left = mergeSort(head);        
        return merge(left, right);
    }
    private ListNode merge(ListNode left, ListNode right) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = dummyHead;
        while (left != null && right != null) {
            if (left.val <= right.val) {
                p.next = left;
                left = left.next;
            } else {
                p.next = right;
                right = right.next;
            }
            p = p.next;
        }
        if (left != null) p.next = left;
        if (right != null) p.next = right;
        return dummyHead.next;
    }
}

相交链表(两个链表的第一个公共节点)

题目:找到两个单链表相交的起始节点。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode A = headA, B = headB;
        while (A != B) {        	
            A = A != null ? A.next : headB;
            B = B != null ? B.next : headA;
        }
        return A;
    }
}

反转链表

class Solution {
    public ListNode reverseList(ListNode head) {
        // 待输出链表的头结点
        ListNode pre = new ListNode();
        // 遍历所给链表用的指针 curNode
        ListNode curNode = head;
        while(curNode != null) {
            // 保存当前遍历的结点同时当前指针后移一位
            ListNode temp = curNode;
            curNode = curNode.next;
            // 将当前遍历到的结点放入新链表头部之后
            // insert temp node into result after NEW LIST'S HEAD
            temp.next = pre.next;
            pre.next = temp;
        }
        return pre.next;
    }
}

反转链表(部分)

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。
请你反转从位置 left 到 位置 right 的链表节点,返回 反转后的链表 。
示例 1:
输入:head =[1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        ListNode pre = dummyNode;
        // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
        // 建议写在 for 循环里,语义清晰
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }

        // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
        ListNode rightNode = pre;
        for (int i = 0; i < right - left + 1; i++) {
            rightNode = rightNode.next;
        }

        // 第 3 步:切断出一个子链表(截取链表)
        ListNode leftNode = pre.next;
        ListNode curr = rightNode.next;

        // 注意:切断链接
        pre.next = null;
        rightNode.next = null;

        // 第 4 步:同第 206 题,反转链表的子区间
        reverseLinkedList(leftNode);

        // 第 5 步:接回到原来的链表中
        pre.next = rightNode;
        leftNode.next = curr;
        return dummyNode.next;
    }

    private void reverseLinkedList(ListNode head) {
        // 也可以使用递归反转一个链表
        ListNode pre = null;
        ListNode cur = head;

        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
    }
}

回文链表

题目:请判断一个链表是否为回文链表。
思路:利用栈

public boolean isPalindrome(ListNode head) {
        //定义一个栈
        Stack<ListNode> stack = new Stack<>();
        ListNode node = head;
        //挨个遍历链表的节点,并依次压入栈中
        while (node != null) {
            stack.push(node);
            node = node.next;
        }
        while (!stack.isEmpty()) {
            //head不断从头开始遍历,stack不断从尾开始遍历,直到全部遍历结束,如果过程中有不相等的情况,则不是回文链表
            if (head.val == stack.pop().val) {
                head = head.next;
            } else {
                return false;
            }
        }
        return true;
    }

K个一组翻转链表

步骤分解:

  1. 链表分区为已翻转部分+待翻转部分+未翻转部分
  2. 每次翻转前,要确定翻转链表的范围,这个必须通过 k 此循环来确定
  3. 需记录翻转链表前驱和后继,方便翻转完成后把已翻转部分和未翻转部分连接起来
  4. 初始需要两个变量 pre 和 end,pre代表待翻转链表的前驱,end 代表待翻转链表的末尾
  5. 经过k此循环,end 到达末尾,记录待翻转链表的后继 next = end.next
  6. 翻转链表,然后将三部分链表连接起来,然后重置 pre 和 end 指针,然后进入下一次循环
  7. 特殊情况,当翻转部分长度不足 k 时,在定位 end 完成后,end==null,已经到达末尾,说明题目已完成,直接返回即可
  8. 时间复杂度为 O(n*K) 最好的情况为 O(n)最差的情况未 O(n^2)
  9. 空间复杂度为 O(1) 除了几个必须的节点指针外,我们并没有占用其他空间
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode hair = new ListNode(0);
        hair.next = head;
        ListNode pre = hair;

        while(head!=null){
            ListNode tail = pre;
            //查看剩余部分是否大于k
            for(int i =0;i<k;i++){
                tail = tail.next;
                if(tail==null){
                    return hair.next;
                }
            }
            ListNode nex = tail.next;
            ListNode[] reverse = myReverse(head,tail);
            head = reverse[0];
            tail = reverse[1];
            //把子链表重新接回原链表
            pre.next = head;
            tail.next = nex;
            pre = tail;
            head = tail.next;
        }
        return hair.next;
    }

    public ListNode[] myReverse(ListNode head,ListNode tail){
        ListNode prev = tail.next;
        ListNode p = head;
        while(prev!=tail){
            ListNode nex = p.next;
            p.next = prev;
            prev = p;
            p = nex;
        }
        return new ListNode[]{tail,head};
    }
}

LRU缓存

class LRUCache {

    //利用双向链表和hashmap
    //增删:使用链表,增加和删除数据方便
    //删除:需要获取它前一个节点,可用双向链表
    //查:判断数在不在链表中,可用哈希表记录数据所处位置

    //在双向链表的实现中,使用一个伪头部和伪尾部标记界限,这样在添加节点和删除节点的时候就不需要检查相邻的节点是否存在

    private Map<Integer,Node> map = new HashMap<>();
    private Node head = new Node(-1,-1);
    private Node tail = new Node(-1,-1);
    private int capacity;

    //定义双向链表节点
    static class Node{
        int key,val;
        Node prev,next;
        public Node(int key,int val){
            this.key = key;
            this.val = val;
        }
    }

    public LRUCache(int capacity) {
        this.capacity = capacity;
        head.next = tail;
        tail.prev = head;
    }
    //链表头部  是经常使用的节点,尾部是不经常使用的节点
    public int get(int key) {
        //判断key是否存在于双向链表中,利用HashMap
        //如果存在,将此节点删除,并移至链表头部
        if(map.containsKey(key)){
            Node node = map.get(key);
            node.prev.next = node.next;
            node.next.prev = node.prev;
            moveToHead(node);
            return node.val;
        }
        return -1;
    }
    
    public void put(int key, int value) {
        //判断Key是否存在
        if (get(key) != -1){  //若存在
            map.get(key).val = value; //更新key值
        }else{
            //不存在,需要插入
            //判断容量是否达上限
            if(map.size()==capacity){
                //如果达上限,需将双向链表末尾的值去除,将待添加的值放在表头
                int rk = tail.prev.key;
                tail.prev.prev.next = tail;
                tail.prev = tail.prev.prev;
                map.remove(rk);
            }
            Node node = new Node(key,value);
            map.put(key,node);
            moveToHead(node);
        }
    }

    public void moveToHead(Node node){
        node.next = head.next;
        node.prev = head;
        head.next.prev = node;
        head.next = node;
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

合并K个升序链表

//给你一个链表数组,每个链表都已经按升序排列。
//
// 请你将所有链表合并到一个升序链表中,返回合并后的链表。
//
//
//
// 示例 1:
//
// 输入:lists = [[1,4,5],[1,3,4],[2,6]]
//输出:[1,1,2,3,4,4,5,6]
//解释:链表数组如下:
//[
// 1->4->5,
// 1->3->4,
// 2->6
//]
//将它们合并到一个有序链表中得到。
//1->1->2->3->4->4->5->6

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        //分而治之,所有链表进行排序,两个两个合并,归并排序
        if(lists==null || lists.length==0) return null;
        return merge(lists,0,lists.length-1);
    }
    private ListNode merge(ListNode[] lists, int left,int right){
        if(left==right) return lists[left];
        int mid = left+(right-left)/2;
        ListNode l1 = merge(lists,left,mid);
        ListNode l2 = merge(lists,mid+1,right);
        return mergeTwoLists(l1,l2);
    }

    private ListNode mergeTwoLists(ListNode l1,ListNode l2){
        if(l1==null) return l2;
        if(l2==null) return l1;
        if(l1.val<=l2.val) {
            l1.next = mergeTwoLists(l1.next,l2);
            return l1;
        }
        else {
            l2.next = mergeTwoLists(l1,l2.next);
            return l2;
        }
    }
}

三、栈、队列

栈和队列最好都用LinkedList实现

  • stack方法都是加锁的,线程安全效率低,stack上推荐用Deque<Integer> stack = new LinkedList<Integer>();替换stack,看源码便知

有效的括号

思路:栈

import java.util.Stack;
public boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    if(s.isEmpty()) return true;
    char[] chars = s.toCharArray();
    //遍历所有的元素
    for (char c : chars) {
        //如果是左括号,就把他们对应的右括号压栈
        if (c == '(') {
            stack.push(')');
        } else if (c == '{') {
            stack.push('}');
        } else if (c == '[') {
            stack.push(']');
        } else if (stack.isEmpty() || stack.pop() != c) {
            //否则就只能是右括号。
            //1,如果栈为空,说明括号无法匹配。
            //2,如果栈不为空,栈顶元素就要出栈,和这个右括号比较。
            //如果栈顶元素不等于这个右括号,说明无法匹配,
            //直接返回false。
            return false;
        }
    }
    //最后如果栈为空,说明完全匹配,是有效的括号。
    //否则不完全匹配,就不是有效的括号
    return stack.isEmpty();
}
class Solution {
    public boolean isValid(String s) {
        //栈
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            switch (aChar){
                case '(':
                    stack.push(')');
                    continue;
                case '[':
                    stack.push(']');
                    continue;
                case '{':
                    stack.push('}');
                    continue;
                default:
                    if(stack.isEmpty() || stack.pop()!=aChar){ //此处已经将匹配的括号pop了
                        return false;
                    }
            }
        }
        if(stack.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}

两个栈实现队列

思想:push操作就是往stack1中push,
pop操作需要分类:如果stack2为空,那么需要将stack1中的数据转移到stack2中,然后对stack2进行pop,如果stack2不为空,直接pop

import java.util.Stack;
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        if (stack2.size() <= 0) {
            while (stack1.size() != 0) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}
class CQueue {
    Deque<Integer> stack1;
    Deque<Integer> stack2;
    public CQueue() {
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    }
    
    public void appendTail(int value) {
        stack1.push(value);
    }
    
    public int deleteHead() {
        if(!stack2.isEmpty()){
            return stack2.pop();
        }else if(!stack1.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }   
            return stack2.pop();        
        }
        return -1;
    }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */
class MyQueue {
    Deque<Integer> inStack;
    Deque<Integer> outStack;
    /** Initialize your data structure here. */
    public MyQueue() {
        inStack = new LinkedList<>();
        outStack = new LinkedList<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        inStack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return outStack.isEmpty() && inStack.isEmpty();
    }

    public void in2out(){
        while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}

push时间复杂度
pop空间复杂度

最小栈 ×

题目:设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。

class MinStack {
    Deque<Integer> xStack;
    Deque<Integer> minStack;

    public MinStack() {
        xStack = new LinkedList<Integer>();
        minStack = new LinkedList<Integer>();
        minStack.push(Integer.MAX_VALUE);
    }
    
    public void push(int x) {
        xStack.push(x);
        minStack.push(Math.min(minStack.peek(), x));
    }
    
    public void pop() {
        xStack.pop();
        minStack.pop();
    }
    
    public int top() {
        return xStack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

队列的最大值

请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。
若队列为空,pop_front 和 max_value 需要返回 -1

class MaxQueue {
//用双端队列确保队列单调递减
    Queue<Integer> q;//保存所有入队列的值
    Deque<Integer> d;//单调队列
    public MaxQueue() {
        q = new LinkedList<Integer>();  //注意队列的定义
        d = new LinkedList<Integer>();
    }
    
    public int max_value() {
        if(d.isEmpty()){
            return -1;
        }
        return d.peekFirst();  //返回队首值
    }
    
    public void push_back(int value) {//放入当前值时,判断队列中是否有小于此值的值
    //有的话,将小于当前值的值,全部弹出,确保队列单调递减
        while(!d.isEmpty() && d.peekLast() < value){
            d.pollLast(); //从队列尾部取出比当前值小的值
        }
        d.offerLast(value);
        q.offer(value);
    }
    
    public int pop_front() {
        if(q.isEmpty()){
            return -1;
        }
        int ans = q.poll();
        if(ans == d.peekFirst()){
            d.pollFirst();
        }
        return ans;
    }
}

/**
 * Your MaxQueue object will be instantiated and called as such:
 * MaxQueue obj = new MaxQueue();
 * int param_1 = obj.max_value();
 * obj.push_back(value);
 * int param_3 = obj.pop_front();
 */

四、二叉树

二叉树的右视图

//每一层入队列,返回每一层队列的最后一个节点
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root!=null){
            queue.add(root);
        }
        while(!queue.isEmpty()){
            List<Integer> level = new ArrayList<>();
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
            }
            res.add(level.get(n-1));
        }
        return res;

二叉树的前序遍历(非递归)

import java.util.*;
public class TreeNode{
    public int val;
    TreeNode left = null;
    TreeNode right = null;
}
public static void preOrder(TreeNode head) {
	if (head == null) {
		return;
	}
	Stack<TreeNode> stack = new Stack<>();
	stack.push(head);
	while (!stack.isEmpty()) {
		TreeNode node = stack.pop();
		System.out.print(node.val + " ");
		if (node.right != null) {
			stack.push(node.right);
		}
		if (node.left != null) {
			stack.push(node.left);
		}
	}
}

二叉树的中序遍历(非递归)

public static void inOrder(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur = head;
	Stack<TreeNode> stack = new Stack<>();
	while (!stack.isEmpty() || cur != null) {
		while (cur != null) {
			stack.push(cur);
			cur = cur.left;
		}
		TreeNode node = stack.pop();
		System.out.print(node.value + " ");
		if (node.right != null) {
			cur = node.right;
		}
	}
}

二叉树的后序遍历(非递归)

前序遍历的过程 是 中左右。
将其转化成 中右左。也就是压栈的过程中优先压入左子树,在压入右子树。
然后将这个结果返回来,这里是利用栈的先进后出倒序打印。

public static void postOrder(TreeNode head) {
		if (head == null) {
			return;
		}
		Stack<TreeNode> stack1 = new Stack<>();
		Stack<TreeNode> stack2 = new Stack<>();
		stack1.push(head);
		while (!stack1.isEmpty()) {
			TreeNode node = stack1.pop();
			stack2.push(node);
			if (node.left != null) {
				stack1.push(node.left);
			}
			if (node.right != null) {
				stack1.push(node.right);
			}
		}
		while (!stack2.isEmpty()) {
			System.out.print(stack2.pop().value + " ");
		}
	}

二叉树的层序遍历(队列)

有两种类型
队列操作:
add()和remove()方法在失败时会抛出异常(不推荐)
queue.offer():添加元素
queue.poll():返回第一个元素,并在队列中删除
queue.peek():返回第一个元素
queue.size():队列长度

//类型一
[
  [3],
  [9,20],
  [15,7]
]
public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> res = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();
    if (root != null) {
        queue.add(root);
    }
    while (!queue.isEmpty()) {
        int n = queue.size();
        List<Integer> level = new ArrayList<>();
        for (int i = 0; i < n; i++) { 
            TreeNode node = queue.poll();
            level.add(node.val);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        res.add(level);
    }
    return res;
}
//类型二[3,9,20,15,7]
class Solution {
    public int[] levelOrder(TreeNode root) {
        if(root == null) return new int[0];
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
        queue.add(root);
    	}
        ArrayList<Integer> ans = new ArrayList<>();
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            ans.add(node.val);
            if(node.left != null) queue.add(node.left);
            if(node.right != null) queue.add(node.right);
        }
        int[] res = new int[ans.size()];
        for(int i = 0; i < ans.size(); i++)
            res[i] = ans.get(i);
        return res;
    }
}

之字形打印二叉树

给定二叉树: [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其层次遍历结果:
[
[3],
[20,9],
[15,7]
]

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            if(res.size() % 2 == 1) Collections.reverse(tmp);
            res.add(tmp);
        }
        return res;
    }
}

二叉搜索树

二叉搜索树的第K小个节点

  • 思想:DFS 二叉搜索树的中序遍历时递增序列
  • 时间复杂度:O(N),遍历了整个树。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public int kthSmallest(TreeNode root, int k) {
    Stack<TreeNode> stk = new Stack<>();
    int cnt = 0;
    while (!stk.isEmpty() || root != null) {
        if (root != null) {
            stk.push(root);
            root = root.left;
        } else {
            TreeNode cur = stk.pop();
            if (++cnt == k) return cur.val;
            root = cur.right;
        }
    }
    return -1;
}

二叉搜索树的第K大节点

中序遍历是递增序列
利用倒序的中序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int result;
    int count;
    public int kthLargest(TreeNode root, int k) {
        dfs(root,k);
        return result;
    }
    void dfs(TreeNode root,int k){
        if(root==null) return;
        dfs(root.right,k);
        count++;
        if(count==k){
            result = root.val;
            return;
        }
        dfs(root.left,k);
    }
}

验证二叉搜索树

假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

为什么JDK建议使用ArrayDeque实现栈

public boolean isValidBST(TreeNode root) {
    if (root == null)
        return true;
    Stack<TreeNode> stack = new Stack<>();
    TreeNode pre = null;
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        if (pre != null && root.val <= pre.val)
            return false;
        //保存前一个访问的结点
        pre = root;
        root = root.right;
    }
    return true;
}

时间复杂度 : O(n)O(n),其中 nn 为二叉树的节点个数。二叉树的每个节点最多被访问一次,因此时间复杂度为 O(n)O(n)。
空间复杂度 : O(n)O(n),其中 nn 为二叉树的节点个数。栈最多存储 nn 个节点,因此需要额外的 O(n)O(n) 的空间。

对称二叉树

题目:判断是否为对称二叉树

class Solution {
	public boolean isSymmetric(TreeNode root) {
		if(root==null || (root.left==null && root.right==null)) {
			return true;
		}
		//用队列保存节点
		LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
		//将根节点的左右孩子放到队列中
		queue.add(root.left);
		queue.add(root.right);
		while(queue.size()>0) {
			//从队列中取出两个节点,再比较这两个节点
			TreeNode left = queue.removeFirst();
			TreeNode right = queue.removeFirst();
			//如果两个节点都为空就继续循环,两者有一个为空就返回false
			if(left==null && right==null) {
				continue;
			}
			if(left==null || right==null) {
				return false;
			}
			if(left.val!=right.val) {
				return false;
			}
			//将左节点的左孩子, 右节点的右孩子放入队列
			queue.add(left.left);
			queue.add(right.right);
			//将左节点的右孩子,右节点的左孩子放入队列
			queue.add(left.right);
			queue.add(right.left);
		}
		return true;
	}
}

二叉树的最大深度

//DFS 递归
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return Math.max(l, r) + 1;
    }
}

时间复杂度:O(n)
空间复杂度:O(height),树的高度

//BFS
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while(!queue.isEmpty()){
            int n = queue.size();
            for(int i = 0; i < n; i++){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
            depth++;
        }
        return depth;
    }
}

时间复杂度:O(n)
空间复杂度:O(width),树的宽度,最坏情况O(n)

从前序和中序遍历序列构造二叉树

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
            if(preorder.length == 0 || inorder.length == 0){
                return null;
            }
            TreeNode tree = new TreeNode(preorder[0]);
            for(int i=0;i<preorder.length;i++){
                if(preorder[0] == inorder[i]){
                    tree.left = buildTree(Arrays.copyOfRange(preorder,1,i+1),Arrays.copyOfRange(inorder,0,i));
                    tree.right = buildTree(Arrays.copyOfRange(preorder,i+1,preorder.length),Arrays.copyOfRange(inorder,i+1,inorder.length));
                }
            }
            return tree;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //将中序放入map,便于查找根节点在中序的下标
    private Map<Integer,Integer> map = new HashMap<>();
    private int[] preorder;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        for(int i = 0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        return recur(0,0,inorder.length-1);
    }

    //i表示在前序中的下标,当前根节点的下标
    //left表示在中序的子树,最左的下标
    //right表示在中序的子树,最右的下标
    private TreeNode recur(int i,int left,int right){
        if(left>right){
            return null;
        }
        TreeNode root = new TreeNode(preorder[i]);   //先建立根节点
        root.left = recur(i+1,left,map.get(preorder[i])-1);//左子树
        int leftSize = map.get(preorder[i])-left;//左子树的节点个数
        root.right = recur(i+leftSize+1 , map.get(preorder[i])+1 , right);//右子树
        return root;
    }
}
  • 二叉树展开为链表
  • 二叉树中的最大路径和
  • 单词拆分
  • 打家劫舍
  • 岛屿数量
  • 课程表
  • 实现前缀树
  • 最大正方形

翻转二叉树

用队列 BFS

    public TreeNode invertTree(TreeNode root) {
        if (root == null)
            return root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);//相当于把数据加入到队列尾部
        while (!queue.isEmpty()) {
            //poll方法相当于移除队列头部的元素
            TreeNode node = queue.poll();
            //先交换子节点
            TreeNode left = node.left;
            node.left = node.right;
            node.right = left;

            if (node.left != null)
                queue.add(node.left);
            if (node.right != null)
                queue.add(node.right);
        }
        return root;
    }

二叉树的最近公共祖先

后序遍历

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null) return right;
        if(right == null) return left;
        return root;
    }
}

判断平衡二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return height(root) != -1;
    }
    private int height(TreeNode node) {
        if (node == null) {
            return 0;
        }

        int left = height(node.left);
        if (left == -1) {
            return -1;
        }
        int right = height(node.right);
        // 如果左子树或者右子树返回的高度为-1.直接返回,不再继续向下递归
        if (right == -1) {
            return -1;
        }
        // 判断左右子树高度差
        if (Math.abs(left - right) >= 2) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }
}

N皇后

解题思路
经典回溯题
以行为单位,每一行放置一个Queen
【返回的条件】:每一行都走完了,说明找到了一个可行解,加入到结果列表中
【如何进行迭代】:遍历一行中所有的位置,判断每个位置的有效性,即向上寻找是否已经有Queen,和斜向是否有Queen(都是向上寻找即可,因为下面的还没有放),如果满足条件,则进行递归,继续找下一行。

class Solution {
    List<List<String>> result = new ArrayList<>();
    public List<List<String>> solveNQueens(int n) {
        backtrack(new ArrayList<>(), 0, n);
        return result;
    }

    public void backtrack(List<String> list, int row, int n) {
        // 满足条件,加入到结果列表中,然后返回。
        if(row == n) {
            result.add(new ArrayList<>(list));
            return;
        }
        for(int i = 0; i < n; i++) {
            // 如果放置在该位置合法,则进行递归
            if(isValid(i, list, row, n)) {
                // 生成该行的字符串
                list.add(generateString(i, n));
                backtrack(list, row+1, n);
                list.remove(list.size()-1);
            }
        }
    }

    public boolean isValid(int pos, List<String> list, int row, int n) {
        // 判断竖着有没有重合的
        for(int i = 0; i < row; i++) {
            if(list.get(i).charAt(pos) == 'Q') {
                return false;
            }
        }
        // 判断斜向有没有重合的
        for(int i = row-1, j = pos-1; i >= 0 && j >= 0; i--, j--) {
            if(list.get(i).charAt(j) == 'Q') {
                return false;
            }
        }
        for(int i = row-1, j = pos+1; i >= 0 && j < n; i--, j++) {
            if(list.get(i).charAt(j) == 'Q') {
                return false;
            }
        }
        return true;
    }

    public String generateString(int pos, int n) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < pos; i++) {
            sb.append(".");
        }
        sb.append("Q");
        for(int i = pos+1; i < n; i++) {
            sb.append(".");
        }
        return sb.toString();
    }
}

五、排序算法

在这里插入图片描述

十大经典排序算法(Java实现)

快排

public class QuickSort {
    public static void main(String[] args) {
        int [] a = {1,6,8,7,3,5,16,4,8,36,13,44};
        quickSort(a,0,a.length-1);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
    }
    private static void quickSort(int[] nums,int left,int right){
        //定义递归出口
        if (nums.length<0){
            return;
        }
        if(left>=right){
            return;
        }
        int i = left;
        int j = right;
        int pivot = nums[left];
        while(i<j){
            while(i<j && nums[j]>=pivot){
                j--;
            }
            nums[i]=nums[j];
            while(i<j && nums[i]<=pivot){
                i++;
            }
            nums[j]=nums[i];
        }
        nums[i]=pivot;
        quickSort(nums,left,i-1);
        quickSort(nums,i+1,right);
    }
}

大根堆+排序

//建立大根堆
public class DuiSort {
    //堆是完全二叉树
    private static void heapify(int[] tree,int n,int i){
        //最后写递归出口
        if(i>=n){
            return;
        }
        //n:树中的节点数   i:表示对哪个节点做heapify
        int c1 = 2*i+1;//左孩子
        int c2 = 2*i+2;//右孩子
        int max = i;//先假定最大值的根节点
        //比较三者大小
        if(c1<n && tree[c1]>tree[max]){//注意c1和c2必须是存在的,也就是说i是非叶子节点
            max = c1;
        }
        if(c2<n && tree[c2]>tree[max]){
            max = c2;
        }
        if(max!=i){//也就是说存在孩子节点大于父节点,则需要进行交换
            swap(tree,max,i);//只是将对应下标的数组值进行交换。max还指向的是孩子节点
            heapify(tree,n,max);//继续对向下孩子节点进行递归heapify
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private static void build_heap(int[] tree,int n){
        int last_node = n-1;
        int parent = (last_node-1)/2;
        for (int i = parent; i >= 0 ; i--) {//从最后一个非叶子节点开始,从下至上,从右至左
            heapify(tree,n,i);
        }
    }
   
    private static void heap_sort(int[] tree,int n){//堆排序
        build_heap(tree,n);
        for (int i = n-1; i >=0 ; i--) {
            swap(tree,0,i);//交换根节点与最后一个节点,也就是把最大值取下来
            build_heap(tree,i);//然后重新建堆, i可以代表当前这棵树的节点个数
        }
    }
    public static void main(String[] args) {
        int tree[] = {5,9,6,3,8,7};
        heap_sort(tree,6);
        for (int i = 0; i < 6; i++) {
            System.out.println(tree[i]);
        }
    }
}

归并排序

/**
     * 归并排序
     *
     * @param array
     * @return
     */
    public static int[] MergeSort(int[] array) {
        if (array.length < 2) return array;
        int mid = array.length / 2;
        int[] left = Arrays.copyOfRange(array, 0, mid);
        int[] right = Arrays.copyOfRange(array, mid, array.length);
        return merge(MergeSort(left), MergeSort(right));
    }
    /**
     * 归并排序——将两段排序好的数组结合成一个排序数组
     *
     * @param left
     * @param right
     * @return
     */
    public static int[] merge(int[] left, int[] right) {
        int[] result = new int[left.length + right.length];
        for (int index = 0, i = 0, j = 0; index < result.length; index++) {
            if (i >= left.length)
                result[index] = right[j++];
            else if (j >= right.length)
                result[index] = left[i++];
            else if (left[i] > right[j])
                result[index] = right[j++];
            else
                result[index] = left[i++];
        }
        return result;
    }

冒泡排序

/**
     * 冒泡排序
     *
     * @param array
     * @return
     */
    public static int[] bubbleSort(int[] array) {
        if (array.length == 0)
            return array;
        for (int i = 0; i < array.length; i++)
            for (int j = 0; j < array.length - 1 - i; j++)
                if (array[j + 1] < array[j]) {
                    int temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
        return array;
    }

二分查找

public int search(int[] nums, int target) {
        //二分查找
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(nums[mid]>target) right = mid-1;
            else if(nums[mid]<target) left = mid+1;
            else return mid;
        }
        return -1;
    }

六、字符串

数组的为length
字符串的为length()

替换空格

输入:s = “We are happy.”
输出:“We%20are%20happy.”

//法一:利用StringBuilder  效率更高
class Solution {
    public String replaceSpace(String s) {
    //StringBuilder效率高,不安全; StringBuffer效率低,安全;
        StringBuilder res = new StringBuilder();
        for(Character c : s.toCharArray()) //字符串转为字符数组
        {
            if(c == ' ') res.append("%20");
            else res.append(c);
        }
        return res.toString(); //字符数组再转为字符串
    }
}

        // 法二:直接利用字符串  效率低
        // String res = "";
        // for(int i=0;i<s.length();i++){
        //     if(s.charAt(i)==' '){
        //         res+="%20";
        //     }else{
        //         res+=s.charAt(i);
        //     }
        // }
        // return res;

第一个只出现一次的字符

题目:s = “abaccdeff”
返回 “b”
s = “”
返回 " "

class Solution {
    public char firstUniqChar(String s) {
        if(s.equals("")){
            return ' ';
        }
        char[] arr =s.toCharArray();
        HashMap<Character,Integer>map = new HashMap<>();
        for(int i = 0;i < arr.length;i++){
            map.put(arr[i],map.getOrDefault(arr[i],0)+1);
        }
        int k = 0;
        for( k = 0;k < arr.length;k++){
            if(map.get(arr[k]) == 1){
                break;
            }
        }
        if(k >= arr.length){
            return ' ';
        }else{
        return arr[k];
        }
    }
}

无重复字符的最长子串(滑动窗口)

题目:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
思路:滑动窗口

import java.util.HashMap;

public class LengthOfLongestSubstring {
    public static int lengthOfLongestSubstring(String s) {
        if (s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        int left = 0;
        for(int i = 0; i < s.length(); i ++){
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }
}

时间复杂度:O(n)

最长回文子串

思路:中心扩散法
首先往左寻找与当期位置相同的字符,直到遇到不相等为止。
然后往右寻找与当期位置相同的字符,直到遇到不相等为止。
最后左右双向扩散,直到左和右不相等。

public class LongestHuiWen {
    public static void main(String[] args) {
        System.out.println(longestPalindrome1("babad"));
    }
    public static String longestPalindrome1(String s) {
        if (s.length() == 0) {
            return "";
        }
        int left = 0;
        int right = 0;
        int len = 1;
        int maxStart = 0;//用来记录子串的起始位置
        int maxLen = 0;

        for (int i = 0; i < s.length(); i++) {
            left = i - 1;
            right = i + 1;
            while (left >= 0 && s.charAt(left) == s.charAt(i)) {
                //向左扩散
                len++;
                left--;
            }
            while (right < s.length() && s.charAt(right) == s.charAt(i)) {
                //向右扩散
                len++;
                right++;
            }
            while (left >= 0 && right < s.length() && s.charAt(right) == s.charAt(left)) {
                //两边扩散
                len = len + 2;
                left--;
                right++;
            }
            if (len > maxLen) {
                maxLen = len;
                maxStart = left;
            }
            len = 1;
        }
        return s.substring(maxStart + 1, maxStart + maxLen + 1);
    }
}

时间复杂度:O(n^2),其中 n 是字符串的长度。长度为 1和 2的回文中心分别有 n 和 n-1个,每个回文中心最多会向外扩展 O(n)次。
空间复杂度:O(1)。

最长公共子串

题目:给定两个字符串str1和str2,输出两个字符串的最长公共子串
题目保证str1和str2的最长公共子串存在且唯一。
思路滑动窗口/动态规划 start和end描述了一个窗口
从str1中按照窗口截取窗口子串,检查str2中是否包含,如果包含就扩展窗口
如果str2中不包含窗口子串,我们就移动窗口起始位置
sb是用来记录最大的窗口子串的,窗口最小为0个单位

public static String LCS2(String str1, String str2) {
        if (str1 == null || str2 == null) return "-1";
        int n1 = str1.length(), n2 = str2.length();
        if (n1 == 0 || n2 == 0) return "-1";
        int[][] dp = new int[n1 + 1][n2 + 1];
 
        int maxLen = 0, index = 0;
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    if (j == 0 && i == 0){
                        dp[i][j]=1;
                        maxLen = dp[i][j];
                        index = i;
                    } else{
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                        if(maxLen <= dp[i][j]){
                            maxLen = dp[i][j];
                            index = i;
                        }
                    }
                }
            }
        }
        return maxLen == 0 ? "-1" : str1.substring(index - maxLen+1, index+1);
    }

字符串转换成整数

public int myAtoi(String s) {
        //去除空格
        String s1 = s.trim();
        if(s1.length()==0) return 0;
        boolean negative = false; //标记是正负

        char[] chars = s1.toCharArray();
        long res = 0;
        int i =0;
        //判断第一个字符是不是数字
        if((chars[i]<'0' || chars[i]>'9') && chars[i]!='+' && chars[i]!='-' ) return 0;

        if(chars[i]=='+'){
            i++;
        }else if(chars[i]=='-'){
            negative = true;
            i++;
        }

        for (; i < chars.length; i++) {
            if(chars[i]>='0' && chars[i]<='9'){
                res = res*10 + chars[i] - '0';
                if(!negative && res>Integer.MAX_VALUE) return Integer.MAX_VALUE;
                if(negative && -res<Integer.MIN_VALUE) return Integer.MIN_VALUE;
            }else break;
        }
        return (int)(negative? -res:res);
    }
class Solution {
    public int strToInt(String str) {
        //空串,返回0
        if(str.length() == 0) return 0;
        //转换为char数组方便遍历
        char[] s = str.toCharArray();
        //下标指针
        int i = 0;
        //结果
        long total = 0;
        //过滤空格
        while(i < s.length - 1 && s[i] == ' ') i++;
        //有效首字符非法,返回0
        if((s[i] - '0' < 0 || s[i] - '0' > 9) && (s[i] != '+' && s[i] != '-')) return 0;
        //正负号标识
        boolean negative = false;
        if(s[i] == '-'){
            i++;
            negative = true;
        }else if(s[i] == '+'){
            i++;
        }
        //进位累加,溢出返回整数边界
        while(i < s.length && s[i] - '0' >= 0 && s[i] - '0' <= 9){
            total = total * 10 + s[i] - '0';
            if(!negative && total > Integer.MAX_VALUE) return Integer.MAX_VALUE;
            else if(negative && -total < Integer.MIN_VALUE) return Integer.MIN_VALUE;
            i++;
        }
        //返回结果
        return (int)(negative? -total: total);
    }
}

输入: “4193 with words”
输出: 4193
解释: 转换截止于数字 ‘3’ ,因为它的下一个字符不为数字。

public int strToInt(String str) {
    str = str.trim();//去掉前后的空格
    //如果为空,直接返回0
    if (str.length() == 0)
        return 0;
    int index = 0;//遍历字符串中字符的位置
    int res = 0;//最终结果
    int sign = 1;//符号,1是正数,-1是负数,默认为正数
    int length = str.length();
    //判断符号
    if (str.charAt(index) == '-' || str.charAt(index) == '+'){
        sign = str.charAt(i) == '+'?1:-1;
        index++;
    }
    for (; index < length; ++index) {
        //取出字符串中字符,然后转化为数字
        int digit = str.charAt(index) - '0';
        //按照题中的要求,读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。
        //字符串的其余部分将被忽略。如果读取了非数字,后面的都要忽略
        if (digit < 0 || digit > 9)
            break;
        //越界处理
        if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10))
            return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        else
            res = res * 10 + digit;
    }
    return sign * res;
}

左旋转字符串

题目:把字符串前面的若干个字符转移到字符串的尾部
输入: s = “abcdefg”, k = 2
输出: “cdefgab”

class Solution {
    public String reverseLeftWords(String s, int n) {
        return s.substring(n, s.length()) + s.substring(0, n);
    }
}
class Solution {
    public String reverseLeftWords(String s, int n) {
        String res = "";
        for(int i = n; i < s.length(); i++)
            res += s.charAt(i);
        for(int i = 0; i < n; i++)
            res += s.charAt(i);
        return res;
    }
}

字符串逆序

        // 方法一:
        for (int i = 0; i < a.length(); i++) {
            one += a.substring(a.length() - 1 - i, a.length() - i);
        }

        // 方法二:
        StringBuffer stringBuffer = new StringBuffer(a);
        two = stringBuffer.reverse().toString();

四则运算

题目:解析一般数学算式,实现简单的带括号的加减乘除运算。
思想:使用两个栈,一个数字栈,一个符号栈
从左往右遍历表达式字符串
遇到数字,直接压入数字栈
遇到符号
遇到左括号,直接入符号栈
遇到右括号,”符号栈弹栈取栈顶符号b,数字栈弹栈取栈顶数字a1,数字栈弹栈取栈顶数字a2,计算a2 b a1 ,将结果压入数字栈”,重复引号步骤至取栈顶为左括号,将左括号弹出
遇到运算符,1)若该运算符的优先级大于栈顶元素的优先级,直接入符号栈。2)若小于,”符号栈弹栈取栈顶符号b,数字栈弹栈取栈顶数字a1,数字栈弹栈取栈顶数字a2,计算a2 b a1 ,将结果压入数字栈”,重复引号步骤至该运算符的优先级大于符号栈顶元素的优先级,然后将该符号入符号栈
遍历结束后,”符号栈弹栈取栈顶符号b,数字栈弹栈取栈顶数字a1,数字栈弹栈取栈顶数字a2,计算a2 b a1 ,将结果压入数字栈”,重复引号步骤至符号栈无符号(或数字栈只有一个元素),则数字栈的元素为运算结果
参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值