Skip to content

Commit

Permalink
04112013
Browse files Browse the repository at this point in the history
  • Loading branch information
zhugaoping committed Apr 11, 2013
1 parent 56e7e67 commit 302225a
Show file tree
Hide file tree
Showing 114 changed files with 1,784 additions and 1,895 deletions.
37 changes: 12 additions & 25 deletions 3Sum Closeest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,22 @@ public:
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int ret = -1;
int size = num.size();
if (size < 3)
return ret;
long long dist = LLONG_MAX;
sort(num.begin(), num.end());
for (int i = 0; i < size - 2; i++) {
while ((i > 0) && (i < (size - 2)) && (num[i] == num[i-1]))
i++;
int l = i + 1, u = size - 1, sum = 0;
for (int i = 0; i < num.size() - 2; i++) {
if ((i > 0) && (num[i] == num[i-1]))
continue;
int l = i + 1, u = num.size() - 1;
while (l < u) {
sum = num[i] + num[l] + num[u];
if (ret == -1)
ret = sum;
int sum = num[i] + num[l] + num[u];
if (llabs((long long)sum - target) < llabs(dist))
dist = sum - target;
if (sum < target)
while ((++l < u) && (num[l] == num[l-1]));
else
ret = (abs(sum - target) < abs(ret - target))?sum:ret;
if (ret == target)
return ret;
else if (sum > target) {
u--;
while ((l < u) && (num[u] == num[u+1]))
u--;
}
else {
l++;
while ((l < u) && (num[l] == num[l-1]))
l++;
}
while ((--u > l) && (num[u] == num[u+1]));
}
}
return ret;
return dist + target;
}
};
37 changes: 14 additions & 23 deletions 3Sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@ public:
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > rets;
int size = num.size();
if (size < 3)
if (num.size() < 3)
return rets;
sort(num.begin(), num.end());
for (int i = 0; i < size - 2; i++) {
while ((i > 0) && (i < (size - 2)) && (num[i] == num[i-1]))
i++;
int l = i + 1, u = size - 1, sum = 0;
for (size_t i = 0; i < num.size() - 2; i++) {
if ((i > 0) && (num[i] == num[i-1]))
continue;
size_t l = i + 1, u = num.size() - 1;
while (l < u) {
sum = num[i] + num[l] + num[u];
if (sum > 0) {
u--;
while ((l < u) && (num[u] == num[u+1]))
u--;
}
else {
if (sum == 0) {
vector<int> ret;
rets.push_back(ret);
rets.back().push_back(num[i]);
rets.back().push_back(num[l]);
rets.back().push_back(num[u]);
}
l++;
while ((l < u) && (num[l] == num[l-1]))
l++;
long long sum = (long long)num[i] + num[l] + num[u];
if (sum == 0) {
rets.push_back(vector<int> (3, num[i]));
rets.back()[1] = num[l];
rets.back()[2] = num[u];
}
if (sum < 0)
while ((++l < u) && (num[l] == num[l-1]));
else
while ((--u > l) && (num[u] == num[u+1]));
}
}
return rets;
Expand Down
45 changes: 18 additions & 27 deletions 4Sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,28 @@ public:
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > rets;
int size = num.size();
if (size < 4)
if (num.size() < 4)
return rets;
sort(num.begin(), num.end());
for (int i = 0; i < size - 3; i++) {
while ((i > 0) && (i < size - 3) && (num[i] == num[i-1]))
i++;
for (int j = i+1; j < size - 2; j++) {
while ((j > i+1) && (j < size - 2) && (num[j] == num[j-1]))
j++;
int l = j + 1, u = size - 1, sum = 0;
for (size_t i = 0; i < num.size() - 3; i++) {
if ((i > 0) && (num[i] == num[i-1]))
continue;
for (size_t j = i + 1; j < num.size() - 2; j++) {
if ((j > i + 1) && (num[j] == num[j-1]))
continue;
size_t l = j + 1, u = num.size() - 1;
while (l < u) {
sum = num[i] + num[j] + num[l] + num[u];
if (sum > target) {
u--;
while ((l < u) && (num[u] == num[u+1]))
u--;
}
else {
if (sum == target) {
vector<int> ret;
rets.push_back(ret);
rets.back().push_back(num[i]);
rets.back().push_back(num[j]);
rets.back().push_back(num[l]);
rets.back().push_back(num[u]);
}
l++;
while ((l < u) && (num[l] == num[l-1]))
l++;
long long sum = (long long)num[i] + num[j] + num[l] + num[u];
if (sum == target) {
rets.push_back(vector<int> (4, num[i]));
rets.back()[1] = num[j];
rets.back()[2] = num[l];
rets.back()[3] = num[u];
}
if (sum < target)
while ((++l < u) && (num[l] == num[l-1]));
else
while ((--u > l) && (num[u] == num[u+1]));
}
}
}
Expand Down
39 changes: 16 additions & 23 deletions Add Binary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,26 @@ public:
string addBinary(string a, string b) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (a == "")
if (a.empty())
return b;
if (b == "")
if (b.empty())
return a;
int ap = a.size() - 1, bp = b.size() - 1, ca = 0, sum = 0;
string ret;
while ((ap >= 0) && (bp >= 0)) {
sum = (a[ap] - '0') + (b[bp] - '0') + ca;
ret = (char)('0' + sum%2) + ret;
ca = (sum >= 2);
ap--;
bp--;
}
while ((bp < 0) && (ap >= 0)) {
sum = (a[ap] - '0') + ca;
ret = (char)('0' + sum%2) + ret;
ca = (sum >= 2);
ap--;
}
while ((ap < 0) && (bp >= 0)) {
sum = (b[bp] - '0') + ca;
ret = (char)('0' + sum%2) + ret;
ca = (sum >= 2);
bp--;
string ret = "";
int ap = a.size() - 1, bp = b.size() - 1, ca = 0;
while ((ap >= 0) || (bp >= 0)) {
int sum = ((ap >= 0)?a[ap] - '0':0) + ((bp >= 0)?b[bp] - '0':0) + ca;
ret = ret + (char)(sum%2 + '0');
ca = sum/2;
ap = (ap >= 0)?ap-1:ap;
bp = (bp >= 0)?bp-1:bp;
}
if (ca > 0)
ret = '1' + ret;
ret = ret + '1';
for (int i = 0; i < ret.size()/2; i++) {
char c = ret[i];
ret[i] = ret[ret.size()-1-i];
ret[ret.size()-1-i] = c;
}
return ret;
}
};
47 changes: 13 additions & 34 deletions Add Two Numbers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,20 @@ public:
return l2;
if (!l2)
return l1;
ListNode* head = new ListNode(0), *tail = head;
int sum = 0, car = 0;
while (l1 && l2) {
sum = l1->val + l2->val + car;
ListNode* node = new ListNode(sum%10);
car = sum/10;
tail->next = node;
ListNode *head = new ListNode(0), *tail = head;
int c = 0;
while (l1 || l2) {
int s = (l1?l1->val:0) + (l2?l2->val:0) + c;
tail->next = new ListNode(s%10);
tail = tail->next;
l1 = l1->next;
l2 = l2->next;
c = s/10;
l1 = l1?l1->next:l1;
l2 = l2?l2->next:l2;
}
while (!l2 && l1) {
sum = l1->val + car;
ListNode* node = new ListNode(sum%10);
car = sum/10;
tail->next = node;
tail = tail->next;
l1 = l1->next;
}
while (!l1 && l2) {
sum = l2->val + car;
ListNode* node = new ListNode(sum%10);
car = sum/10;
tail->next = node;
tail = tail->next;
l2 = l2->next;
}
if (car > 0) {
ListNode* node = new ListNode(car);
tail->next = node;
tail = tail->next;
}
ListNode* temp = head;
head = head->next;
delete temp;
return head;
if (c > 0)
tail->next = new ListNode(1);
tail = head->next;
delete head;
return tail;
}
};
31 changes: 11 additions & 20 deletions Anagrams.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@ public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> anagrams;
map<string, vector<string> > maps;
for (int i = 0; i < strs.size(); i++)
maps[sorts(strs[i])].push_back(strs[i]);
map<string,vector<string> >::iterator it = maps.begin();
vector<string> rets;
unordered_map<string, vector<string>> maps;
for (size_t i = 0; i < strs.size(); i++) {
string r = strs[i];
sort(r.begin(), r.end());
maps[r].push_back(strs[i]);
}
unordered_map<string, vector<string>>::iterator it = maps.begin();
while (it != maps.end()) {
if (it->second.size() > 1) {
for (int i = 0; i < it->second.size(); i++)
anagrams.push_back((it->second)[i]);
}
if (it->second.size() > 1)
rets.insert(rets.end(), it->second.begin(), it->second.end());
it++;
}
return anagrams;
}
string sorts(string& s) {
vector<char> r;
for (int i = 0; i < s.size(); i++)
r.push_back(s[i]);
sort(r.begin(), r.end());
string ret;
for (int i = 0; i < r.size(); i++)
ret+= r[i];
return ret;
return rets;
}
};
15 changes: 6 additions & 9 deletions Balanced Binary Tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ public:
bool isBalanced(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (!root)
return true;
bool ret = true;
height(root, ret);
return ret;
}
int height(TreeNode* root, bool& ret) {
if (root == NULL)
int height(TreeNode* root, bool &ret) {
if (!root)
return 0;
int lh = height(root->left, ret);
int rh = height(root->right, ret);
if (abs(lh - rh) > 1)
ret = false;
return max(lh, rh) + 1;
int l = height(root->left, ret);
int r = height(root->right, ret);
ret = (abs(l - r) > 1)?false:ret;
return max(l, r)+1;
}
};
23 changes: 7 additions & 16 deletions Best Time to Buy and Sell Stock II.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = prices.size();
if (size < 2)
return 0;
int prof = 0;
vector<int> b(1, prices[0]);
for (int i = 1; i < size; i++) {
if (prices[i] > b.back())
b.push_back(prices[i]);
else {
prof += b.back() - b[0];
while (b.size() > 0)
b.pop_back();
b.push_back(prices[i]);
int ret = 0, beg = 0;
for (int i = 1; i < prices.size(); i++)
if (prices[i] < prices[i-1]) {
ret += prices[i-1] - prices[beg];
beg = i;
}
}
prof += b.back() - b[0];
return prof;
ret += prices.empty()?0:prices.back() - prices[beg];
return ret;
}
};
24 changes: 10 additions & 14 deletions Best Time to Buy and Sell Stock III.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ public:
// DO NOT write int main() function
if (prices.size() < 2)
return 0;
int size = prices.size();
int l[size];
int r[size];
int minp = prices[0];
int size = prices.size(), l[size], r[size];
int miv = prices[0], mav = prices.back();
l[0] = 0;
for (int i = 1; i < size; i++) {
l[i] = max((prices[i] - minp), l[i-1]);
minp = min(prices[i], minp);
miv = min(miv, prices[i]);
l[i] = max(l[i-1], prices[i] - miv);
}
r[size-1] = 0;
int maxp = prices[size-1];
for (int i = size - 2; i >= 0; i--) {
r[i] = max((maxp - prices[i]), r[i+1]);
maxp = max(prices[i], maxp);
mav = max(mav, prices[i]);
r[i] = max(r[i+1], mav - prices[i]);
}
int m = l[size - 1];
for (int i = 0; i < size - 2; i++) {
m = max(l[i]+r[i+1], m);
}
return m;
int ret = r[0];
for (int i = 0; i < size - 1; i++)
ret = max(ret, l[i] + r[i+1]);
return ret;
}
};
Loading

0 comments on commit 302225a

Please sign in to comment.