Skip to content

Commit 21f262e

Browse files
committed
first commit
0 parents  commit 21f262e

File tree

764 files changed

+27539
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

764 files changed

+27539
-0
lines changed

0001.two-sum/two-sum.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int,int> um;
5+
for(int i=0;i<nums.size();i++){
6+
if(um.find(target-nums[i])!=um.end()){
7+
vector<int> res{i,um[target-nums[i]]};
8+
return res;
9+
}
10+
um[nums[i]]=i;
11+
}
12+
return vector<int>();
13+
}
14+
};

0001.two-sum/two-sum.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
2+
3+
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
4+
5+
<p><strong>Example:</strong></p>
6+
7+
<pre>
8+
Given nums = [2, 7, 11, 15], target = 9,
9+
10+
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
11+
return [<strong>0</strong>, <strong>1</strong>].
12+
</pre>
13+
14+
<p>&nbsp;</p>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p>You are given two <b>non-empty</b> linked lists representing two non-negative integers. The digits are stored in <b>reverse order</b> and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.</p>
2+
3+
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>
4+
5+
<p><b>Example:</b></p>
6+
7+
<pre>
8+
<b>Input:</b> (2 -&gt; 4 -&gt; 3) + (5 -&gt; 6 -&gt; 4)
9+
<b>Output:</b> 7 -&gt; 0 -&gt; 8
10+
<b>Explanation:</b> 342 + 465 = 807.
11+
</pre>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class Solution {
2+
public:
3+
// 自己做出来了
4+
int lengthOfLongestSubstring(string s) {
5+
if(s.size()==0) return 0;
6+
int i=0,j=0;
7+
unordered_map<char,int> um;
8+
int res=1;
9+
while(j<s.size()){
10+
um[s[j]]++;
11+
while(um[s[j]]>1){
12+
um[s[i++]]--;
13+
}
14+
res=max(res,j-i+1);
15+
j++;
16+
}
17+
return res;
18+
}
19+
20+
// 模板题:双指针+map
21+
// https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems
22+
int lengthOfLongestSubstring3(string s) {
23+
if(s.size()==0) return 0;
24+
unordered_map<char,int> um;
25+
int i=0;
26+
int res=1;
27+
for(int j=0;j<s.size();j++){
28+
um[s[j]]++;
29+
while(um[s[j]]>1){
30+
um[s[i++]]--;
31+
}
32+
res=max(res,j-i+1);
33+
}
34+
return res;
35+
}
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
// 二刷,自己做出来了,还是map靠谱啊
57+
int lengthOfLongestSubstring2(string s) {
58+
if(s.size()==0) return 0;
59+
unordered_map<char,int> um;
60+
int res=1;
61+
for(int i=0,j=0;j<s.size();j++){
62+
um[s[j]]++;
63+
while(um[s[j]]>1 && i<j){ // 以j为尾部,能拿到的最长不重复字串长度
64+
um[s[i]]--;
65+
i++;
66+
}
67+
res=max(res,j-i+1);
68+
69+
}
70+
return res;
71+
}
72+
73+
74+
75+
76+
77+
//https://www.acwing.com/solution/LeetCode/content/49/
78+
//思路巧妙,字符串的问题以后要多想想双指针。
79+
// 这个for循环的结构也可以学习一下!
80+
unordered_map<char,int> um;
81+
int lengthOfLongestSubstring1(string s) {
82+
if(s.size()==0) return 0;
83+
int res=INT_MIN;
84+
for(int i=0,j=0;j<s.size();j++){
85+
char &c=s[j];
86+
um[c]+=1;
87+
while(um[c]>=2 && i<j){
88+
um[s[i++]]--;
89+
}
90+
res=max(res,j-i+1);
91+
}
92+
return res;
93+
}
94+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<p>Given a string, find the length of the <b>longest substring</b> without repeating characters.</p>
2+
3+
<div>
4+
<p><strong>Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input: </strong><span id="example-input-1-1">&quot;abcabcbb&quot;</span>
8+
<strong>Output: </strong><span id="example-output-1">3
9+
<strong>Explanation:</strong></span> The answer is <code>&quot;abc&quot;</code>, with the length of 3.
10+
</pre>
11+
12+
<div>
13+
<p><strong>Example 2:</strong></p>
14+
15+
<pre>
16+
<strong>Input: </strong><span id="example-input-2-1">&quot;bbbbb&quot;</span>
17+
<strong>Output: </strong><span id="example-output-2">1
18+
</span><span id="example-output-1"><strong>Explanation: </strong>T</span>he answer is <code>&quot;b&quot;</code>, with the length of 1.
19+
</pre>
20+
21+
<div>
22+
<p><strong>Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input: </strong><span id="example-input-3-1">&quot;pwwkew&quot;</span>
26+
<strong>Output: </strong><span id="example-output-3">3
27+
</span><span id="example-output-1"><strong>Explanation: </strong></span>The answer is <code>&quot;wke&quot;</code>, with the length of 3.
28+
Note that the answer must be a <b>substring</b>, <code>&quot;pwke&quot;</code> is a <i>subsequence</i> and not a substring.
29+
</pre>
30+
</div>
31+
</div>
32+
</div>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Solution {
2+
public:
3+
4+
// 三刷,这道题目感觉挺难的,没有做出来
5+
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
6+
int m=nums1.size(),n=nums2.size();
7+
int l=(m+n+1)/2, r=(m+n+2)/2;
8+
return (bh(nums1,nums2,0,0,l)+bh(nums1,nums2,0,0,r))/2.0;
9+
}
10+
11+
double bh(vector<int>& nums1, vector<int>& nums2, int i, int j, int k){
12+
if(i>=nums1.size()) return nums2[j+k-1];
13+
if(j>=nums2.size()) return nums1[i+k-1];
14+
if(k==1) return min(nums1[i],nums2[j]);// 否则下文中 k/2-1 可能小于0
15+
16+
int n1=INT_MAX,n2=INT_MAX;
17+
if(i+k/2-1<nums1.size()) n1=nums1[i+k/2-1];
18+
if(j+k/2-1<nums2.size()) n2=nums2[j+k/2-1];
19+
20+
if(n1<n2) return bh(nums1,nums2,i+k/2,j,k-k/2);
21+
else return bh(nums1,nums2,i,j+k/2,k-k/2);
22+
}
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
// 二刷,自己又没有做出来。看的是一刷的答案。
35+
double findMedianSortedArrays2(vector<int>& nums1, vector<int>& nums2) {
36+
// sort(nums1.begin(),nums1.end());
37+
// sort(nums2.begin(),nums2.end());
38+
int m=nums1.size();
39+
int n=nums2.size();
40+
//中位数是(nums[left]+nums[right])/2,这样寻找中位数就转化为在两个数组中找第K个数字了。
41+
int left=(m+n+1)/2;
42+
int right=(m+n+2)/2;
43+
// cout<<left<<" "<<right<<endl;
44+
// cout<<helper(nums1,nums2,0,0,left)<<" "<<helper(nums1,nums2,0,0,right)<<endl;
45+
return (helper(nums1,nums2,0,0,left)+helper(nums1,nums2,0,0,right))/2.0;
46+
}
47+
48+
//i标记的是nums1的起始位置
49+
//j标记的是nums2的起始位置
50+
//k是要在nums1和nums2中找到第k个数
51+
int helper(vector<int>& nums1, vector<int>& nums2, int i, int j, int k){
52+
//各种边界条件
53+
if(i>=nums1.size()) return nums2[j+k-1];
54+
if(j>=nums2.size()) return nums1[i+k-1];
55+
if(k==1) return min(nums1[i],nums2[j]);
56+
57+
// 使用二分法优化。每次砍掉k/2个数的搜索空间
58+
// 分别在nums1和nums2中找到第k/2个数,如果tmp1<tmp2,说明第nums1中前k/2个数都是无效的,反之亦然。
59+
// 如果nums1中剩余的数量都不足k/2了,而我们每次都可以砍掉k/2个数字,则我们此次砍掉的是nums2中前k/2个数字,所以赋值为tmp1为max。
60+
int tmp1=(i+k/2-1<nums1.size())?nums1[i+k/2-1]:INT_MAX;
61+
int tmp2=(j+k/2-1<nums2.size())?nums2[j+k/2-1]:INT_MAX;
62+
if(tmp1<tmp2) return helper(nums1,nums2,i+k/2,j,k-k/2);
63+
else return helper(nums1,nums2,i,j+k/2,k-k/2);
64+
}
65+
66+
67+
//没做出来,参照了http://www.cnblogs.com/grandyang/p/4465932.html。
68+
double findMedianSortedArrays1(vector<int>& nums1, vector<int>& nums2) {
69+
int m=nums1.size();
70+
int n=nums2.size();
71+
int left=(m+n+1)/2;
72+
int right=(m+n+2)/2;
73+
return (findKthNum(nums1,nums2,0,0,left)+findKthNum(nums1,nums2,0,0,right))/2.0;
74+
}
75+
76+
//在两个数组中找第k个元素。使用二分法,递归。
77+
int findKthNum(vector<int>& nums1, vector<int>& nums2, int i,int j, int k){
78+
if(i>=nums1.size()) return nums2[j+k-1];
79+
if(j>=nums2.size()) return nums1[i+k-1];
80+
if(k==1) return min(nums1[i],nums2[j]); //因为下文有k/2,所以对于k==1要特殊处理。
81+
int tmp1=(i+k/2-1<nums1.size())?nums1[i+k/2-1]:INT_MAX;
82+
int tmp2=(j+k/2-1<nums2.size())?nums2[j+k/2-1]:INT_MAX;
83+
if(tmp1<tmp2) return findKthNum(nums1,nums2,i+k/2,j,k-k/2);
84+
if(tmp1>=tmp2) return findKthNum(nums1,nums2,i,j+k/2,k-k/2);
85+
}
86+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>There are two sorted arrays <b>nums1</b> and <b>nums2</b> of size m and n respectively.</p>
2+
3+
<p>Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).</p>
4+
5+
<p>You may assume <strong>nums1</strong> and <strong>nums2</strong>&nbsp;cannot be both empty.</p>
6+
7+
<p><b>Example 1:</b></p>
8+
9+
<pre>
10+
nums1 = [1, 3]
11+
nums2 = [2]
12+
13+
The median is 2.0
14+
</pre>
15+
16+
<p><b>Example 2:</b></p>
17+
18+
<pre>
19+
nums1 = [1, 2]
20+
nums2 = [3, 4]
21+
22+
The median is (2 + 3)/2 = 2.5
23+
</pre>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Solution {
2+
public:
3+
// 三刷,自己做出来了
4+
string longestPalindrome(string s) {
5+
if(s.size()==0) return "";
6+
int res=1;
7+
int start=0;
8+
int len=s.size();
9+
vector<vector<bool>> dp(len,vector<bool>(len,false));
10+
for(int k=1;k<=len;k++){ // 需要注意最外层循环是长度
11+
for(int i=0;i+k-1<len;i++){
12+
int j=i+k-1;
13+
if(j-i<1) dp[i][j]=true;
14+
else if(j-i==1 && s[i]==s[j]) dp[i][j]=true;
15+
else if(j-i>1 && dp[i+1][j-1] && s[i]==s[j]) dp[i][j]=true;
16+
if(dp[i][j] && j-i+1>res){
17+
res=j-i+1;
18+
start=i;
19+
}
20+
}
21+
}
22+
return s.substr(start,res);
23+
}
24+
25+
26+
// 二刷,建议记这种,实现起来比较简单。但是运行慢
27+
int res=1;
28+
string tmp;
29+
vector<vector<bool>> dp;
30+
string longestPalindrome2(string s) {
31+
if(s.size()==0) return s;
32+
tmp=s[0];
33+
int len=s.size();
34+
dp.resize(len+1,vector<bool>(len+1,0));
35+
dp[0][0]=1;
36+
37+
for(int i=1;i<=len;i++){
38+
for(int j=i;j>=1;j--){
39+
if(i-j<=1)
40+
dp[j][i]=(s[i-1]==s[j-1]);
41+
else{
42+
dp[j][i]=(dp[j+1][i-1] && s[i-1]==s[j-1]);
43+
}
44+
if(dp[j][i]){
45+
if(i-j+1>res){
46+
res=(i-j+1);
47+
tmp=s.substr(j-1,res); // 注意j的下标
48+
}
49+
}
50+
}
51+
}
52+
return tmp;
53+
}
54+
55+
56+
57+
//https://www.acwing.com/solution/LeetCode/content/51/
58+
string longestPalindrome1(string s) {
59+
if(s.size()==0) return s;
60+
int len=0;
61+
string res;
62+
for(int i=0;i<s.size();i++){
63+
for(int j=0;i-j>=0 && i+j<s.size();j++){ //回文串长度为奇数
64+
if(s[i-j]==s[i+j]){
65+
if(1+2*j>len){
66+
len=1+2*j;
67+
res=s.substr(i-j,1+2*j);
68+
}
69+
}
70+
else break;
71+
}
72+
73+
for(int j=i,k=i+1;j>=0 && k<s.size();j--,k++){ //回文串长度为偶数
74+
if(s[j]==s[k]){
75+
if(k-j+1>len){
76+
len=k-j+1;
77+
res=s.substr(j,k-j+1);
78+
}
79+
}
80+
else break;
81+
}
82+
}
83+
cout<<len<<endl;
84+
return res;
85+
}
86+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<p>Given a string <strong>s</strong>, find the longest palindromic substring in <strong>s</strong>. You may assume that the maximum length of <strong>s</strong> is 1000.</p>
2+
3+
<p><strong>Example 1:</strong></p>
4+
5+
<pre>
6+
<strong>Input:</strong> &quot;babad&quot;
7+
<strong>Output:</strong> &quot;bab&quot;
8+
<strong>Note:</strong> &quot;aba&quot; is also a valid answer.
9+
</pre>
10+
11+
<p><strong>Example 2:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> &quot;cbbd&quot;
15+
<strong>Output:</strong> &quot;bb&quot;
16+
</pre>

0 commit comments

Comments
 (0)