LeetCode—permutations_combinations_permutations-sequence(全排列——DFS深度优先搜索)—java

138 篇文章 0 订阅
132 篇文章 0 订阅

题目描述

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3]have the following permutations:
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].

思路解析

https://blog.csdn.net/Lynn_Baby/article/details/79736771

跟这个题是比较类似的

  • 首先需要有一个临时的item来一一添加num[i],然后长度达到num.length时,就可以添加进入res了
  • 然后每个num[i]需要标记是否访问过,所以需要用到boolean类型的数组
  • 注意添加后,需要回退之前的item

代码

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> item = new ArrayList<Integer>();
        if(num==null || num.length==0){
            return res;
        }
        boolean[] visited = new boolean[num.length];
        helper(res,item,num,visited);
        return res;
    }
    private void helper(ArrayList<ArrayList<Integer>> res , ArrayList<Integer> item,int[] num,boolean[] visited){
        if(item.size() == num.length){
            res.add(new ArrayList<Integer>(item));
            return;
        }else{
            for(int i=0;i<num.length;i++){
                if(visited[i]==false){
                    item.add(num[i]);
                    visited[i]=true;
                    helper(res,item,num,visited);
                    item.remove(item.size()-1);
                    visited[i]=false;
                }
            }
        }
    }
}

Combinations

题目描述:

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

思路解析

  • 给出一个n和k,要求求出由n中k个不同的数组成是序列,序列为升序排序。
  • 这道题要求所有可能的情况,显然我们可以使用回溯的方法求解,对于每次判断的边界条件为:后面的数要大于前面的数。而且由于这里1到n肯定是递增的,所以继续进行下一层运算的条件可以为 当前位置后面的数。

代码

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> combine(int n, int k) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(n<=0||n<k)
            return res;
        ArrayList<Integer> list = new ArrayList<Integer>();
        dfs(res,list,1,n,k);
        return res;
    }
    public void dfs(ArrayList<ArrayList<Integer>> res,ArrayList<Integer> list,int start,int n,int k){
        if(list.size()== k){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i=start;i<=n;i++){
            list.add(i);
            dfs(res,list,i+1,n,k);
            list.remove(list.size()-1);
        }
    }
}

permutations-sequence(全排列的第k个)

题目描述

The set[1,2,3,…,n]contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the k thpermutation sequence.

Note: Given n will be between 1 and 9 inclusive.

思路解析

  • 先把1到n的每个数存入list中,然后后边写一个数字,就可以从里面删掉一个了。
  • n个数字有n!个排列方式,n-1就有(n-1)!个排列方式,
  • 为了和下标保持一致,需要k--,k/(n-1)!是当前字符的下标(注意是从零开始啊,下标0对应的数字1)
  • 下一次的k就可以更新为 k%(n-1)!,循环n次
  • 需要一个times表示每次更新阶乘的除数。

代码

import java.util.*;
public class Solution {
    public String getPermutation(int n, int k) {
        k--;
        List<Integer> list = new ArrayList<Integer>();//注意存储1-n
        StringBuilder s = new StringBuilder();
        int times = n-1;
        for(int i=1;i<=n;i++){
            list.add(i);
        }
        int factorail = 1;//阶乘
        for(int i=2;i<n;i++){//不要×n
            factorail*=i;
        }
        while(times>=0){
            int indexList = k/factorail;
            s.append(list.get(indexList));
            list.remove(indexList);
            k=k%factorail;
            if(times!=0){
                factorail/=times;
            }
            times--;
        }
        return s.toString();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和't'都只出现一次。因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。 示例 2: 输入: "cccaaa" 输出: "cccaaa" 解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。注意"cacaca"是不正确的,因为相同的字母必须放在一起。 示例 3: 输入: "Aabb" 输出: "bbAa" 解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。注意'A'和'a'被认为是两种不同的字符。 Java代码如下: ``` import java.util.*; public class Solution { public String frequencySort(String s) { if (s == null || s.length() == 0) { return ""; } Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : list) { char c = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { sb.append(c); } } return sb.toString(); } } ``` 解题思路: 首先遍历字符串,使用HashMap记录每个字符出现的次数。然后将HashMap转换为List,并按照出现次数从大到小进行排序。最后遍历排序后的List,将每个字符按照出现次数依次添加到StringBuilder中,并返回StringBuilder的字符串形式。 时间复杂度:O(nlogn),其中n为字符串s的长度。遍历字符串的时间复杂度为O(n),HashMap和List的操作时间复杂度均为O(n),排序时间复杂度为O(nlogn),StringBuilder操作时间复杂度为O(n)。因此总时间复杂度为O(nlogn)。 空间复杂度:O(n),其中n为字符串s的长度。HashMap和List的空间复杂度均为O(n),StringBuilder的空间复杂度也为O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值