代码随想录五刷day8

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、力扣77. 组合

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        fun(n,k,1,1);
        return res;
    }
    public void fun(int n, int k, int cur, int deep){
        if(deep > k){
            res.add(new ArrayList<>(path));
            return ;
        }
        for(int i = cur; i <= n ; i ++){
            path.add(i);
            fun(n, k, i+1, deep+1);
            path.remove(path.size()-1);
        }
    }
}

二、力扣216. 组合总和 III

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        fun(k,n,0,1);
        return res;
    }
    public void fun(int k , int n, int sum, int cur){
        if(path.size() == k){
            if(sum == n){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        for(int i = cur; i < 10; i ++){
            path.add(i);
            fun(k, n, sum + i, i + 1);
            path.removeLast();
        }
    }
}

二、力扣17. 电话号码的字母组合

class Solution {
    String[] str = new String[]{
        "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
    };
    List<String> res = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if(digits == null || digits.equals("")){
            return res;
        }
        fun(digits, 0);
        return res;
    }
    public void fun(String digits, int cur){
        if(sb.length() >= digits.length()){
            res.add(sb.toString());
            return;
        }
        String s = str[digits.charAt(cur)-'0'];
        for(int i = 0; i < s.length();i ++){
            sb.append(s.charAt(i));
            fun(digits, cur +1);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}

三、力扣39. 组合总和

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        fun(candidates, target,0, 0);
        return res;
    }
    public void fun(int[] candidates, int target,int cur ,int sum){
        if(sum >= target){
            if(sum == target){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        for(int i = cur; i < candidates.length; i ++){
            path.add(candidates[i]);
            fun(candidates, target, i,sum + candidates[i]);
            path.removeLast();
        }
    }
}

四、力扣40. 组合总和 II

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        fun(candidates, target, 0, 0);
        return res;
    }
    public void fun(int[] candidates, int target, int sum, int cur){
        if(sum >= target){
            if(sum == target){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        for(int i = cur; i < candidates.length; i ++){
            if(i != cur && candidates[i] == candidates[i-1]){
                continue;
            }
            if(sum + candidates[i] > target){
                return;
            }
            path.add(candidates[i]);
            fun(candidates, target, sum + candidates[i], i+1);
            path.removeLast();
        }
    }
}

五、力扣131. 分割回文串

class Solution {
    List<List<String>> res = new ArrayList<>();
    List<String> str = new ArrayList<>();
    public List<List<String>> partition(String s) {
        fun(s, 0, new StringBuilder());
        return res;
    }
    public void fun(String s, int idx, StringBuilder sb){
        if(idx == s.length()){
            res.add(new ArrayList<>(str));
            return;
        }
        for(int i = idx; i < s.length(); i ++){
            sb.append(s.charAt(i));
            if(chech(s,idx, i)){
                str.add(sb.toString());
                fun(s, i+1, new StringBuilder());
                str.remove(str.size()-1);
            }
        }
    }
    public boolean chech(String s, int i, int j){
        for(; i <= j; i ++, j --){
            if(s.charAt(i) != s.charAt(j)){
                return false;
            }
        }
        return true;
    }
}

六、力扣93. 复原 IP 地址

class Solution {
    List<String> res = new ArrayList<>();
    List<String> path = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if(s.length() < 4 || s.length() > 12){
            return res;
        }
        fun(s,0, new StringBuilder());
        return res;
    }
    public void fun(String s, int idx, StringBuilder sb1){
        if(idx == s.length()){
            if(path.size() == 4){
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < 4; i ++){
                    sb.append(path.get(i));
                    if(i < 3){
                        sb.append(".");
                    }
                }
                res.add(sb.toString());
            }
            return;
        }
        for(int i = idx; i < s.length(); i ++){
            sb1.append(s.charAt(i));
            if(funA(sb1)){
                path.add(sb1.toString());
                fun(s, i+1, new StringBuilder());
                path.remove(path.size()-1);
            }
        }
    }
    public boolean funA(StringBuilder sb1){
        if(sb1.length() > 1 && sb1.charAt(0) == '0'){
            return false;
        }
        if(sb1.length() > 3){
            return false;
        }
        int sum = 0, k = 1;
        for(int i = sb1.length()-1; i >= 0 ; i --){
            int a = sb1.charAt(i) - '0';
            sum += (a * k);
            k *= 10;
        }
        if(sum < 0 || sum > 255){
            return false;
        }
        return true;
    }
}

七、力扣78. 子集

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        fun(nums,0);
        return res;
    }
    public void fun(int[] nums, int idx){
        res.add(new ArrayList<>(path));
        if(idx >= nums.length){
            return;
        }
        for(int i = idx; i < nums.length; i ++){
            path.add(nums[i]);
            fun(nums, i +1);
            path.remove(path.size()-1);
        }
    }
}

八、力扣90. 子集 II

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        fun(nums, 0);
        return res;
    }
    public void fun(int[] nums, int idx){
        res.add(new ArrayList<>(path));
        if(idx >= nums.length){
            return;
        }
        for(int i = idx; i < nums.length; i ++){
            if(i != idx && nums[i] == nums[i-1]){
                continue;
            }
            path.add(nums[i]);
            fun(nums, i+1);
            path.remove(path.size()-1);
        }
    }
}

九、力扣491. 非递减子序列

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        fun(nums, 0, new HashSet());
        return res;
    }
    public void fun(int[] nums, int idx, Set<Integer> set){
        if(path.size() >= 2){
            res.add(new ArrayList<>(path));
        }
        if(idx >= nums.length){
            return;
        }
        for(int i = idx; i < nums.length; i ++){
            if(set.contains(nums[i])){
                continue;
            }
            if(funA(path, nums[i])){
                set.add(nums[i]);
                path.add(nums[i]);
                fun(nums, i+1, new HashSet<>());
                path.remove(path.size()-1);
            }
        }
    }
    public boolean funA(List<Integer> path, int a){
        if(path == null || path.size() == 0){
            return true;
        }
        if(path.size() == 1){
            return path.get(0) <= a;
        }
        for(int i = 1; i < path.size(); i ++){
            if(path.get(i) < path.get(i-1)){
                return false;
            }
        }
        return path.get(path.size()-1) <= a ;
    }
}

十、力扣46. 全排列

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permute(int[] nums) {
        flag = new boolean[nums.length];
        fun(nums);
        return res;
    }
    public void fun(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i ++){
            if(flag[i]){
                continue;
            }
            path.add(nums[i]);
            flag[i] = true;
            fun(nums);
            path.remove(path.size()-1);
            flag[i] = false;
        }
    }
}

十一、力扣47. 全排列 II

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permuteUnique(int[] nums) {
        flag = new boolean[nums.length];
        Arrays.sort(nums);
        fun(nums);
        return res;
    }
    public void fun(int[] nums){
        if(path.size() >= nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i ++){
            if(flag[i]){
                continue;
            }
            if(i > 0 && nums[i] == nums[i-1] && flag[i-1]){
                continue;
            }
            path.add(nums[i]);
            flag[i] = true;
            fun(nums);
            path.remove(path.size()-1);
            flag[i] = false;
        }
    }
}

十二、力扣90. 子集 II

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        fun(nums,0);
        return res;
    }
    public void fun(int[] nums, int idx){
        res.add(new ArrayList<>(path));
        if(idx == nums.length){
            return;
        }
        for(int i = idx; i < nums.length; i ++){
            if(i > idx && nums[i] == nums[i-1]){
                continue;
            }
            path.add(nums[i]);
            fun(nums, i +1);
            path.remove(path.size()-1);
        }
    }
}

十三、力扣40. 组合总和 II

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        fun(candidates, target, 0, 0);
        return res;
    }
    public void fun(int[] candidates, int target, int sum, int cur){
        if(sum >= target){
            if(sum == target){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        for(int i = cur; i < candidates.length; i ++){
            if(i > cur && candidates[i] == candidates[i-1]){
                continue;
            }
            path.add(candidates[i]);
            fun(candidates, target, sum+candidates[i], i+1);
            path.remove(path.size()-1);
        }
    }
}

十四、力扣47. 全排列 II

在这里插入代码片class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permuteUnique(int[] nums) {
        flag = new boolean[nums.length];
        Arrays.sort(nums);
        fun(nums);
        return res;
    }
    public void fun(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i ++){
            //同一树层
            if(i > 0 && nums[i] == nums[i-1] && !flag[i-1]){
                continue;
            }
            if(!flag[i]){
                flag[i] = true;
                path.add(nums[i]);
                fun(nums);
                flag[i] = false;
                path.remove(path.size()-1);
            }
        }
    }
}

十五、力扣332. 重新安排行程

class Solution {
    Deque<String> res = new LinkedList<>();
    Map<String, Map<String,Integer>> map = new HashMap<>();
    public List<String> findItinerary(List<List<String>> tickets) {
        for(List<String> list : tickets){
            Map<String, Integer> temp = null;
            if(map.containsKey(list.get(0))){
                temp = map.get(list.get(0));
                temp.put(list.get(1), temp.getOrDefault(list.get(1),0) + 1);
            }else{
                temp = new TreeMap<>();
                temp.put(list.get(1), 1);
            }
            map.put(list.get(0), temp);
        }
        res.add("JFK");
        fun(tickets.size() + 1);
        return new ArrayList<>(res);
    }
    public boolean fun(int ticketNum){
        if(res.size() == ticketNum){
            return true;
        }
        String last = res.getLast();
        if(map.containsKey(last)){
            for(Map.Entry<String,Integer> entry : map.get(last).entrySet()){
                int count = entry.getValue();
                if(count > 0){
                    res.add(entry.getKey());
                    entry.setValue(count - 1);
                    if(fun(ticketNum)){
                        return true;
                    }
                    entry.setValue(count);
                    res.removeLast();
                }
            }
        }
        return false;
    }
}

十六、力扣51. N 皇后

class Solution {
    List<List<String>> res = new ArrayList<>();
    char[][] path;
    public List<List<String>> solveNQueens(int n) {
        path = new char[n][n];
        for(char[] c : path){
            Arrays.fill(c,'.');
        }
        fun(n, 0);
        return res;
    }
    public void fun(int n, int cur){
        if(cur == n){
            List<String> temp = new ArrayList<>();
            for(int i = 0; i <n; i ++){
                StringBuilder sb = new StringBuilder();
                for(int j = 0; j < n; j ++){
                    if(path[i][j] != 'Q'){
                        sb.append(".");
                    }else{
                        sb.append("Q");
                    }
                }
                temp.add(sb.toString());
            }
            res.add(temp);
            return;
        }
        for(int i = 0; i < n; i ++){
            if(funA(cur, i, n)){
                path[cur][i] = 'Q';
                fun(n, cur + 1);
                path[cur][i] = '.';
            }
        }
    }
    public boolean funA(int l, int d, int n){
        for(int i = 0; i < n; i ++){
            if(path[l][i] == 'Q'){
                return false;
            }
        }

        for(int i = 0; i < n; i ++){
            if(path[i][d] == 'Q'){
                return false;
            }
        }

        for(int i = l, j = d; i >= 0 && j >= 0; i --, j --){
            if(path[i][j] == 'Q'){
                return false;
            }
        }
        for(int i = l, j = d; i >= 0 && j < n; i --, j ++){
            if(path[i][j] == 'Q'){
                return false;
            }
        }
        return true;
    }
}

十七、力扣37. 解数独

class Solution {
    public void solveSudoku(char[][] board) {
        fun(board);
    }
    public boolean fun(char[][] board){
        for(int i = 0; i < 9; i ++){
            for(int k = 0; k < 9; k ++){
                if(board[i][k] != '.'){
                    continue;
                }
                for(char j = '1'; j <= '9'; j ++){
                    if(funA(board, i,k, j)){
                        board[i][k] = j;
                        if(fun(board)){
                            return true;
                        }
                        board[i][k] = '.';
                    }
                }
                return false;
            }
        }
        return true;
    }
    public boolean funA(char[][] board, int cur, int low, char c){
        for(int i = 0; i < 9; i ++){
            if(board[cur][i] == c){
                return false;
            }
        }
        for(int i = 0; i < 9; i ++){
            if(board[i][low] == c){
                return false;
            }
        }
        int startRow = (cur/3)*3;
        int startCol = (low/3)*3;
        for(int i = startRow; i < startRow + 3; i ++){
            for(int j = startCol; j < startCol + 3; j ++){
                if(board[i][j] == c){
                    return false;
                }
            }
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值