标签:while ble substring oat tin character ora length white
Longest Substring Without Repeating Characters
找无相同字符的最长子串,o(n)暴力判断即可
class Solution { public: int a[300]; int lengthOfLongestSubstring(string s) { int len=s.length(); if(len==0) return 0; memset(a,0,sizeof(a)); int ans=1; int sum=1; a[s[0]]=1; for(int i=1;i<len;i++) { int shit=s[i]; if(sum+1>i-a[s[i]]+1) sum=i-a[s[i]]+1; else sum++; // cout<<sum<<" "<<i-a[s[i]]+1<<endl; if(sum>ans) ans=sum; a[shit]=i+1; } return ans; } };
16. 3Sum Closest
对于给定整数,在给定数组中寻找三个数的和,使得这个和最接近这个整数
显然二分即可
class Solution { public: int mx= 0x7fffffff; int bs(vector<int>& a,int left,int right,int tar) { int x=left,y=right; while(left<=right) { int mid=(left+right)>>1; if(a[mid]>tar) right=mid-1; else left= mid+1; } //cout<<left-1; int shit,fuck; shit=fuck=mx; if(left-1>=x) shit=tar-a[left-1]; if(left<=y) fuck=a[left]-tar; if(shit<fuck) return left-1; else return left; } int fb(int x) { if(x>0) return x; return -x; } int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(),nums.end()); int len=nums.size(); int ans=mx; int x=0; for(int i=0;i<len;i++) x+=nums[i]; for(int i=0;i<len;i++) { int sum=nums[i]; for(int j=i+1;j<len-1;j++) { sum+=nums[j]; int shit=bs(nums,j+1,len-1,target-sum); shit=nums[shit]; // cout<<ans<<endl; if(ans>fb(target-shit-sum)) { ans=fb(target-shit-sum); x=shit+sum; } sum-=nums[j]; } } return x; } };
14. Longest Common Prefix
找一系列字符串的公共子串,直接暴力
class Solution { public: string longestCommonPrefix(vector<string>& strs) { int len=strs.size(); if(len==0) { string ans=""; return ans; } string ans=strs[0]; int j; for(j=0;j<ans.length();j++) { int i; for(i=1;i<len;i++) if(ans[j]!=strs[i][j]) break; if(i<len) { break; } } ans=ans.substr(0,j); return ans; } };
标签:while ble substring oat tin character ora length white
原文地址:http://www.cnblogs.com/bitch1319453/p/6661463.html