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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][第491题][JAVA][递增子序列][回溯][RK算法]

發(fā)布時(shí)間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][第491题][JAVA][递增子序列][回溯][RK算法] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【問(wèn)題描述】[中等]


【解答思路】

1. 二進(jìn)制枚舉 + 哈希

復(fù)雜度

class Solution {List<Integer> temp = new ArrayList<Integer>();List<List<Integer>> ans = new ArrayList<List<Integer>>();Set<Integer> set = new HashSet<Integer>();int n;public List<List<Integer>> findSubsequences(int[] nums) {n = nums.length;//遍歷所有可能 3位 由000-111(7=1<<3)for (int i = 0; i < (1 << n); ++i) {findSubsequences(i, nums);int hashValue = getHash(263, (int) 1E9 + 7);if (check() && !set.contains(hashValue)) {ans.add(new ArrayList<Integer>(temp));set.add(hashValue);}}return ans;} //7 ->111 全都選public void findSubsequences(int mask, int[] nums) {temp.clear();for (int i = 0; i < n; ++i) {if ((mask & 1) != 0) {temp.add(nums[i]);}mask >>= 1;}} //RK算法 public int getHash(int base, int mod) {int hashValue = 0;for (int x : temp) {//數(shù)組中有負(fù)數(shù),范圍是[-100, 100],加101相當(dāng)于將數(shù)組中的數(shù)往上提了100,并不會(huì)影響hash函數(shù)值的唯一性hashValue = hashValue * base % mod + (x + 101);hashValue %= mod;}return hashValue;} //檢查是否有序public boolean check() {for (int i = 1; i < temp.size(); ++i) {if (temp.get(i) < temp.get(i - 1)) {return false;}}return temp.size() >= 2;} }
2. 遞歸枚舉 + 減枝

一個(gè)遞歸枚舉子序列的通用模板,即用一個(gè)臨時(shí)數(shù)組temp 來(lái)保存當(dāng)前選出的子序列,使用cur 來(lái)表示當(dāng)前位置的下標(biāo),在 dfs(cur, nums) 開(kāi)始之前,[0,cur?1] 這個(gè)區(qū)間內(nèi)的所有元素都已經(jīng)被考慮過(guò),而[cur,n] 這個(gè)區(qū)間內(nèi)的元素還未被考慮。在執(zhí)行 dfs(cur, nums) 時(shí),我們考慮 cur 這個(gè)位置選或者不選,如果選擇當(dāng)前元素,那么把當(dāng)前元素加入到temp 中,然后遞歸下一個(gè)位置,在遞歸結(jié)束后,應(yīng)當(dāng)把temp 的最后一個(gè)元素刪除進(jìn)行回溯;如果不選當(dāng)前的元素,直接遞歸下一個(gè)位置。

List<List<Integer>> ans = new ArrayList<List<Integer>>(); List<Integer> temp = new ArrayList<Integer>(); public void dfs(int cur, int[] nums) {if (cur == nums.length) {// 判斷是否合法,如果合法判斷是否重復(fù),將滿足條件的加入答案if (isValid() && notVisited()) {ans.add(new ArrayList<Integer>(temp));}return;}// 如果選擇當(dāng)前元素temp.add(nums[cur]);dfs(cur + 1, nums);temp.remove(temp.size() - 1);// 如果不選擇當(dāng)前元素dfs(cur + 1, nums); }

復(fù)雜度

class Solution {List<Integer> temp = new ArrayList<Integer>();List<List<Integer>> ans = new ArrayList<List<Integer>>();public List<List<Integer>> findSubsequences(int[] nums) {dfs(0, Integer.MIN_VALUE, nums);return ans;}public void dfs(int cur, int last, int[] nums) {if (cur == nums.length) {if (temp.size() >= 2) {ans.add(new ArrayList<Integer>(temp));}return;}if (nums[cur] >= last) {temp.add(nums[cur]);dfs(cur + 1, nums[cur], nums);temp.remove(temp.size() - 1);}if (nums[cur] != last) {dfs(cur + 1, last, nums);}} }鏈接:https://leetcode-cn.com/problems/increasing-subsequences/solution/di-zeng-zi-xu-lie-by-leetcode-solution/

【總結(jié)】

1. 一個(gè)遞歸枚舉子序列的通用模板,即用一個(gè)臨時(shí)數(shù)組temp 來(lái)保存當(dāng)前選出的子序列,使用cur 來(lái)表示當(dāng)前位置的下標(biāo),在 dfs(cur, nums) 開(kāi)始之前,[0,cur?1] 這個(gè)區(qū)間內(nèi)的所有元素都已經(jīng)被考慮過(guò),而[cur,n] 這個(gè)區(qū)間內(nèi)的元素還未被考慮。在執(zhí)行 dfs(cur, nums) 時(shí),我們考慮 cur 這個(gè)位置選或者不選,如果選擇當(dāng)前元素,那么把當(dāng)前元素加入到temp 中,然后遞歸下一個(gè)位置,在遞歸結(jié)束后,應(yīng)當(dāng)把temp 的最后一個(gè)元素刪除進(jìn)行回溯;如果不選當(dāng)前的元素,直接遞歸下一個(gè)位置。
List<List<Integer>> ans = new ArrayList<List<Integer>>(); List<Integer> temp = new ArrayList<Integer>(); public void dfs(int cur, int[] nums) {if (cur == nums.length) {// 判斷是否合法,如果合法判斷是否重復(fù),將滿足條件的加入答案if (isValid() && notVisited()) {ans.add(new ArrayList<Integer>(temp));}return;}// 如果選擇當(dāng)前元素temp.add(nums[cur]);dfs(cur + 1, nums);temp.remove(temp.size() - 1);// 如果不選擇當(dāng)前元素dfs(cur + 1, nums); }
2.RK算法 哈希散列表

【數(shù)據(jù)結(jié)構(gòu)與算法】字符串匹配 BF算法 RK算法

轉(zhuǎn)載鏈接:https://leetcode-cn.com/problems/increasing-subsequences/solution/di-zeng-zi-xu-lie-by-leetcode-solution/

總結(jié)

以上是生活随笔為你收集整理的[Leetcode][第491题][JAVA][递增子序列][回溯][RK算法]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。