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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

leetcode中求subset、全排列等问题的回溯算法总结

發布時間:2025/7/25 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode中求subset、全排列等问题的回溯算法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 在leetcode上刷題的時候,偶然看到一位仁兄總結的關于尋找數組的子集(78,90)、全排列(46,47)、在數組中找出等于固定值的元素的集合(39,40)、找出字符串回文子串的集合(131),感覺很驚喜,所以搬運到這里分享給大家,下邊是原文鏈接,里面也有很多討論。https://discuss.leetcode.com/topic/46161/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioning/2

? ? ? 里面比較難想的部分(對于我這種只撿easy模式的題目做的算法小白)是循環里面的遞歸,每次退棧的時候,會從cur中remove一個元素出來,然后i要加1,繼續循環!!,而且一定要弄清楚退棧后i的值是多少。建議大家拿比較簡單的{1,2,3}來跟著程序走一遍,能充分的說明問題,如果實在不能理解一層層遞歸中的各個變量的值的變化,建議在eclipse中,自己打斷點,一步步走,觀測各個值的變化。

? ? ? 一通百通,只要搞懂第一個題目,其他的一看就會了。

?

78 Subset

Given a set of?distinct?integers,?nums, return all possible subsets.

Note:?The solution set must not contain duplicate subsets.

For example,
If?nums?=?[1,2,3], a solution is:

[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[] ] public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> list = new ArrayList<>();Arrays.sort(nums);backtrack(list, new ArrayList<>(), nums, 0);return list; }private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){list.add(new ArrayList<>(tempList));for(int i = start; i < nums.length; i++){tempList.add(nums[i]);backtrack(list, tempList, nums, i + 1);tempList.remove(tempList.size() - 1);} }

?

90 Subsets II (contains duplicates)

Given a collection of integers that might contain duplicates,?nums, return all possible subsets.

Note:?The solution set must not contain duplicate subsets.

For example,
If?nums?=?[1,2,2], a solution is:

[[2],[1],[1,2,2],[2,2],[1,2],[] ] public List<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> list = new ArrayList<>();Arrays.sort(nums);backtrack(list, new ArrayList<>(), nums, 0);return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){list.add(new ArrayList<>(tempList));for(int i = start; i < nums.length; i++){if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]);backtrack(list, tempList, nums, i + 1);tempList.remove(tempList.size() - 1);} }

46?Permutations?

Given a collection of?distinct?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],[3,2,1] ] public List<List<Integer>> permute(int[] nums) {List<List<Integer>> list = new ArrayList<>();// Arrays.sort(nums); // not necessarybacktrack(list, new ArrayList<>(), nums);return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){if(tempList.size() == nums.length){list.add(new ArrayList<>(tempList));} else{for(int i = 0; i < nums.length; i++){ if(tempList.contains(nums[i])) continue; // element already exists, skip tempList.add(nums[i]);backtrack(list, tempList, nums);tempList.remove(tempList.size() - 1);}} }

47 Permutations II (contains duplicates)?

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2]?have the following unique permutations:

[[1,1,2],[1,2,1],[2,1,1] ] public List<List<Integer>> permuteUnique(int[] nums) {List<List<Integer>> list = new ArrayList<>();Arrays.sort(nums);backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, boolean [] used){if(tempList.size() == nums.length){list.add(new ArrayList<>(tempList));} else{for(int i = 0; i < nums.length; i++){if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue;used[i] = true; tempList.add(nums[i]);backtrack(list, tempList, nums, used);used[i] = false; tempList.remove(tempList.size() - 1);}} }

39 Combination Sum?

Given a?set?of candidate numbers (C)?(without duplicates)?and a target number (T), find all unique combinations in?C?where the candidate numbers sums to?T.

The?same?repeated number may be chosen from?C?unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

?

For example, given candidate set?[2, 3, 6, 7]?and target?7,?
A solution set is:?

[[7],[2, 2, 3] ] public List<List<Integer>> combinationSum(int[] nums, int target) {List<List<Integer>> list = new ArrayList<>();Arrays.sort(nums);backtrack(list, new ArrayList<>(), nums, target, 0);return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){if(remain < 0) return;else if(remain == 0) list.add(new ArrayList<>(tempList));else{ for(int i = start; i < nums.length; i++){tempList.add(nums[i]);backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elementstempList.remove(tempList.size() - 1);}} }

40 Combination Sum II (can't reuse same element)?

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in?C?where the candidate numbers sums to?T.

Each number in?C?may only be used?once?in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

?

For example, given candidate set?[10, 1, 2, 7, 6, 1, 5]?and target?8,?
A solution set is:?

[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6] ] public List<List<Integer>> combinationSum2(int[] nums, int target) {List<List<Integer>> list = new ArrayList<>();Arrays.sort(nums);backtrack(list, new ArrayList<>(), nums, target, 0);return list;}private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){if(remain < 0) return;else if(remain == 0) list.add(new ArrayList<>(tempList));else{for(int i = start; i < nums.length; i++){if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]);backtrack(list, tempList, nums, remain - nums[i], i + 1);tempList.remove(tempList.size() - 1); }} }

131 Palindrome Partitioning

Given a string?s, partition?s?such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of?s.

For example, given?s?=?"aab",
Return

[["aa","b"],["a","a","b"] ] public List<List<String>> partition(String s) {List<List<String>> list = new ArrayList<>();backtrack(list, new ArrayList<>(), s, 0);return list; }public void backtrack(List<List<String>> list, List<String> tempList, String s, int start){if(start == s.length())list.add(new ArrayList<>(tempList));else{for(int i = start; i < s.length(); i++){if(isPalindrome(s, start, i)){tempList.add(s.substring(start, i + 1));backtrack(list, tempList, s, i + 1);tempList.remove(tempList.size() - 1);}}} }public boolean isPalindrome(String s, int low, int high){while(low < high)if(s.charAt(low++) != s.charAt(high--)) return false;return true; }

?

轉載于:https://www.cnblogs.com/chenzhima/p/6440930.html

總結

以上是生活随笔為你收集整理的leetcode中求subset、全排列等问题的回溯算法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 女仆裸体打屁屁羞羞免费 | 亚洲自拍av在线 | 日韩午夜伦 | 欧美亚洲国产一区二区三区 | 日韩黄色在线视频 | av东方在线 | 亚洲小说区图片区 | 欧美18aaaⅹxx| 成人国产av一区二区三区 | 亚洲日本网站 | 色版视频在线观看 | 亚洲欧美日韩一区在线观看 | 午夜免费高清视频 | a级片免费在线观看 | av免费观看网 | 精品少妇视频 | 五月天综合视频 | 曰本无码人妻丰满熟妇啪啪 | hd极品free性xxx护士 | 成人影片在线免费观看 | 波多野结衣在线免费视频 | 少妇高潮一区二区三区69 | 日韩国产欧美精品 | 四虎在线看片 | 色人阁在线视频 | 手机看片福利久久 | 亚洲天堂网站 | 亚洲国产欧美在线 | 国产极品999| 国产一区二区三区成人 | 亚洲人女屁股眼交6 | 国产九九热 | 国产成人三级在线观看视频 | 久久久久一 | 久久综合干 | 婷婷色在线 | 久久久久在线视频 | 娇喘顶撞深初h1v1 | 羞羞草影院 | 国产精品免费视频一区二区三区 | 男女黄床上色视频 | 不卡的av在线播放 | 欧美一二 | 荫道bbwbbb高潮潮喷 | √天堂中文官网8在线 | 日日摸日日碰 | 人妻无码一区二区三区免费 | 国产精品夜夜嗨 | 欧美一区二区三 | 波多野在线观看 | 一区二区三区免费在线 | 国产美女裸体无遮挡免费视频 | 毛片网站免费观看 | 国产精品久久久久久在线观看 | 2020亚洲天堂 | 亚洲最大网站 | www.av成人 | 四虎永久在线观看 | 亚洲国产精品一 | 久久免费一区 | 中文字幕被公侵犯的漂亮人妻 | 亚洲国产欧美在线观看 | 男女野外做受全过程 | 欧美黄在线观看 | 三级成人 | 午夜激情免费视频 | 图书馆的女友在线观看 | 欧美性做爰猛烈叫床潮 | 久久依人网 | 情五月 | 国内少妇精品 | 波多野结衣精品视频 | 亚洲第一综合色 | 小敏的受孕日记h | 日本伦理一区二区三区 | 欧美成人一区二免费视频软件 | 一级性生活大片 | 激情六月综合 | 日本一区免费 | 黄色香港三级三级三级 | 亚洲aⅴ乱码精品成人区 | 97色伦图片| 一区视频 | 久久电影一区 | 96超碰在线| 阿的白色内裤hd中文 | 999www | 人与动物2免费观看完整版电影高清 | 色综合自拍 | 国精产品一区一区三区mba下载 | 97国产精品视频 | 在线观看福利电影 | 天天看天天摸天天操 | 在线小视频 | 99视频国产精品免费观看a | 国产精品久久久久久网站 | 久久中文网 | 国产一级不卡毛片 | 国产色黄 |