LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...

混淆点:

  子串 连续

  子序列 可以不连续

知识点:

  HashMap:

出现问题:

  1.使用unordered_map头文件时报错

#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.#end if

 

  解决方法:

  工程上右键,选择build options,在compiler settings里面,有列表,选择c++0x支持。

  PS:这个错误比较诡异的就是,他直接跳到了头文件里。所以可以知道这不是自己的错误,在工程里更改下编译方式就好了。  

  解决代码:

 1 //Leetcode #3
2 //题目描述:给定一个字符串求最大子串个数 5 6 #include<stdio.h> 7 #include<iostream> 8 #include<vector> 9 #include<unordered_map> 10 using namespace std; 11 12 13 class Solution { 14 public: 15 int lengthOfLongestSubstring(string s) 16 { 17 int res=0,left=-1,n=s.size(); 18 unordered_map<int,int> m; 19 for(int i=0;i<n;++i) 20 { 21 //count check whether s[i] exist 22 if(m.count(s[i])&&m[s[i]]>left) 23 { 24 left=m[s[i]]; 25 26 } 27 //update s[i]'s position 28 m[s[i]]=i; 29 res=max(res,i-left); 30 31 } 32 return res; 33 } 34 }; 35 36 37 38 39 40 41 int main() 42 { 43 Solution s;//initializing object s 44 string str1="abttybacds"; 45 int t=0; 46 t=s.lengthOfLongestSubstring(str1); 47 cout<<t<<endl; 48 return 0; 49 }

 https://www.cnblogs.com/grandyang/p/4480780.html

这个里面还有其余几种方法的讲解

4.Median of Two Sorted Arrays两个有序数组的中位数 Hard

 这道题是用分治法做来保证时间限制

1 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
2         int k=nums1.size()+nums2.size();
3         if(k%2==0)
4             return (median(nums1,nums2,k/2)+median(nums1,nums2,k/2+1))/2.0;
5         else return median(nums1,nums2,k/2+1);
6     }
 1 double median(vector<int>& vec1,vector<int>& vec2,int k){
 2         if(vec1.size()>vec2.size()) return median(vec2,vec1,k);
 3         if(vec1.empty()) return vec2[k-1];
 4         if(k==1) return min(vec1[0],vec2[0]);
 5         int p1=vec1.size()>k/2?k/2:vec1.size();
 6         int p2=k-p1;
 7         if(vec1[p1-1]>vec2[p2-1]){
 8             vector<int> vec(vec2.begin()+p2,vec2.end());
 9             return median(vec1,vec,k-p2);
10         }
11        AS else if(vec1[p1-1]<vec2[p2-1]){
12             vector<int> vec(vec1.begin()+p1,vec1.end());
13             return median(vec,vec2,k-p1);
14         }
15         else return vec1[p1-1];
16     }

思路还是有点不懂,先把来源贴上,明天再研究研究

转载于:https://www.cnblogs.com/Marigolci/p/10987999.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值