正文
增加 15 行删除 15 行
12345169 行未改动全部展开17517617717817918018118218327 行未改动21121221321421521621721821926 行未改动24624724824925025125225325451 行未改动30630730830931031131231331444 行未改动3593603613623633643653663673683693703713723733744 行未改动37938038138238338438538638727 行未改动41541641741841942042142242349 行未改动47347447547647747847948048148248348448548648748848939 行未改动52953053153253353453553653774 行未改动612613614615616617618619620215 行未改动全部展开### 一、引言:反向遍历的基本思想在计算机科学中,反向遍历(Reverse Traversal)是一种重要的算法思想,通过从后向前或从右到左的顺序处理数据,能够有效避免重复方案或简化问题复杂度。与传统的正向遍历相比,反向遍历通过改变处理顺序,能够在某些场景下更高效地解决问题,特别是在需要避免重复计算、去重或处理依赖关系的问题中表现出色(34)。169 行未改动全部展开}void backtrack(int[] nums, int startIndex, List<Integer> path, List<List<Integer>> result) {result.add(new ArrayList<>(path)); // 收集当前子集for (int i = startIndex; i < nums.length; i++) {//去重条件:如果当前元素与前一个元素相同,且前一个元素未被选取(即未进入递归),则跳过if (i > startIndex && nums[i] == nums[i-1]) {continue;27 行未改动result.add(new ArrayList<>(path));return;}for (int i = 0; i < nums.length; i++) {//去重条件:如果当前元素已被使用,或者与前一个元素相同且前一个元素未被使用过,则跳过if (used[i] || (i > 0 && nums[i] == nums[i-1] && !used[i-1])) {continue;26 行未改动result.add(new ArrayList<>(path));}Set<Integer> used = new HashSet<>(); // 本层去重for (int i = startIndex; i < nums.length; i++) {//如果当前元素已被使用,或者比前一个元素小,则跳过if (used.contains(nums[i]) || (!path.isEmpty() && nums[i] < path.get(path.size()-1))) {continue;}51 行未改动last_occurrence = {char: i for i, char in enumerate(s)}for i, char in enumerate(s):if in_stack[ord(char) - ord('a')]:continue#如果当前字符比栈顶字符小,且栈顶字符在后面还会出现,则弹出栈顶字符while stack and char < stack[-1] and i < last_occurrence[stack[-1]]:popped_char = stack.pop()in_stack[ord(popped_char) - ord('a')] = False44 行未改动visited[exit_x][exit_y] = True# 反向BFSwhile queue:x, y = queue.popleft()#检查当前位置的四个方向for dx, dy in directions:nx, ny = x + dx, y + dy#检查是否在迷宫范围内if 0 <= nx < N and 0 <= ny < M:#如果当前位置是传送器,需要根据类型处理current_char = data[nx][ny]if current_char == 'U':nx_new, ny_new = nx - 1, ny4 行未改动elif current_char == 'R':nx_new, ny_new = nx, ny + 1else:nx_new, ny_new = nx, ny#检查传送后的位置是否在迷宫范围内if 0 <= nx_new < N and 0 <= ny_new < M:if not visited[nx_new][ny_new]:visited[nx_new][ny_new] = True27 行未改动if (current == start) {reverse(path.begin(), path.end()); // 反转路径,得到从start到end的顺序return true;}//反向遍历所有邻接节点for (Node neighbor : current.neighbors) {if (!visited[neighbor.id]) {visited[neighbor.id] = true;49 行未改动// 埃及分数的反向贪心算法vector<int> reverseGreedy(int numerator, int denominator) {vector<int> fractions;while (numerator > 0) {//计算最大的单位分数1/k <= numerator/denominatorint k = denominator / numerator + 1;fractions.add(k);//计算剩余部分int new_numerator = numerator * k - denominator;int new_denominator = denominator * k;//约分int gcd = __gcd(new_numerator, new_denominator);numerator = new_numerator / gcd;denominator = new_denominator / gcd;39 行未改动。反向遍历作为一种重要的算法思想,不仅可以帮助我们解决特定的问题,还可以提供新的问题视角和解决方案。通过深入理解和灵活应用反向遍历,我们可以设计出更高效、更简洁的算法,解决更多复杂的问题。参考资料[1] 背包双面镜:从“一和零“的多维迷宫到“组合总和“的无限维度-CSDN博客 https://blog.csdn.net/m0_65465065/article/details/148797214[2] 掌握滑动窗口本质,秒杀相关题型 介绍了滑动窗口的本质和例题练习。-抖音 https://www.iesdouyin.com/share/video/7500044415919082793/?did=MS4wLjABAAAANwkJuWIRFOzg574 行未改动mid=7536132033945701183®ion=&scene_from=dy_open_search_video&share_sign=Q_EhAcG4YoKXc6Q7LUa8nummfnr0DNZQkFwe_c_bRjQ-&share_version=280700&titleType=title&ts=1755249202&u_code=0&video_share_track_ver=&with_sec_did=1[17] 不是我催...以后大家学动态规划,都得看这个视频 《算法思想》试看章节《动态规划》从本质上带你了解状态、状态转移、无后效性、复杂度、状态转移方程以及动态规划的四种写法《算法思想》介绍:原理可视化:以可视化的方式讲解算法原理标准模版:给出可线上、线下使用的简洁、高效、优雅的算法模版模型抽象:讲解算法所对应的问题模型215 行未改动全部展开12345169 行未改动全部展开17517617717817918018118218327 行未改动21121221321421521621721821926 行未改动24624724824925025125225325451 行未改动30630730830931031131231331444 行未改动3593603613623633643653663673683693703713723733744 行未改动37938038138238338438538638727 行未改动41541641741841942042142242349 行未改动47347447547647747847948048148248348448548648748848939 行未改动52953053153253353453553653774 行未改动612613614615616617618619620215 行未改动全部展开### 一、引言:反向遍历的基本思想在计算机科学中,反向遍历(Reverse Traversal)是一种重要的算法思想,通过从后向前或从右到左的顺序处理数据,能够有效避免重复方案或简化问题复杂度。与传统的正向遍历相比,反向遍历通过改变处理顺序,能够在某些场景下更高效地解决问题,特别是在需要避免重复计算、去重或处理依赖关系的问题中表现出色(34)。169 行未改动全部展开}void backtrack(int[] nums, int startIndex, List<Integer> path, List<List<Integer>> result) {result.add(new ArrayList<>(path)); // 收集当前子集for (int i = startIndex; i < nums.length; i++) {//去重条件:如果当前元素与前一个元素相同,且前一个元素未被选取(即未进入递归),则跳过if (i > startIndex && nums[i] == nums[i-1]) {continue;27 行未改动result.add(new ArrayList<>(path));return;}for (int i = 0; i < nums.length; i++) {//去重条件:如果当前元素已被使用,或者与前一个元素相同且前一个元素未被使用过,则跳过if (used[i] || (i > 0 && nums[i] == nums[i-1] && !used[i-1])) {continue;26 行未改动result.add(new ArrayList<>(path));}Set<Integer> used = new HashSet<>(); // 本层去重for (int i = startIndex; i < nums.length; i++) {//如果当前元素已被使用,或者比前一个元素小,则跳过if (used.contains(nums[i]) || (!path.isEmpty() && nums[i] < path.get(path.size()-1))) {continue;}51 行未改动last_occurrence = {char: i for i, char in enumerate(s)}for i, char in enumerate(s):if in_stack[ord(char) - ord('a')]:continue#如果当前字符比栈顶字符小,且栈顶字符在后面还会出现,则弹出栈顶字符while stack and char < stack[-1] and i < last_occurrence[stack[-1]]:popped_char = stack.pop()in_stack[ord(popped_char) - ord('a')] = False44 行未改动visited[exit_x][exit_y] = True# 反向BFSwhile queue:x, y = queue.popleft()#检查当前位置的四个方向for dx, dy in directions:nx, ny = x + dx, y + dy#检查是否在迷宫范围内if 0 <= nx < N and 0 <= ny < M:#如果当前位置是传送器,需要根据类型处理current_char = data[nx][ny]if current_char == 'U':nx_new, ny_new = nx - 1, ny4 行未改动elif current_char == 'R':nx_new, ny_new = nx, ny + 1else:nx_new, ny_new = nx, ny#检查传送后的位置是否在迷宫范围内if 0 <= nx_new < N and 0 <= ny_new < M:if not visited[nx_new][ny_new]:visited[nx_new][ny_new] = True27 行未改动if (current == start) {reverse(path.begin(), path.end()); // 反转路径,得到从start到end的顺序return true;}//反向遍历所有邻接节点for (Node neighbor : current.neighbors) {if (!visited[neighbor.id]) {visited[neighbor.id] = true;49 行未改动// 埃及分数的反向贪心算法vector<int> reverseGreedy(int numerator, int denominator) {vector<int> fractions;while (numerator > 0) {//计算最大的单位分数1/k <= numerator/denominatorint k = denominator / numerator + 1;fractions.add(k);//计算剩余部分int new_numerator = numerator * k - denominator;int new_denominator = denominator * k;//约分int gcd = __gcd(new_numerator, new_denominator);numerator = new_numerator / gcd;denominator = new_denominator / gcd;39 行未改动。反向遍历作为一种重要的算法思想,不仅可以帮助我们解决特定的问题,还可以提供新的问题视角和解决方案。通过深入理解和灵活应用反向遍历,我们可以设计出更高效、更简洁的算法,解决更多复杂的问题。参考资料[1] 背包双面镜:从“一和零“的多维迷宫到“组合总和“的无限维度-CSDN博客 https://blog.csdn.net/m0_65465065/article/details/148797214[2] 掌握滑动窗口本质,秒杀相关题型 介绍了滑动窗口的本质和例题练习。-抖音 https://www.iesdouyin.com/share/video/7500044415919082793/?did=MS4wLjABAAAANwkJuWIRFOzg574 行未改动mid=7536132033945701183®ion=&scene_from=dy_open_search_video&share_sign=Q_EhAcG4YoKXc6Q7LUa8nummfnr0DNZQkFwe_c_bRjQ-&share_version=280700&titleType=title&ts=1755249202&u_code=0&video_share_track_ver=&with_sec_did=1[17] 不是我催...以后大家学动态规划,都得看这个视频 《算法思想》试看章节《动态规划》从本质上带你了解状态、状态转移、无后效性、复杂度、状态转移方程以及动态规划的四种写法《算法思想》介绍:原理可视化:以可视化的方式讲解算法原理标准模版:给出可线上、线下使用的简洁、高效、优雅的算法模版模型抽象:讲解算法所对应的问题模型215 行未改动全部展开