日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Permutation 和 Combination

發布時間:2023/12/16 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Permutation 和 Combination 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • Permutation
      • 代碼
      • 代碼核心思路
    • Combination
      • 代碼
      • 代碼核心思路
    • 總結

Permutation 和 Combination是算法中非常常見的兩種數據的排列方式,也就是數學中的排列和組合。

Permutation: 排列,指從給定個數的元素中取出指定個數的元素進行排序。
Combination: 組合,指從給定個數的元素中僅僅取出指定個數的元素,不考慮排序。

本文的主要目的在于總結在算法題中,這兩種類型題目的做題模板。雖然題目變化可能是多樣的,但是萬變不離其宗。


Permutation

以leetcode題目46. Permutations為例。

題目敘述如下:

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

代碼

class Solution {public List<List<Integer>> permute(int[] nums) {List<List<Integer>> ans = new ArrayList<>();dfs(ans, new ArrayList<>(), 0, nums);return ans;}private void dfs(List<List<Integer>> ans, List<Integer> list, int begin, int[] nums) {if (list.size() == nums.length) {ans.add(new ArrayList<>(list));return;}if (begin >= nums.length) {return;}for (int i = begin; i < nums.length; i++) {swap(nums, begin, i);list.add(nums[begin]);dfs(ans, list, begin + 1, nums);list.remove(list.size() - 1);swap(nums, begin, i);}return;}private void swap(int[] nums, int a, int b) {int tmp = nums[a];nums[a] = nums[b];nums[b] = tmp;} }

代碼核心思路

  • DFS;
  • 交換(如題目中的swap)
  • 保存中間結果(如函數dfs的第二個參數List<Integer> list )
  • 保存最終結果(如函數dfs的第一個參數List<List<Integer>> ans)
  • 恢復現場(list.remove(list.size() - 1))

  • Combination

    以leetcode題目combination-sum-iii為例。

    題目敘述如下:

    Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
    Only numbers 1 through 9 are used.
    Each number is used at most once.
    Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

    Example

    Input: k = 3, n = 7
    Output: [[1,2,4]]
    Explanation:
    1 + 2 + 4 = 7
    There are no other valid combinations.

    代碼

    class Solution {public List<List<Integer>> combinationSum3(int k, int n) {List<List<Integer>> ans = new ArrayList<>();dfs(ans, k, n, new ArrayList<>(), 1);return ans;}private void dfs(List<List<Integer>> ans, int k, int n, List<Integer> list, int begin) {if (n == 0 && list.size() != 0 && k == 0) {ans.add(new ArrayList<Integer>(list));return;}if (n < 0 || begin > 9 || k < 0) {return;}for (int i = begin; i <= 9; i++) {list.add(i);dfs(ans, k - 1, n - i, list, i + 1);list.remove(list.size() - 1);}return;} }

    代碼核心思路

  • DFS;
  • 保存中間結果(如函數dfs的第四個參數List<Integer> list )
  • 保存最終結果(如函數dfs的第一個參數List<List<Integer>> ans)
  • 恢復現場(list.remove(list.size() - 1))
  • 總結

    共同點:

  • DFS
  • 需要追蹤中間結果
  • 需要保存最終結果
  • 需要恢復現場
  • 根據需求在dfs函數內由for循環來找答案。
  • 總結

    以上是生活随笔為你收集整理的Permutation 和 Combination的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。