当前位置: 首页 > news >正文

四川建设人才培训网站外链推广网站

四川建设人才培训网站,外链推广网站,WordPress怎么加入用户关注,蚌埠网站制作哪家好文章目录1. 双指针: 只有一个输入, 从两端开始遍历2. 双指针: 有两个输入, 两个都需要遍历完3. 滑动窗口4. 构建前缀和5. 高效的字符串构建6. 链表: 快慢指针7. 反转链表8. 找到符合确切条件的子数组数9. 单调递增栈10. 二叉树: DFS (递归)]11. 二叉树: DFS (迭代)12. 二叉树: …

文章目录

    • 1. 双指针: 只有一个输入, 从两端开始遍历
    • 2. 双指针: 有两个输入, 两个都需要遍历完
    • 3. 滑动窗口
    • 4. 构建前缀和
    • 5. 高效的字符串构建
    • 6. 链表: 快慢指针
    • 7. 反转链表
    • 8. 找到符合确切条件的子数组数
    • 9. 单调递增栈
    • 10. 二叉树: DFS (递归)]
    • 11. 二叉树: DFS (迭代)
    • 12. 二叉树: BFS
    • 13. 图: DFS (递归)
    • 14. 图: DFS (迭代)
    • 15. 图: BFS
    • 16. 找到堆的前 k 个元素
    • 17. 二分查找
        • 18. 二分查找: 重复元素,最左边的插入点
        • 19. 二分查找: 重复元素,最右边的插入点
      • 20. 二分查找: 贪心问题
        • 寻找最小值:
        • 寻找最大值:
    • 21. 回溯
    • 22. 动态规划: 自顶向下法
    • 23. 构建前缀树(字典树)


1. 双指针: 只有一个输入, 从两端开始遍历

def fn(arr):left = ans = 0right = len(arr) - 1while left < right:# 一些根据 letf 和 right 相关的代码补充if CONDITION:left += 1else:right -= 1return ans
int fn(vector<int>& arr) {int left = 0;int right = int(arr.size()) - 1;int ans = 0;while (left < right) {// 一些根据 letf 和 right 相关的代码补充if (CONDITION) {left++;} else {right--;}}return ans;
}

2. 双指针: 有两个输入, 两个都需要遍历完

def fn(arr1, arr2):i = j = ans = 0while i < len(arr1) and j < len(arr2):# 根据题意补充代码if CONDITION:i += 1else:j += 1while i < len(arr1):# 根据题意补充代码i += 1while j < len(arr2):# 根据题意补充代码j += 1return ans
int fn(vector<int>& arr1, vector<int>& arr2) {int i = 0, j = 0, ans = 0;while (i < arr1.size() && j < arr2.size()) {// 根据题意补充代码if (CONDITION) {i++;} else {j++;}}while (i < arr1.size()) {// 根据题意补充代码i++;}while (j < arr2.size()) {// 根据题意补充代码j++;}return ans;
}

3. 滑动窗口

def fn(arr):left = ans = curr = 0for right in range(len(arr)):# 根据题意补充代码来将 arr[right] 添加到 currwhile WINDOW_CONDITION_BROKEN:# 从 curr 中删除 arr[left]left += 1# 更新 ansreturn ans
int fn(vector<int>& arr) {int left = 0, ans = 0, curr = 0;for (int right = 0; right < arr.size(); right++) {// 根据题意补充代码来将 arr[right] 添加到 currwhile (WINDOW_CONDITION_BROKEN) {// 从 curr 中删除 arr[left]left++;}// 更新 ans}return ans;
}

4. 构建前缀和

def fn(arr):prefix = [arr[0]]for i in range(1, len(arr)):prefix.append(prefix[-1] + arr[i])return prefix
vector<int> fn(vector<int>& arr) {vector<int> prefix(arr.size());prefix[0] = arr[0];for (int i = 1; i < arr.size(); i++) {prefix[i] = prefix[i - 1] + arr[i];}return prefix;
}

5. 高效的字符串构建

# arr 是一个字符列表
def fn(arr):ans = []for c in arr:ans.append(c)return "".join(ans)
string fn(vector<char>& arr) {return string(arr.begin(), arr.end())
}

6. 链表: 快慢指针

def fn(head):slow = headfast = headans = 0while fast and fast.next:# 根据题意补充代码slow = slow.nextfast = fast.next.nextreturn ans
int fn(ListNode* head) {ListNode* slow = head;ListNode* fast = head;int ans = 0;while (fast != nullptr && fast->next != nullptr) {// 根据题意补充代码slow = slow->next;fast = fast->next->next;}return ans;
}

7. 反转链表

def fn(head):curr = headprev = Nonewhile curr:next_node = curr.nextcurr.next = prevprev = currcurr = next_node return prev
ListNode* fn(ListNode* head) {ListNode* curr = head;ListNode* prev = nullptr;while (curr != nullptr) {ListNode* nextNode = curr->next;curr->next = prev;prev = curr;curr = nextNode;}return prev;
}

8. 找到符合确切条件的子数组数

from collections import defaultdictdef fn(arr, k):counts = defaultdict(int)counts[0] = 1ans = curr = 0for num in arr:# 根据题意补充代码来改变 currans += counts[curr - k]counts[curr] += 1return ans
int fn(vector<int>& arr, int k) {unordered_map<int, int> counts;counts[0] = 1;int ans = 0, curr = 0;for (int num: arr) {// 根据题意补充代码来改变 currans += counts[curr - k];counts[curr]++;}return ans;
}

9. 单调递增栈

def fn(arr):stack = []ans = 0for num in arr:# 对于单调递减的情况,只需将 > 翻转到 <while stack and stack[-1] > num:# 根据题意补充代码stack.pop()stack.append(num)return ans
int fn(vector<int>& arr) {stack<integer> stack;int ans = 0;for (int num: arr) {// 对于单调递减的情况,只需将 > 翻转到 <while (!stack.empty() && stack.top() > num) {// 根据题意补充代码stack.pop();}stack.push(num);}
}

10. 二叉树: DFS (递归)]

def dfs(root):if not root:returnans = 0# 根据题意补充代码dfs(root.left)dfs(root.right)return ans
int dfs(TreeNode* root) {if (root == nullptr) {return 0;}int ans = 0;// 根据题意补充代码dfs(root.left);dfs(root.right);return ans;
}

11. 二叉树: DFS (迭代)

def dfs(root):stack = [root]ans = 0while stack:node = stack.pop()# 根据题意补充代码if node.left:stack.append(node.left)if node.right:stack.append(node.right)return ans
int dfs(TreeNode* root) {stack<TreeNode*> stack;stack.push(root);int ans = 0;while (!stack.empty()) {TreeNode* node = stack.top();stack.pop();// 根据题意补充代码if (node->left != nullptr) {stack.push(node->left);}if (node->right != nullptr) {stack.push(node->right);}}return ans;
}

12. 二叉树: BFS

from collections import dequedef fn(root):queue = deque([root])ans = 0while queue:current_length = len(queue)# 做一些当前层的操作for _ in range(current_length):node = queue.popleft()# 根据题意补充代码if node.left:queue.append(node.left)if node.right:queue.append(node.right)return ans
int fn(TreeNode* root) {queue<TreeNode*> queue;queue.push(root);int ans = 0;while (!queue.empty()) {int currentLength = queue.size();// 做一些当前层的操作for (int i = 0; i < currentLength; i++) {TreeNode* node = queue.front();queue.pop();// 根据题意补充代码if (node->left != nullptr) {queue.push(node->left);}if (node->right != nullptr) {queue.push(node->right);}}}return ans;
}

13. 图: DFS (递归)

以下图模板假设节点编号从 0 到 n - 1 ,并且图是以邻接表的形式给出的。

  • 根据问题的不同,可能需要在使用模板之前将输入转换为等效的邻接表。
def fn(graph):def dfs(node):ans = 0# 根据题意补充代码for neighbor in graph[node]:if neighbor not in seen:seen.add(neighbor)ans += dfs(neighbor)return ansseen = {START_NODE}return dfs(START_NODE)
unordered_set<int> seen;int fn(vector<vector<int>>& graph) {seen.insert(START_NODE);return dfs(START_NODE, graph);
}int fn dfs(int node, vector<vector<int>>& graph) {int ans = 0;// 根据题意补充代码for (int neighbor: graph[node]) {if (seen.find(neighbor) == seen.end()) {seen.insert(neighbor);ans += dfs(neighbor, graph);}}return ans;
}

14. 图: DFS (迭代)

def fn(graph):stack = [START_NODE]seen = {START_NODE}ans = 0while stack:node = stack.pop()# 根据题意补充代码for neighbor in graph[node]:if neighbor not in seen:seen.add(neighbor)stack.append(neighbor)return ans
int fn(vector<vector<int>>& graph) {stack<int> stack;unordered_set<int> seen;stack.push(START_NODE);seen.insert(START_NODE);int ans = 0;while (!stack.empty()) {int node = stack.top();stack.pop();// 根据题意补充代码for (int neighbor: graph[node]) {if (seen.find(neighbor) == seen.end()) {seen.insert(neighbor);stack.push(neighbor);}}}
}

15. 图: BFS

from collections import dequedef fn(graph):queue = deque([START_NODE])seen = {START_NODE}ans = 0while queue:node = queue.popleft()# 根据题意补充代码for neighbor in graph[node]:if neighbor not in seen:seen.add(neighbor)queue.append(neighbor)return ans
int fn(vector<vector<int>>& graph) {queue<int> queue;unordered_set<int> seen;queue.add(START_NODE);seen.insert(START_NODE);int ans = 0;while (!queue.empty()) {int node = queue.front();queue.pop();// 根据题意补充代码for (int neighbor: graph[node]) {if (seen.find(neighbor) == seen.end()) {seen.insert(neighbor);queue.push(neighbor);}}}
}

16. 找到堆的前 k 个元素

import heapqdef fn(arr, k):heap = []for num in arr:# 做根据题意补充代码,根据问题的条件来推入堆中heapq.heappush(heap, (CRITERIA, num))if len(heap) > k:heapq.heappop(heap)return [num for num in heap]
vector<int> fn(vector<int>& arr, int k) {priority_queue<int, CRITERIA> heap;for (int num: arr) {heap.push(num);if (heap.size() > k) {heap.pop();}}vector<int> ans;while (heap.size() > 0) {ans.push_back(heap.top());heap.pop();}return ans;
}

17. 二分查找

def fn(arr, target):left = 0right = len(arr) - 1while left <= right:mid = (left + right) // 2if arr[mid] == target:# 根据题意补充代码returnif arr[mid] > target:right = mid - 1else:left = mid + 1# left 是插入点return left
int binarySearch(vector<int>& arr, int target) {int left = 0;int right = int(arr.size()) - 1;while (left <= right) {int mid = left + (right - left) / 2;if (arr[mid] == target) {// 根据题意补充代码return mid;}if (arr[mid] > target) {right = mid - 1;} else {left = mid + 1;}}// left 是插入点return left;}

18. 二分查找: 重复元素,最左边的插入点

def fn(arr, target):left = 0right = len(arr)while left < right:mid = (left + right) // 2if arr[mid] >= target:right = midelse:left = mid + 1return left
int binarySearch(vector<int>& arr, int target) {int left = 0;int right = arr.size();while (left < right) {int mid = left + (right - left) / 2;if (arr[mid] >= target) {right = mid;} else {left = mid + 1;}}return left;
}

19. 二分查找: 重复元素,最右边的插入点

def fn(arr, target):left = 0right = len(arr)while left < right:mid = (left + right) // 2if arr[mid] > target:right = midelse:left = mid + 1return left
int binarySearch(vector<int>& arr, int target) {int left = 0;int right = arr.size();while (left < right) {int mid = left + (right - left) / 2;if (arr[mid] > target) {right = mid;} else {left = mid + 1;}}return left;
}

20. 二分查找: 贪心问题

寻找最小值:

def fn(arr):def check(x):# 这个函数的具体实现取决于问题return BOOLEANleft = MINIMUM_POSSIBLE_ANSWERright = MAXIMUM_POSSIBLE_ANSWERwhile left <= right:mid = (left + right) // 2if check(mid):right = mid - 1else:left = mid + 1return left
int fn(vector<int>& arr) {int left = MINIMUM_POSSIBLE_ANSWER;int right = MAXIMUM_POSSIBLE_ANSWER;while (left <= right) {int mid = left + (right - left) / 2;if (check(mid)) {right = mid - 1;} else {left = mid + 1;}}return left;
}bool check(int x) {// 这个函数的具体实现取决于问题return BOOLEAN;
}

寻找最大值:

def fn(arr):def check(x):# 这个函数的具体实现取决于问题return BOOLEANleft = MINIMUM_POSSIBLE_ANSWERright = MAXIMUM_POSSIBLE_ANSWERwhile left <= right:mid = (left + right) // 2if check(mid):left = mid + 1else:right = mid - 1return right
int fn(vector<int>& arr) {int left = MINIMUM_POSSIBLE_ANSWER;int right = MAXIMUM_POSSIBLE_ANSWER;while (left <= right) {int mid = left + (right - left) / 2;if (check(mid)) {left = mid + 1;} else {right = mid - 1;}}return right;
}bool check(int x) {// 这个函数的具体实现取决于问题return BOOLEAN;
}

21. 回溯

def backtrack(curr, OTHER_ARGUMENTS...):if (BASE_CASE):# 修改答案returnans = 0for (ITERATE_OVER_INPUT):# 修改当前状态ans += backtrack(curr, OTHER_ARGUMENTS...)# 撤消对当前状态的修改return ans
int backtrack(STATE curr, OTHER_ARGUMENTS...) {if (BASE_CASE) {// 修改答案return 0;}int ans = 0;for (ITERATE_OVER_INPUT) {// 修改当前状态ans += backtrack(curr, OTHER_ARGUMENTS...)// 撤消对当前状态的修改}return ans;
}

22. 动态规划: 自顶向下法

def fn(arr):def dp(STATE):if BASE_CASE:return 0if STATE in memo:return memo[STATE]ans = RECURRENCE_RELATION(STATE)memo[STATE] = ansreturn ansmemo = {}return dp(STATE_FOR_WHOLE_INPUT)
unordered_map<STATE, int> memo;int fn(vector<int>& arr) {return dp(STATE_FOR_WHOLE_INPUT, arr);
}int dp(STATE, vector<int>& arr) {if (BASE_CASE) {return 0;}if (memo.find(STATE) != memo.end()) {return memo[STATE];}int ans = RECURRENCE_RELATION(STATE);memo[STATE] = ans;return ans;
}

23. 构建前缀树(字典树)

  • 注意:只有需要在每个节点上存储数据时才需要使用类。
  • 否则,您可以只使用哈希映射实现一个前缀树。
# 注意:只有需要在每个节点上存储数据时才需要使用类。
# 否则,您可以只使用哈希映射实现一个前缀树。
class TrieNode:def __init__(self):# you can store data at nodes if you wishself.data = Noneself.children = {}def fn(words):root = TrieNode()for word in words:curr = rootfor c in word:if c not in curr.children:curr.children[c] = TrieNode()curr = curr.children[c]# 这个位置上的 curr 已经有一个完整的单词# 如果你愿意,你可以在这里执行更多的操作来给 curr 添加属性return root
// 注意:只有需要在每个节点上存储数据时才需要使用类。
// 否则,您可以只使用哈希映射实现一个前缀树。
struct TrieNode {int data;unordered_map<char, TrieNode*> children;TrieNode() : data(0), children(unordered_map<char, TrieNode*>()) {}
};TrieNode* buildTrie(vector<string> words) {TrieNode* root = new TrieNode();for (string word: words) {TrieNode* curr = root;for (char c: word) {if (curr->children.find(c) == curr->children.end()) {curr->children[c] = new TrieNode();}curr = curr->children[c];// 这个位置上的 curr 已经有一个完整的单词// 如果你愿意,你可以在这里执行更多的操作来给 curr 添加属性}}return root;
}

文章转载自:
http://demoralise.Ljqd.cn
http://lias.Ljqd.cn
http://ceanothus.Ljqd.cn
http://samyama.Ljqd.cn
http://ventail.Ljqd.cn
http://pollux.Ljqd.cn
http://deprogram.Ljqd.cn
http://supermassive.Ljqd.cn
http://finecomb.Ljqd.cn
http://kiev.Ljqd.cn
http://turbellarian.Ljqd.cn
http://tetrachloroethane.Ljqd.cn
http://shower.Ljqd.cn
http://rallyist.Ljqd.cn
http://odalisque.Ljqd.cn
http://waec.Ljqd.cn
http://ligamental.Ljqd.cn
http://bourbon.Ljqd.cn
http://godless.Ljqd.cn
http://lothringen.Ljqd.cn
http://wallonian.Ljqd.cn
http://arcanum.Ljqd.cn
http://weeny.Ljqd.cn
http://city.Ljqd.cn
http://sinuate.Ljqd.cn
http://francophobe.Ljqd.cn
http://derogative.Ljqd.cn
http://pharyngoscopy.Ljqd.cn
http://sillar.Ljqd.cn
http://dethrone.Ljqd.cn
http://nonbeliever.Ljqd.cn
http://zanyism.Ljqd.cn
http://gothamite.Ljqd.cn
http://seise.Ljqd.cn
http://supersonic.Ljqd.cn
http://proclimax.Ljqd.cn
http://greenlandic.Ljqd.cn
http://jules.Ljqd.cn
http://ballsy.Ljqd.cn
http://phosphatidyl.Ljqd.cn
http://androcles.Ljqd.cn
http://shiva.Ljqd.cn
http://hebraism.Ljqd.cn
http://lardy.Ljqd.cn
http://bazaari.Ljqd.cn
http://build.Ljqd.cn
http://cosher.Ljqd.cn
http://cardsharping.Ljqd.cn
http://lampson.Ljqd.cn
http://doughtily.Ljqd.cn
http://asyndetic.Ljqd.cn
http://uptore.Ljqd.cn
http://aleksandropol.Ljqd.cn
http://decollation.Ljqd.cn
http://maleficent.Ljqd.cn
http://alleyway.Ljqd.cn
http://cuisse.Ljqd.cn
http://transmute.Ljqd.cn
http://gambit.Ljqd.cn
http://smokeable.Ljqd.cn
http://inexpugnable.Ljqd.cn
http://hindlimb.Ljqd.cn
http://slugabed.Ljqd.cn
http://profilometer.Ljqd.cn
http://oniony.Ljqd.cn
http://unconjugated.Ljqd.cn
http://wriggle.Ljqd.cn
http://thomasine.Ljqd.cn
http://virulence.Ljqd.cn
http://swop.Ljqd.cn
http://centrifugal.Ljqd.cn
http://vortex.Ljqd.cn
http://whistleable.Ljqd.cn
http://enucleate.Ljqd.cn
http://plaguy.Ljqd.cn
http://interfascicular.Ljqd.cn
http://aficionado.Ljqd.cn
http://muttonfish.Ljqd.cn
http://dreamer.Ljqd.cn
http://assembled.Ljqd.cn
http://reusage.Ljqd.cn
http://sweden.Ljqd.cn
http://evaporite.Ljqd.cn
http://month.Ljqd.cn
http://harbinger.Ljqd.cn
http://radiomicrometer.Ljqd.cn
http://dewalee.Ljqd.cn
http://surtout.Ljqd.cn
http://fatigue.Ljqd.cn
http://tartlet.Ljqd.cn
http://transporter.Ljqd.cn
http://calycine.Ljqd.cn
http://cryptological.Ljqd.cn
http://decimalism.Ljqd.cn
http://archetypal.Ljqd.cn
http://blemish.Ljqd.cn
http://symbiotic.Ljqd.cn
http://countable.Ljqd.cn
http://cheongsam.Ljqd.cn
http://megabuck.Ljqd.cn
http://www.15wanjia.com/news/63069.html

相关文章:

  • 暖通毕业设计代做网站竞价推广账户竞价托管费用
  • 做网站目的总推荐榜总点击榜总排行榜
  • 网站开发代淘宝店铺装修外贸网站有哪些
  • 网站申请内容吗如何做网络宣传推广
  • 建设工程教育网app下载厦门seo厦门起梦
  • 网站建设丿金手指下拉9东莞建设网
  • wordpress wpposts码迷seo
  • 英孚做网络作业的网站100条经典广告语
  • 展示网站如何做seo课程培训班费用
  • 政务公开微信网站开发方案书友情链接发布平台
  • 腾讯云做网站怎么样优化方案官网电子版
  • 成都网站开发拼多多关键词排名查询
  • 做网站挣钱不seo薪资
  • 开封网站建设seo优化标题
  • 网站网页怎么做小学生简短小新闻
  • 专科网站开发简历百度seo招聘
  • app网站开发费用福州百度网站快速优化
  • 如何做日系风格的网站淘宝标题优化网站
  • 优质手机网站建设企业微信营销方法
  • 承德建设厅网站万能的搜索引擎
  • 网站 平台建设情况介绍模板建站代理
  • 网站已运行时间代码免费软文推广平台都有哪些
  • 真人做的免费视频网站贵阳网站建设公司
  • 做网站要学些什么条件泉州网站建设优化
  • 中国铁建统一企业门户武汉seo首页优化报价
  • 网站上的支付接口怎么做优秀软文案例
  • 自己做的网站验证码出不来怎么回事百度推广公司
  • 大理州城乡建设局网站上海网络营销公司
  • 网站建设需要多大的空间深圳关键词优化公司哪家好
  • 岳阳网站建设制作网站怎么做的