随笔分类 -  经典算法思想

摘要:这个题目也是二分搜索的变体 阅读全文
posted @ 2020-01-07 22:20 repinkply 阅读(6) 评论(0) 推荐(0)
摘要://方法一,利用一个set集合的数据结构 //Time:O(n),Space:O(n) class Solution { public: int singleNumber(vector<int>& nums) { set<int> v; int sum=0,uniqueSum=0; for(int 阅读全文
posted @ 2020-01-07 14:18 repinkply 阅读(7) 评论(0) 推荐(0)
摘要://方法一,递归版 //Time::O(n),Space:O(1) class Solution { public: int Add(int num1, int num2) { return num2==0?num1:Add(num1^num2,(num1&num2)<<1); } }; //方法二 阅读全文
posted @ 2020-01-06 22:06 repinkply 阅读(4) 评论(0) 推荐(0)
摘要://Time:O(n),Space:O(n) //动态规划版本的解题方法,类比,求解第n个斐波那契数列 class Solution { public: //求解2个数中的最小数 int min(int a,int b) { return a<b?a:b; } //求解3个数中的最小数 int mi 阅读全文
posted @ 2020-01-06 16:35 repinkply 阅读(7) 评论(0) 推荐(0)
摘要://Time:O(m+n+l) m为多少个2,n为多少个3,l为多少个5,Space:O(1) class Solution { public: bool isUgly(int num) { if(num<=0) return false; while(num%2==0) num/=2; while 阅读全文
posted @ 2020-01-06 15:43 repinkply 阅读(3) 评论(0) 推荐(0)
摘要://Time: O(n),Space:O(1) //定义2个游标,也称快慢指针法 class Solution { public: int removeDuplicates(vector<int>& nums) { int p=1; if(nums.size()==0) return 0; for( 阅读全文
posted @ 2020-01-05 16:54 repinkply 阅读(3) 评论(0) 推荐(0)
摘要://Time:O(n),Space:O(n) //语言:C++11 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> res={-1,-1}; unordered_map< 阅读全文
posted @ 2020-01-05 12:46 repinkply 阅读(5) 评论(0) 推荐(0)
摘要://Time:(O(log(n))),Space:O(1) //此题为二分查找的变形题目,需要先学会二分查找法 class Solution { public: int binarySearchLast(vector<int>& nums,int target) { int begin=0; int 阅读全文
posted @ 2019-12-31 21:33 repinkply 阅读(4) 评论(0) 推荐(0)
摘要://解法一: class Solution { public: int firstUniqChar(string s) { int ret=-1; int a[26]={0}; for(int i=0;i<s.size();i++) { a[s[i]-'a']++; } for(int i=0;i< 阅读全文
posted @ 2019-12-25 13:24 repinkply 阅读(7) 评论(0) 推荐(0)
摘要://解法一:非递归版,最常见的一种二分查找算法 class Solution { public: int search(vector<int>& nums, int target) { int ret=-1; int begin=0; int end=nums.size()-1; while(beg 阅读全文
posted @ 2019-12-23 19:47 repinkply 阅读(8) 评论(0) 推荐(0)
摘要://解法一:递归版算法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), l 阅读全文
posted @ 2019-12-23 12:52 repinkply 阅读(6) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r 阅读全文
posted @ 2019-12-23 11:38 repinkply 阅读(7) 评论(0) 推荐(0)
摘要://中序遍历二叉树,解法一:递归版 //Time:O(n) SPace:O(n) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; 阅读全文
posted @ 2019-12-22 22:00 repinkply 阅读(4) 评论(0) 推荐(0)
摘要://解法一:非递归版本 Time:O(n),Space:O(n) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo 阅读全文
posted @ 2019-12-22 21:18 repinkply 阅读(7) 评论(0) 推荐(0)
摘要:方法一:首先我们来看看用函数怎么样实现这个题目? #include <iostream> using namespace std; int MAX(int a,int b) { int d=a-b; int flag=((unsigned int)d)>>31; //此处将d转化为 unsigned 阅读全文
posted @ 2019-10-22 18:02 repinkply 阅读(33) 评论(0) 推荐(0)
摘要:算法的空间复杂度和时间复杂度的策略: 1.多数情况下,算法的时间复杂度更加令人关注。2.因为计算机硬件的发展速度比软件的发展速度要快很多,所以如果有必要,我们可以增加额外的空间降低时间复杂度。3.同理,也可以增加算法的耗时来降低算法的空间复杂度。 我们看一个例子: 设计一个由自然数1-1000中某些 阅读全文
posted @ 2019-10-11 11:53 repinkply 阅读(21) 评论(0) 推荐(0)