摘要:

1、动态规划(128ms,89%;89MB,47%) 时间复杂度:O(n):n为数组元素个数 空间复杂度:O(1) 1 class Solution { 2 public: 3 long theMax(long a,long b,long c){ 4 return a>b? (a>c? a:c):(
阅读全文
posted @ 2021-12-16 23:17
夏天吃辣椒
阅读(73)
推荐(0)
摘要:

1、双循环(超时) 时间复杂度:O(n^2):n为数组元素个数 空间复杂度:O(1) 1 int maxSubArray(vector<int>& nums) { 2 int maxnum=nums[0]; 3 int sum=0; 4 for(int i=0;i<nums.size();i++){
阅读全文
posted @ 2021-12-15 22:25
夏天吃辣椒
阅读(27)
推荐(0)
摘要:

1、二分法(124ms,95%;45.2MB,24%) 时间复杂度:O(nlog m):m、n分别是矩阵的行数、列数 空间复杂度:O(1) 1 //max_element(r,r+6)返回数组前6个元素中的最大值的地址 2 //取值则为*max_element(r,r+6) 3 //int(返回的地
阅读全文
posted @ 2021-12-03 22:12
夏天吃辣椒
阅读(45)
推荐(0)
摘要:

1、直接循环(超出时间限制) 时间复杂度:O(mn):m、n分别为矩阵的行数、列数,mn即为矩阵元素个数 空间复杂度:O(1) bool searchMatrix(vector<vector<int>>& matrix, int target) { if(matrix.empty()) return
阅读全文
posted @ 2021-12-03 20:21
夏天吃辣椒
阅读(68)
推荐(0)
摘要:

1、直接循环(4ms,76%;9.3MB,31%) 时间复杂度:O(n):最坏情况要遍历整个数组 空间复杂度:O(1) bool searchMatrix(vector<vector<int>>& matrix, int target) { if(matrix.empty()) return fal
阅读全文
posted @ 2021-12-03 19:40
夏天吃辣椒
阅读(32)
推荐(0)
摘要:

1、直接循环(4ms,90%;13.5MB,85%) 时间复杂度:O(n):循环次数 空间复杂度:O(1) 1 bool search(vector<int>& nums, int target) { 2 if(nums.empty()) 3 return false; 4 int n=0; 5 w
阅读全文
posted @ 2021-12-01 22:32
夏天吃辣椒
阅读(20)
推荐(0)
摘要:

1、二分法(4ms,69%;10.8MB,70%) 时间复杂度:O(log n) 空间复杂度:O(1) 1 int search(vector<int>& nums, int target) { 2 //这里的empty()注意是空则返回真 3 if(nums.empty()) 4 return -
阅读全文
posted @ 2021-12-01 21:45
夏天吃辣椒
阅读(32)
推荐(0)
摘要:

1、直接循环求(超时) 1 double myPow(double x, int n) { 2 if(n==0||x==1) 3 return 1; 4 double temp=x; 5 if(n<0){ 6 x=1/x; 7 temp=x; 8 n*=-1; 9 } 10 while(--n>0)
阅读全文
posted @ 2021-12-01 19:27
夏天吃辣椒
阅读(49)
推荐(0)
摘要:

1、转成数组处理(208ms,33%;125MB,14%) 时间复杂度O(n):复制处是O(n),判断处是O(n/2)即O(n),相加即为O(n) 空间复杂度O(n):复制处用了大小为n的数组存链表数据,即O(n) 1 bool isPalindrome(ListNode* head) { 2 //
阅读全文
posted @ 2021-11-28 09:19
夏天吃辣椒
阅读(35)
推荐(0)
摘要:

1、递归(4ms,93%;14.4MB,76%) 1 //递归需关注目前这一层的数据如何处理,以及如何结束递归 2 //即该层数据以什么状态传给下一层,以及数据以什么状态返回上一层 3 ListNode* mergeTwoLists(ListNode* list1, ListNode* list2)
阅读全文
posted @ 2021-11-27 19:46
夏天吃辣椒
阅读(32)
推荐(0)