数据结构与算法
文章平均质量分 75
RyannchenOO
这个作者很懒,什么都没留下…
展开
-
cpp 排序算法
bubbleSort/*输入n个整数,对它们进行排序,从大到小输出。0<n<=100。*/#include <iostream>using namespace std;void bubbleSort(int data[],int n);void swap(int data[],int i,int j);int main() { int n; cin >> n; int data[n]; cout<<"请输入待排序数组:"&l原创 2022-03-09 14:35:38 · 829 阅读 · 0 评论 -
算法:二分法
有时候迭代法更方便#include <iostream>#include<cmath>#include <limits.h>using namespace std;//求方程2x^3−4x^2+3x−6=0double func(double x){ double res = 2*x*x*x - 4*x*x + 3*x - 6; return res;}//用迭代法二分能求,用递归法失败!有时候要灵活选择int main(){ d.原创 2022-03-07 17:33:30 · 134 阅读 · 0 评论 -
leetcode-回溯
22.Generate Parentheses(1) Generate Parentheses - LeetCode思路:类似排列组合,画出选择树可知这是一颗二叉树从二叉树的遍历考虑:从根结点出发,往下进行二分支排列组合判断。对于当前结点,如果符合条件,则继续往下前进重复进行二分支判断,不符合条件则放弃此分支前进并回退到上一结点选择下一分支这是一个自顶向下的遍历方式→选择前序遍历抽象出每一个结点的任务:①做选择:left或right②试探选择是否符合条件==继续遍历...原创 2022-02-11 21:30:50 · 6802 阅读 · 0 评论 -
leetcode-DP,动态规划问题
经典递归问题509. Fibonacci Number(1) Fibonacci Number - LeetCode int fib(int n) { if(n==0 || n==1) return n; return fib(n-1)+fib(n-2); }time complexity 为O(2^n)根据递归树可见,这是一个自顶向下的递归树,本递归存在大量重复递归步骤如何避免不必要的重复?——使用记录器int fib(int n)原创 2022-02-10 21:36:20 · 356 阅读 · 0 评论 -
leetcode-二分查找
704.Binary Search(1) Binary Search - LeetCode迭代方式 int search(vector<int>& nums, int target) { int lo = 0; int hi = nums.size()-1; while(lo<=hi){ int mid = (lo+hi)/2; if(nums[mid] .原创 2022-02-07 12:29:19 · 723 阅读 · 1 评论 -
leetcode-数组
数组求和问题303. Range Sum Query - Immutable(1) Range Sum Query - Immutable - LeetCode初始思路是直接计算累加和class NumArray {public: NumArray(vector<int>& nums) { for(int elem:nums) arr.push_back(elem); } in原创 2022-02-04 21:58:15 · 6716 阅读 · 0 评论 -
链表-leetcode
(一)使用快慢指针(二)相遇的角度思考141.Linked List CycleLinked List Cycle - LeetCode相遇则且不为NULL则说明存在环 bool hasCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while(fast && slow){ slow = slow->.原创 2022-01-26 21:53:34 · 664 阅读 · 0 评论 -
二叉树-leetcode
104. Maximum Depth of Binary Tree(14) Maximum Depth of Binary Tree - LeetCode递归思路:DFS分成左右子树两部分递归(分而治之)——后序遍历 int maxDepth(TreeNode* root) { if(root==NULL) return 0; //递归基 int hleft = maxDepth(root->left);//递归函数帮忙求左子树高度原创 2022-01-14 20:55:28 · 237 阅读 · 0 评论