2013/6/10 · smilegithub01/leetcode@e4b611e · GitHub
Skip to content

Commit e4b611e

Browse files
committed
2013/6/10
1 parent f9fb00c commit e4b611e

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

AddTwoNumbers/AddTwoNumbers.cpp

Lines changed: 60 additions & 0 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string longestPalindrome(string s) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
int beg = 0, end = 0;
7+
int longest = 1;
8+
for (int i = 0; i < s.size(); i++) {
9+
dp[i][i] = dp[i+1][i] = true;
10+
}
11+
for (int d = 1; d < s.size(); d++) {
12+
for (int i = 0, j = i + d; j < s.size(); i++, j++) {
13+
dp[i][j] = false;
14+
if (s[i] == s[j]) dp[i][j] |= dp[i+1][j-1];
15+
if (dp[i][j] && longest < j-i+1) {
16+
beg = i, end = j;
17+
}
18+
}
19+
}
20+
return s.substr(beg, end - beg + 1);
21+
}
22+
private:
23+
static const int MAXN = 1010;
24+
bool dp[MAXN][MAXN];
25+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
int hash[256];
7+
memset(hash, -1, sizeof(hash));
8+
int maxlen = 0, count = 0;
9+
for (int i = 0, j = 0; j < s.size(); j++) {
10+
if (hash[s[j]] == -1) {
11+
hash[s[j]] = j;
12+
maxlen = max(maxlen, j - i + 1);
13+
} else {
14+
while (i <= hash[s[j]]) {
15+
hash[s[i]] = -1;
16+
i += 1;
17+
}
18+
hash[s[j]] = j;
19+
}
20+
}
21+
return maxlen;
22+
}
23+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
double findMedianSortedArrays(int A[], int m, int B[], int n) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
if ((m + n) & 1)
7+
return (double)findKth(A, m, B, n, (m+n+1)/2);
8+
else
9+
return (findKth(A, m, B, n, (m+n)/2) + findKth(A, m, B, n, (m+n)/2+1)) / 2.0;
10+
11+
}
12+
13+
int findKth(int A[], int m, int B[], int n, int k) {
14+
if (m <= 0) return B[k-1];
15+
if (n <= 0) return A[k-1];
16+
if (k <= 1) return min(A[0], B[0]);
17+
18+
int ans;
19+
if (m/2 + n/2 + 1 >= k) {
20+
if (A[m/2] >= B[n/2])
21+
ans = findKth(A, m/2, B, n, k);
22+
else
23+
ans = findKth(A, m, B, n/2, k);
24+
}
25+
else {
26+
if (A[m/2] >= B[n/2])
27+
ans = findKth(A, m, B + n/2 + 1, n - n/2 - 1, k - n/2 - 1);
28+
else
29+
ans = findKth(A + m/2 + 1, m - m/2 - 1, B, n, k - m/2 - 1);
30+
}
31+
return ans;
32+
}
33+
};

TwoSum/TwoSum.cpp

Lines changed: 33 additions & 0 deletions

0 commit comments

Comments
 (0)