Now comes the solution (in C++) ...
'1'-'0' = 1.
代码: |
class Solution {
public:
string addBinary(string a, string b) {
int N = a.size(), M = b.size(), K = max(N, M);
string res(K, ' ');
int carry = 0;
int i = N-1, j = M-1, k = K-1;
while (i >= 0 || j >= 0)
{
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';
carry = sum / 2;
res[k--] = sum % 2 + '0';
}
if (carry == 1)
res.insert(res.begin(), '1');
return res;
}
};
|
Problem Name: Add Two Numbers
Description:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Now comes the solution (in C++) ...
dummy head...
代码: |
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode dummy(0), *cur = &dummy;
int carry = 0;
while (l1 || l2 || carry)
{
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum >= 10 ? 1 : 0;
sum %= 10;
ListNode *newNode = new ListNode(sum);
cur->next = newNode;
cur = newNode;
}
return dummy.next;
}
};
|
Problem Name: Anagrams
Description:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
Now comes the solution (in C++) ...
Sort the string to see if they're anagrams.
Solution 1 is simpler than 2.
代码: |
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
return anagrams_1(strs);
}
// solution 1
vector<string> anagrams_1(vector<string> &strs) {
typedef map<string, vector<int> > MAP;
MAP map;
for (int i = 0; i < strs.size(); ++i)
{
string s = strs[i];
sort(s.begin(), s.end());
map[s].push_back(i);
}
vector<string> res;
MAP::iterator it = map.begin();
for (; it != map.end(); it++)
{
vector<int> &anagrams = it->second;
if (anagrams.size() > 1) {
for (int i = 0; i < anagrams.size(); ++i)
res.push_back(strs[anagrams[i]]);
}
}
return res;
}
// solution 2
vector<string> anagrams_2(vector<string> &strs) {
typedef unordered_map<string, int > MAP;
vector<string> res;
MAP anagram;
for (int i = 0; i < strs.size(); ++i)
{
string s = strs[i];
sort(s.begin(), s.end());
MAP::iterator it = anagram.find(s);
if (it == anagram.end())
{
anagram[s] = i;
}
else
{
if (it->second >= 0) {
res.push_back(strs[it->second]);
it->second = -1;
}
res.push_back(strs[i]);
}
}
return res;
}
};
|
Problem Name: Balanced Binary Tree
Description:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which
the depth of the two subtrees of every node never differ by more than 1.
Now comes the solution (in C++) ...
DFS.
代码: |
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
int height = 0;
return isBalancedRe(root, height);
}
bool isBalancedRe(TreeNode *root, int &height){
if (!root) return true;
int leftHeight = 0, rightHeight = 0;
if (!isBalancedRe(root->left, leftHeight)) return false;
if (!isBalancedRe(root->right, rightHeight)) return false;
if (abs(leftHeight-rightHeight) > 1) return false;
height = 1 + max(leftHeight, rightHeight);
return true;
}
};
|
Problem Name: Best Time to Buy and Sell Stock
Description:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
design an algorithm to find the maximum profit.
Now comes the solution (in C++) ...
For each element, calculate the max difference with the former elements.
代码: |
class Solution {
public:
int maxProfit(vector<int> &prices) {
int imin = 0;
int res = 0;
for (int i = 1; i < prices.size(); ++i)
{
if (prices[i] < prices[imin])
imin = i;
res = max(res, prices[i] - prices[imin]);
}
return res;
}
};
|
Problem Name: Best Time to Buy and Sell Stock II
Description:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
(ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).