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

  解决方法:

  工程上右键,选择buildoptions,在compilersettings里面,有列表,选择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     }

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

2019.8.20二刷,有思路但感觉流程太复杂

BONUS:

1.Python can use ";" Split the different statements in a row

2.division in python 2 can automatic change float into int,python 3 must use // instead

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn