代碼隨想錄算法訓練營|第二十九天|39. 组合总和、40.组合总和II、131.分割回文串。刷题心得(c++)

SAP 更改物料基本计量单位 创建订单单位后,新创建的采购申请、采购合同、采购信息记录均会使用新的订单单位 阅读详情

目录

讀題

39. 组合总和

自己看到题目的第一想法

看完代码随想录之后的想法

40.组合总和II

自己看到题目的第一想法

看完代码随想录之后的想法

131.分割回文串

自己看到题目的第一想法

看完代码随想录之后的想法

39. 组合总和 - 實作

思路

錯誤思路

正確思路

剪枝思路

Code

錯誤代碼

正確代碼

剪枝代碼

40.组合总和II - 實作

思路

錯誤思路

正確思路

使用used的思路

Code

錯誤思路

正確思路

使用used 的思路

131.分割回文串 - 實作

思路

Code

總結

自己实现过程中遇到哪些困难

今日收获,记录一下自己的学习时长

相關資料

39. 组合总和

40.组合总和II

131.分割回文串


讀題

39. 组合总和

自己看到题目的第一想法

看到的想法其實跟昨天電話號碼的思路類似,可以想像每一層的集合都一樣,這個題目是由與數組長度n相等的n個集合,而集合內的元素都一致,終止這裡加入到result的結果要大於等於target並且如果是等於則將結果加入到result 當中, 這裏就可以藉由回溯法窮舉。

看完代码随想录之后的想法

看完對於startIndex的想法要記住以下在代碼隨想錄有提到的:

如果是一个集合求组合的话,就需要startIndex,例如:77.组合 (opens new window),216.组合总和III (opens new window)。

如果是多个集合取组合,各个集合之間互不影響,就不用startIndex,例如:17.电话号码的字母组合

一開始想成多個集合取組合但是雖然是這樣,但是集合之間會互相影響,就需要使用index了

自己對於這道題目沒有到非常透徹,看完之後才發現原來要排序,因為照原本的解法也可以通過,但是假設先進行排序,就可以在後續的過程中進行剪枝的操作,讓整體的速度再次提升,這點是我沒想到的。

40.组合总和II

自己看到题目的第一想法

看到題目的第一想法是使用set, 但我對set的作法實在不熟悉,整體思路想的是先進行排序,排序完後將數組傳遞到回溯函數當中,終止條件大致跟組合總和一樣,但加上一個index == candidates.size一致的狀況,至於去重我在想會不會可以使用一個機制是當我回溯完之後,因為數組已經排序過,那我們要做的事情其實就是不再用跟第一個相同的值,因為實際上在第一次回溯時都遍歷過了,那就下來遍歷就直接忽略相同值,讓index直接指向下一個不同的數值

看完代码随想录之后的想法

看完卡哥的想法之後,我發現我一開始就是直接用startIndex來進行去重,應該說我是使用i 來去重比較正確,在看卡哥的used去重時,需要花點時間來理解,而我的思考角度是從假設有很多個1 組成的集合 那我相同的元素在第一個一時都有遍歷過,就不需要再遍歷一次了,不過我還是需要再理解一下卡哥的思維。

反覆看了代碼隨想錄,大概能夠掌握used這個概念的思路,主要就是判斷目前這個重複是出現在樹層還是樹枝,如果是樹枝則繼續,如果重複是出現在樹層則跳過,理解完之後,在去寫code也很快能夠寫出來了。

131.分割回文串

自己看到题目的第一想法

分割我在思考的時候就有點像是我有很多不同的排列組合,整體感覺跟總合總合有點像,但是組合總合中間可以跳過某些節點,但是在分割時,不能跳過,以這個字串為例 aca 在組合裡可能會有三個回文組合 [“a” “c” “a” ] [“aca”] [”aa”] 但是在分割”aa”是不合法的。所以我要想的是,要怎麼去記錄分割,我在想會不會是迴圈的部分其實就是分割的長度,回溯就是進行分割,假設子字符串都符合條件,則將結果存入result,但還是想不通這段要怎麼操作

看完代码随想录之后的想法

看完之後,並且一步一步造著寫,有粗略的了解了概念,但至於是否有懂,可能還需要更多的題目來考驗,但整體思路很清晰,裡面有兩個重點如下,與自己第一次的想法有點類似,但我沒有想到startIndex到i之間就是子串區間,並且對於切割的思路也不清晰,還需要再多複習

  • startIndex = 切割線
  • startIndex i 左閉右閉的區間

39. 组合总和 - 實作

思路

錯誤思路

  1. 建立一個result 函數以及path 函數
  2. 建立一個回溯函數
  3. 傳入值 candidates and target and pathSum
  4. 終止條件: if pathSum ≥ target and if pathSum == target result.push_back(path) return
  5. 單遞迴
    1. for I = 0 ; i < candidate.size(); i ++)
      1. Path.push_back(candidates[i])
      2. backtracking(candidates, target, pathSum + candidates[i])
      3. Path.pop_back()
  6. return

主函式

backtracking( candidates, target, 0)

return result

錯誤點

  • 沒有一個startIndex 控制下一個集合出現的位置,在題意中我們要找尋唯一的組合,但一個數值又可以重複使用,仍然把全部的集合都當作不同的集合處理,但是因為內部數值都一樣,透過原本i + 1 控制startIndex 找下一個,我們透過傳入i 讓下一個數值由i 開始,而不是i + 1 也不是 0 ,如果是0的話會導致集合可能會出現一樣的組合,因為從0 開始,每個數組又都一樣,以example1 來說可能就會出現 [223] [232] [322]這三種組合,但題意是要我們找出不同的組合,所以我們透過startIndex來控制下一個遞迴從哪開始。

正確思路

  1. 建立一個result 函數以及path 函數
  2. 建立一個回溯函數
  3. 傳入值 candidates and target and pathSum and startIndex
  4. 終止條件: if pathSum ≥ target and if pathSum == target result.push_back(path) return
  5. 單遞迴
    1. for I = startIndex; i < candidate.size(); i ++)
      1. Path.push_back(candidates[i])
      2. backtracking(candidates, target, pathSum + candidates[i], i)
      3. Path.pop_back()
  6. return

主函式

backtracking( candidates, target, 0, 0)

return result

剪枝思路

多加兩個動作

  1. 排序candidates
  2. 假設當前的sum + candidates[i] > target則不用進去遞迴

Code

錯誤代碼

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int> candidates, int target, int pathSum) {
        if(pathSum >= target) {
            if(pathSum == target) results.push_back(path);
            return;
        }

        for(int i = 0; i < candidates.size(); i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, pathSum + candidates[i]);
            path.pop_back();
        }
        
        return;
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        backtracking(candidates, target, 0);
        return results;
    }
};

正確代碼

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int> candidates, int target, int pathSum, int startIndex) {
        if(pathSum >= target) {
            if(pathSum == target) results.push_back(path);
            return;
        }

        for(int i = startIndex; i < candidates.size(); i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, pathSum + candidates[i], i);
            path.pop_back();
        }
        
        return;
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        backtracking(candidates, target, 0, 0);
        return results;
    }
};

剪枝代碼

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int> candidates, int target, int pathSum, int startIndex) {
        if(pathSum >= target) {
            if(pathSum == target) results.push_back(path);
            return;
        }

        for(int i = startIndex; i < candidates.size() && pathSum + candidates[i] <= target; i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, pathSum + candidates[i], i);
            path.pop_back();
        }
        
        return;
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end()); // 需要排序
        backtracking(candidates, target, 0, 0);
        return results;
    }
};

 

40.组合总和II - 實作

思路

錯誤思路

  1. 建立一個result and path 儲存結果
  2. 建立一個回溯函數
    1. 傳入值 candidates target pathSum index
    2. 終止條件
      1. pathSum == target
      2. pathSum > target && index == candidates.size()
    3. 單遞迴
      1. i = index i < candidates.size I ++
      2. path.push_back(candidates[i])
      3. backtracking ( candidates, target, pathSum + candidates[i], i + 1)
      4. path.pop_back
      5. while ( candidates[i] == candidates[i+1] && i < candidates.size())
        1. i++

主函式

  1. qsort candidates
  2. backtracking (candidates, target, 0, 0)
  3. return result

錯誤點

  • pathSum > target && index == candidates.size() 應該為||
  • candidates[i] == candidates[i + 1] && i < candidates.size() 順序應相反,不然有可能出現操作空數組

正確思路

  1. 建立一個result and path 儲存結果
  2. 建立一個回溯函數
    1. 傳入值 candidates target pathSum index
    2. 終止條件
      1. pathSum == target
      2. pathSum > target || index == candidates.size()
    3. 單遞迴
      1. i = index i < candidates.size I ++
      2. path.push_back(candidates[i])
      3. backtracking ( candidates, target, pathSum + candidates[i], i + 1)
      4. path.pop_back
      5. while ( i < candidates.size() && candidates[i] == candidates[i+1] )
        1. i++

主函式

  1. sort candidates
  2. backtracking (candidates, target, 0, 0)
  3. return result

使用used的思路

藉由判斷used[i - 1]是 true 還是 false 判斷這個元素在同一層中是否重複

而要使用這個思路也必須要進行排序,才能夠避免這個元素在後面再次出現時,沒有被包含到前面的去重過程。

  1. 建立一個result and path 儲存結果
  2. 建立一個回溯函數
    1. 傳入值 candidates target pathSum index used
    2. 終止條件
      1. pathSum == target
      2. pathSum > target || index == candidates.size()
    3. 單遞迴
      1. i = index i < candidates.size I ++
      2. if( i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) conntinue;
      3. path.push_back(candidates[i])
      4. used[i] = true \\ 接下來在遞迴遇到相同的就是樹枝重複而不是樹層重複
      5. backtracking ( candidates, target, pathSum + candidates[i], i + 1)
      6. used[i] = false \\ 回溯操作,接下來的for loop就是樹層的操作,樹層不能重複,所以要返回false
      7. path.pop_back

主函式

  1. 建立一個bool 的used 數組
  2. sort candidates
  3. backtracking (candidates, target, 0, 0)
  4. return result

Code

錯誤思路

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int pathSum, int index) {
        if(pathSum == target) {
            results.push_back(path);
            return;
        }
        if(pathSum > target && index == candidates.size()) return;

        for(int i = index; i < candidates.size(); i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, pathSum + candidates[i], i + 1);
            path.pop_back();
            while(candidates[i] == candidates[i + 1] && i < candidates.size()) i++;
        }
        return;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        if(candidates.size() == 0) return results;
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0);
        return results;
    }
};

正確思路

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int pathSum, int index) {
        if(pathSum == target) {
            results.push_back(path);
            return;
        }
        if(pathSum > target || index == candidates.size()) return;

        for(int i = index; i < candidates.size(); i++) {
            path.push_back(candidates[i]);
            backtracking(candidates, target, pathSum + candidates[i], i + 1);
            path.pop_back();
            while( i + 1 < candidates.size() && candidates[i] == candidates[i + 1] ) i++;
        }
        return;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        if(candidates.size() == 0) return results;
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0);
        return results;
    }
};

使用used 的思路

class Solution {
public:
    vector<vector<int>> results;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int pathSum, int index, vector<bool>& used) {
        if(pathSum == target) {
            results.push_back(path);
            return;
        }
        if(pathSum > target || index == candidates.size()) return;

        for(int i = index; i < candidates.size() && pathSum + candidates[i] <= target; i++) {
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;
            path.push_back(candidates[i]);
            used[i] = true;
            backtracking(candidates, target, pathSum + candidates[i], i + 1, used);
            used[i] = false;
            path.pop_back();
        }
        return;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        if(candidates.size() == 0) return results;
        vector<bool> used(candidates.size(), false);
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, 0, used);
        return results;
    }
};

 

131.分割回文串 - 實作

思路

  1. 建立一個results 以及 path 儲存結果
  2. 回溯函數
    1. 傳入值 s, startIndex
    2. 終止條件
      1. startIndex大於s.size
      2. 如果startIndex大於s.size 代表已經找到一組切割方案,因為startIndex可以想像成切割線,如果切割線大於等於s的大小,代表已經沒有要切割的部分了
      3. 並且因為在單遞迴都有判斷是否為回文,所以如果到達終止條件的path裡的子字串都是回文
    3. 單遞迴
      1. i = startIndex; i < s.size(); i++
      2. 獲取startIndex 到 i 之間的區間→ 代表著s中的子串
        1. 以aab來說 如果startIndex = 0 則是從a 開始切割,遞回到下一層startIndex = 1 切割第二個a 、第三層startIndex = 2 切割b ,將結果存入path
        2. 第三層回到第二層 i 則++為 2 startIndex = 1 代表的是[1, 2 ]也就是ab這個區間,而這個區間不是回文則直接跳過
      3. 判斷startIndex 到 i之間是否為回文
        1. 如果是,則將這個子串存入path
        2. 不是則跳過這次迴圈
  3. 判斷回文函數
    1. 使用雙指針,假設前後不一致,則return false 否則return true;
  4. 主函式
    1. 傳入s以及0
    2. return results.

Code

class Solution {
public:
    vector<vector<string>> results;
    vector<string> path;
    void backtracking(const string& s, int startIndex) {
        // 如果startIndex大於s.size 代表已經找到一組切割方案,因為startIndex可以想像成切割線,如果切割線大於等於s的大小,代表已經沒有要切割的部分了
        if(startIndex >= s.size()) {
            results.push_back(path);
            return;
        }

        for(int i = startIndex; i < s.size(); i++) {
                //獲取[startIndex,i] 在s中的子串
            if(isPalindrome(s, startIndex, i)) {
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
            } else {
                continue; // 如果不是代表這個子串已經不是回文,等同這個切割方案不符合條件,則跳過這一次的處理
            }
            backtracking(s, i + 1);
            path.pop_back();
        }
    }
     bool isPalindrome(const string& s, int start, int end) {
     for (int i = start, j = end; i < j; i++, j--) {
         if (s[i] != s[j]) {
             return false;
         }
     }
     return true;
    }
    vector<vector<string>> partition(string s) {
        backtracking(s, 0);
        return results;
    }
};

 

總結

自己实现过程中遇到哪些困难

今天最大的困難點是在分割回文串的部分,自己花了蠻長時間去理解,但看完卡哥的講解後清晰很多,其次就是startIndex的使用,自己沒有到非常理解,不過看到代碼隨想錄當中組合總合裡面的總結,就清晰了很多,另外就是used數組的使用,這部分還需要一點時間來理解。

今日收获,记录一下自己的学习时长

今天大概學了2.5hr,整體學起來,包含自己想辦法做出來的題目都能夠AC真的很爽,另外在學習的過程中可以了解到不同的操作,也讓我的思考更廣了些。

相關資料

● 今日学习的文章链接和视频链接

39. 组合总和

题目链接/文章讲解:https://programmercarl.com/0039.组合总和.html

视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)| 回溯法精讲!_哔哩哔哩_bilibili

40.组合总和II

题目链接/文章讲解:https://programmercarl.com/0040.组合总和II.html

视频讲解:回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili

131.分割回文串

https://programmercarl.com/0131.分割回文串.html

视频讲解:带你学透回溯算法-分割回文串(对应力扣题目:131.分割回文串)| 回溯法精讲!_哔哩哔哩_bilibili

HDMI协议介绍(一)--Overview HDMI全称(High Definition Multimedia Interface)高清多媒体接口,支持在单线缆上传输全数字高清视频和多声道音频。 阅读详情

相关推荐

RAG-Anything 实战:构建企业级多模态知识图谱问答系统

本文详细介绍了如何利用RAG-Anything构建企业级多模态知识图谱问答系统,解决跨模态文档处理的痛点。通过架构设计解析、部署实战指南和典型业务场景解决方案,帮助企业实现高效的知识管理和智能检索,提升决策效率。

weixin_29089057的博客 385

代码随想录算法训练营day23 |39. 组合总和 、40.组合总和II 、131.分割回文串

碎碎念:加油参考:[代码随想录](https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html。

weixin_45182267的博客 1034

基于知识图谱的电影推荐系统——Neo4j&Python

也可直接从这里下载:链接: https://pan.baidu.com/s/1l6wjwcUzy5G_dIlVDbCkpw 提取码: pkq6。修改main.py中的driver,输入自己数据库的用户名与密码。系统会询问是否需要重新加载并创建知识图谱,在第一次时输入1。

weixin_46351593的博客 6355

【代码随想录算法训练营Day26】● 39. 组合总和;● 40.组合总和II;● 131.分割回文串

剪枝条件:当剩余备选数都比需要的数小时就不用遍历了。注意必须按顺序输出所以用队列不能用栈。先用自己和自己组合,直到超出。与39几乎一模一样,区别在于。

Tammy2001的博客 1041

OpenManus本地部署实战:从源码编译到浏览器路径优化全解析

本文详细解析了OpenManus本地部署的全过程,从源码编译到浏览器路径优化。涵盖环境准备、源码获取、模型配置、依赖安装等关键步骤,特别针对Windows系统下的chrome.exe路径修改提供了实用解决方案,帮助开发者高效完成部署并优化性能。

weixin_28826545的博客 379

VSLAM框架对比

格VSLAM特点介绍: (一)ORB-SLAM2 (二)ORB-SLAM3 (三)LSD-SLAM (四)

ljtx200888的博客 2293

代码随想录算法训练营第二十七天| LeetCode 39. 组合总和、40.组合总和II、131.分割回文串

回溯法在切割字符串中的使用,以及去重操作的实现方法

qq_64336203的博客 991

芋道前后端分离项目跳过登录

在此页面中可以看到当在没有获取到token时,如果此页面在白名单中则直接进入,所以我们将刚刚配置在。在此路径下配置需要跳过登录的页面路径,记住path中编辑的名称。根据以上两图可以发现芋道的。将所有需要跳过登录的方法。,否则会提示登录状态过期。至此完成免登录进入页面。

FanZaiYo的博客 5485

代码随想录算法训练营第二十三天 | 39. 组合总和、40. 组合总和 II、131. 分割回文串

文档讲解:https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html。文档讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html。文档讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html。

m0_45175414的博客 614

有限元ABAQUS软件安装后管理员身份运行报错:Cannot connect to license server system...如何解决?

🏆本文收录于 《全栈Bug调优(实战版)》 专栏,该专栏专注于分享我在真实项目开发中遇到的各类疑难Bug及其深层成因,并系统提供高效、可复现的解决思路和实操方案。无论你是刚入行的新手开发者,还是拥有多年项目经验的资深工程师,本专栏都将为你提供一条系统化、高质量的问题排查与优化路径,助力你加速成长,攻克技术壁垒,迈向技术价值最大化与职业发展的更高峰🚀!

**My Coding Family** 2067

【教程4>第5章>第23节】基于FPGA的Gardner环实现——环路滤波器模块和Gardner环整体测试

1]黄凌.基于FPGA的QPSK全数字调制解调系统的设计与实现[D].南京航空航天大学[2025-02-02].DOI:10.7666/d.d166927.注意,这里包括一个NCO模块,一个内插模块,一个时偏误差检测模块,一个环路滤波器模块,在gardner环中,wk的反馈输入,变量为ww,从而构成了闭环。[4]李司.基于FPGA的数字接收机同步技术的研究与实现[D].北京交通大学[2025-02-02].DOI:10.7666/d.y1082455.:调整NCO的相位步长,控制采样时刻。

FPGA/MATLAB学习教程/源码/项目合作开发 853

代码随想录算法训练营第二十七天 | 39. 组合总和、40.组合总和II、131.分割回文串

参考三数之和中的去重方法,我们在横向遍历的过程中,如果当前下标的元素和上一个下标的元素值相同,则跳过该元素,因为此时获得的组合结果一定是重复的。横向遍历的过程是找到划分中当前的分隔,纵向遍历的过程是找到下一个分隔。第一步,确定回溯函数的参数,我们需要指定当前已有元素的组合path,当前已经遍历到的下标starti,组合中元素和与target的差值left。第一步,确定回溯函数的参数,我们需要指定当前已有元素的组合path,当前已经遍历到的下标starti,组合中元素和与target的差值left。

owlmo的博客 1020

基于STM32、HAL库的IP2736U_EF快充协议芯片简介及驱动程序设计

@param connected: 连接状态指针 (1=已连接, 0=未连接)= 0x2736) // 检查厂商ID。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。

corlin6688的博客 430

_23LeetCode代码随想录算法训练营第二十三天-回溯算法 | 39.组合总和、40.组合总和II、131.分割回文串

_23LeetCode代码随想录算法训练营第二十三天-回溯算法 | 39.组合总和、40.组合总和II、131.分割回文串

Jasmine-Lily的博客 281

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

代码随想录算法训练营day23(python版)|第七章 回溯算法part02|·39. 组合总和·40.组合总和II·131.分割回文串

注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。本题是 集合里元素可以用无数次,那么和组合问题的差别 其实仅在于 startIndex上的控制。,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。:大盒子,收集所有“切出来每段都是回文”的方案(例如。:临时背包,装“当前已经选择的数字”(始终非降序)。:临时背包,保存“当前已选择的数字”(非降序)。(一条“切零段”的合法方案),这和题意一致。开始枚举(只往后看,避免重复排列);

qq_43752779的博客 609

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串

代码随想录算法训练营第二十六天 | 39. 组合总和,40.组合总和II,131.分割回文串

qq_41713240的博客 489

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【代码随想录】【算法训练营】【第27天】 [39]组合总和 [40] 组合总和II [131]分割回文串

思路及算法思维,指路。题目来自。day 25,周一,库存没了,哭死~

day day up 622

代码随想录算法训练营第二十六天|39. 组合总和、40.组合总和II、131.分割回文串

【代码】代码随想录算法训练营第二十六天|39. 组合总和、40.组合总和II、131.分割回文串。

qq_51395785的博客 962

代码随想录算法训练营第二十七天|●39. 组合总和 ● 40.组合总和II ● 131.分割回文串(JS写法)

题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ一开始自己写的大概和答案差不多,但是弄不明白回溯要传递的参数,但是自己一开始想到了终止条件,如果>7了就不对了,=7才对,

weixin_44776979的博客 782

算法训练营day27(回溯算法03:组合总和,组合总和2,分割回文串)

以后还是尽量把自己讲懂,独立写出来代码

weixin_61931006的博客 915

代码随想录算法训练营第二十七天| 39. 组合总和、40.组合总和II、131.分割回文串

题目链接:https://leetcode.cn/problems/combination-sum/description/文章链接:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html。

qq_44388663的博客 385

代码随想录算法训练营第二十七天|39. 组合总和、40.组合总和II、131.分割回文串

也是使用回溯模版,不过回溯的处理逻辑有一些不同,代码随想录中的和在遍历往path添加时就已经做了sum的操作,并且在处理完后也会回溯去做sum-=i的操作。整体使用回溯的模版去执行,核心是该题有一个去重复的过程,去重复的过程可以将结果集做成一个set,或者往里addPath的时候遍历都做一下对比。比如 [1,1,2,7],第一个1构成的组合会把第二个1构成的组合全部都包含完,因此去重的关键是树层去重,需要先进行排序。自己没想出来去重复的方法。这几题不一样的地方就是去重复的逻辑,以及切割用回溯的处理逻辑。

shanshe7934的博客 79

代码随想录算法训练营第27天|39. 组合总和|40.组合总和II|131.分割回文串

题目链接/文章讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html。题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html。注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。本题较难,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。

2401_83080774的博客 326

代码随想录算法训练营第23期day26|39. 组合总和、40.组合总和II、131.分割回文串

这道题的主要步骤可以拆分成两个部分,如何分割和如何判断。基本的回溯方法遵循回溯模板,需要注意的是回溯中的startIndex是从当前i继续而不是i+1,因为组合中的数字允许重复。在基本回溯的过程中,终止条件是当前层的sum大于target,但如果之后的数比当前数大,那么后续的递归是多余的。但是这道题还有另一个难点,那就是数组中会出现重复的元素,同一位置的元素不能重复使用,同时最终答案也不能出现重复path,这就是一个判断树层去重(而不是树枝去重)的过程,为此加入一个used数组作为辅助判断。

weixin_42179093的博客 237

代码随想录算法训练营第二十七天_第七章_回溯 | 39. 组合总和、40.组合总和II、131.分割回文串

代码随想录算法训练营2023_1_2

Jane_10358的博客 146

代码随想录算法训练营第27天|39. 组合总和 40.组合总和II 131.分割回文串

JAVA代码编写给你一个的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数target的 所有,并以列表形式返回。你可以按返回这些组合。candidates中的数字可以。如果至少一个数字的被选数量不同,则两种组合是不同的。对于给定的输入,保证和为target的不同组合数少于150个。

Catherinemin的博客 153
上一篇: 代碼隨想錄算法訓練營|第二十八天|216.组合总和III、17.电话号码的字母组合。刷题心得(c++)
下一篇: 代碼隨想錄算法訓練營|第三十天|93.复原IP地址、78.子集、90.子集II。刷题心得(c++)
RVLIN
博客等级 码龄5年 104粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值