【LeetCode-面試演算法經典-Java實現】【216-Combination Sum III (組合數的和)】,leetcode--java

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【216-Combination Sum III (組合數的和)】,leetcode--java
【216-Combination Sum III (組合數的和)】【LeetCode-面試演算法經典-Java實現】【所有題目目錄索引】代碼下載【https://github.com/Wang-Jun-Chao】原題

  Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Ensure that numbers within the set are sorted in ascending order.
  Example 1:
  Input: k = 3, n = 7
  Output:
  [[1,2,4]]
  Example 2:
  Input: k = 3, n = 9
  Output:
  [[1,2,6], [1,3,5], [2,3,4]]

題目大意

  尋找所有滿足k個數之和等於n的組合,只允許使用數字1-9,並且每一種組合中的數字應該是唯一的。確保組合中的數字以遞增順序排列。

解題思路

  回溯法

代碼實現

演算法實作類別

import java.util.Collections;import java.util.LinkedList;import java.util.List;public class Solution {    public List<List<Integer>> combinationSum3(int k, int n) {        // 用於儲存所有結果        List<List<Integer>> result = new LinkedList<>();        // 用於儲存中間結果        List<Integer> list = new LinkedList<>();        // 條件滿足就進行解題操作        if (k > 0 && k <= 9) {                solve(k, 1, n, 0, list, result);        }        // 返回結果        return result;    }    /**     * 求解方法     *     * @param k         每個解的元素個數     * @param cur       當前處理第k個元素     * @param remainder k - cur + 1個元素的和     * @param prevVal   第cur-1個元素的取值     * @param list      將解的元素的集合類     * @param result    儲存所有結果的容器     */    public void solve(int k, int cur, int remainder, int prevVal, List<Integer> list, List<List<Integer>> result) {        // 處理最後一個元素        if (cur == k) {            // remainder為最後一個解元素的值,它必須大於前一個解元素的值,並且不能超出9            if (remainder > prevVal && remainder <= 9) {                // 添加元素值                list.add(remainder);                // 拷貝結果到新的隊列中                List<Integer> one = new LinkedList<>();                for (Integer i : list) {                    one.add(i);                }                // 儲存結果                result.add(one);                // 刪除最後一個元素,進行現場還原                list.remove(list.size() - 1);            }        }        // 不是最後一個元素        else if (cur < k){            for (int i = prevVal + 1; i <= 9 - (k - cur); i++) {                // 添加元素                list.add(i);                // 遞迴求解                solve(k, cur + 1, remainder - i, i, list, result);                // 現場還原                list.remove(list.size() - 1);            }        }    }}
評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中查看完整圖片。

特別說明歡迎轉載,轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/48046271】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.