29. Divide Two Integers
Problem Description
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
Implementation
class Solution {
public:
int divide(int dividend, int divisor) {
if(!divisor || (dividend == INT_MIN && divisor == -1)) {
return INT_MAX;
}
int sign = (dividend & (1 << 31)) ^ (divisor & (1 << 31));
long long ddend = labs(dividend);
long long dvisor = labs(divisor);
int res = 0;
while(ddend >= dvisor) {
int sft = 0;
long long int sft_num = dvisor;
while(ddend >= sft_num) {
sft_num <<= 1;
sft++;
}
res += 1 << (sft-1);
ddend -= dvisor << (sft - 1);
}
return sign?(-1*res):res;
}
};
49. Group Anagrams
Problem Description
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note: All inputs will be in lower-case.
Implementation
If you utilize unordered_map, we can get quick speed. As we are not concerned about searching operations, we need insert more frequently.
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string> strs) {
map<string, vector<string>> mmp;
int size;
if( (size = strs.size()) == 0) {
vector<vector<string>> zero_res;
return zero_res;
}
for(int idx = 0; idx < size; idx++) {
string tmp = strs[idx];
sort(strs[idx].begin(), strs[idx].end());
if(mmp[strs[idx]].size() > 0) {
mmp[strs[idx]].push_back(tmp);
}
else {
vector<string> tmp_new;
tmp_new.push_back(tmp);
mmp[strs[idx]] = tmp_new;
}
}
vector<vector<string>> res;
for(map<string, vector<string>>::iterator it = mmp.begin(); it != mmp.end(); it++) {
res.push_back(it->second);
}
return res;
}
};
96. Unique Binary Search Trees
Problem Description
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Implementation
Dynamic Program:
class Solution {
public:
int numTrees(int n) {
vector<int> res(n+1, 0);
res[0] = res[1] = 1;
for(int idx0 = 2; idx0 <= n; idx0++) {
for(int idx1 = 1; idx1 <= idx0; idx1++) {
res[idx0] += res[idx1 - 1]*res[idx0 - idx1];
}
}
return res[n];
}
};
mathematics way:
class Solution {
public:
int numTrees(int n) {
// C(2n, n)/(n+1)
long long int res = 1;
for(int num = n + 1; num <= (n << 1); num++) {
res = res*num/(num-n);
}
return res/(n+1);
}
};
560. Subarray Sum Equals K
problem description
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
Note:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
implementation
solution 1: accumulated sum
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = 0;
int size = nums.size();
if(!size) {
return res;
}
for(int idx = 1; idx < size; idx++) {
nums[idx] += nums[idx-1];
}
for(int idx = 0; idx < size; idx++) {
if(nums[idx] == k) {
res++;
}
}
for(int idx0 = 0; idx0 < size - 1; idx0++) {
for(int idx1 = idx0 + 1; idx1 < size; idx1++) {
if(nums[idx1] - nums[idx0] == k) {
res++;
}
}
}
return res;
}
};
solution 2:
with map.
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
map<int, int> rec;
int res = 0;
int cum = 0;
int size = nums.size();
if(!size) {
return res;
}
rec[0]++;
for(int idx = 0; idx < size; idx++) {
cum += nums[idx];
res += rec[cum - k];
rec[cum]++;
}
return res;
}
};