日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。