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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

【問題描述】[中等]


【解答思路】

1. 二進制枚舉 + 哈希

復雜度

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) {//數組中有負數,范圍是[-100, 100],加101相當于將數組中的數往上提了100,并不會影響hash函數值的唯一性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. 遞歸枚舉 + 減枝

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

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) {// 判斷是否合法,如果合法判斷是否重復,將滿足條件的加入答案if (isValid() && notVisited()) {ans.add(new ArrayList<Integer>(temp));}return;}// 如果選擇當前元素temp.add(nums[cur]);dfs(cur + 1, nums);temp.remove(temp.size() - 1);// 如果不選擇當前元素dfs(cur + 1, nums); }

復雜度

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/

【總結】

1. 一個遞歸枚舉子序列的通用模板,即用一個臨時數組temp 來保存當前選出的子序列,使用cur 來表示當前位置的下標,在 dfs(cur, nums) 開始之前,[0,cur?1] 這個區間內的所有元素都已經被考慮過,而[cur,n] 這個區間內的元素還未被考慮。在執行 dfs(cur, nums) 時,我們考慮 cur 這個位置選或者不選,如果選擇當前元素,那么把當前元素加入到temp 中,然后遞歸下一個位置,在遞歸結束后,應當把temp 的最后一個元素刪除進行回溯;如果不選當前的元素,直接遞歸下一個位置。
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) {// 判斷是否合法,如果合法判斷是否重復,將滿足條件的加入答案if (isValid() && notVisited()) {ans.add(new ArrayList<Integer>(temp));}return;}// 如果選擇當前元素temp.add(nums[cur]);dfs(cur + 1, nums);temp.remove(temp.size() - 1);// 如果不選擇當前元素dfs(cur + 1, nums); }
2.RK算法 哈希散列表

【數據結構與算法】字符串匹配 BF算法 RK算法

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

總結

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

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