Skip to content

Commit cadb13e

Browse files
committed
621
1 parent 4dcfcea commit cadb13e

10 files changed

+538
-3
lines changed

15. 3Sum.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
3+
4+
Note:
5+
6+
The solution set must not contain duplicate triplets.
7+
8+
Example:
9+
10+
Given array nums = [-1, 0, 1, 2, -1, -4],
11+
12+
A solution set is:
13+
[
14+
[-1, 0, 1],
15+
[-1, -1, 2]
16+
]
17+
18+
Sort first, then do comparison, early break, etc..
19+
*/
20+
class Solution {
21+
public:
22+
vector<vector<int>> threeSum(vector<int>& nums) {
23+
sort( nums.begin(), nums.end() );
24+
vector<vector<int>> res;
25+
if ( nums.empty() || nums.front() > 0 || nums.back() < 0 ) return res;
26+
for ( int i = 0; i <= (int)nums.size() - 3; ++i )
27+
{
28+
if ( nums[i] > 0 ) break;
29+
if ( i > 0 && nums[i] == nums[i - 1] ) continue;
30+
int target = -nums[i];
31+
int j = i + 1, k = nums.size() - 1;
32+
while ( j < k )
33+
{
34+
if ( nums[j] + nums[k] == target )
35+
{
36+
res.push_back( {nums[i], nums[j], nums[k]} );
37+
while ( j < k && nums[j + 1] == nums[j] ) ++j;
38+
while ( j < k && nums[k - 1] == nums[k] ) --k;
39+
++j;
40+
--k;
41+
}
42+
else if ( nums[j] + nums[k] < target )
43+
++j;
44+
else
45+
--k;
46+
}
47+
}
48+
return res;
49+
}
50+
};

16. 3Sum Closest.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
3+
4+
5+
6+
Example 1:
7+
8+
Input: nums = [-1,2,1,-4], target = 1
9+
Output: 2
10+
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
11+
12+
13+
Constraints:
14+
15+
3 <= nums.length <= 10^3
16+
-10^3 <= nums[i] <= 10^3
17+
-10^4 <= target <= 10^4
18+
19+
20+
Solution similar to the previous question.
21+
22+
*/
23+
24+
class Solution {
25+
public:
26+
int threeSumClosest(vector<int>& nums, int target) {
27+
sort( nums.begin(), nums.end() );
28+
int sum = nums[0] + nums[1] + nums[2], diff = abs( sum - target ), n = nums.size();
29+
if ( target > 0 && nums.back() < 0 ) return nums[n - 1] + nums[n - 2] + nums[n - 3];
30+
for ( int i = 0; i < n - 2; ++i )
31+
{
32+
if ( nums[i] * 3 > target ) return min( sum, nums[i] + nums[i + 1] + nums[i + 2] );
33+
int j = i + 1, k = n - 1;
34+
while ( j < k )
35+
{
36+
int tmp = nums[i] + nums[j] + nums[k];
37+
int newDiff = abs( tmp - target );
38+
if ( newDiff < diff )
39+
{
40+
diff = newDiff;
41+
sum = tmp;
42+
}
43+
if ( tmp < target ) ++j;
44+
else --k;
45+
}
46+
}
47+
return sum;
48+
}
49+
};
50+
51+
//Optimized
52+
class Solution {
53+
public:
54+
int threeSumClosest(vector<int>& nums, int target) {
55+
sort( nums.begin(), nums.end() );
56+
int sum = nums[0] + nums[1] + nums[2], diff = abs( sum - target ), n = nums.size();
57+
if ( target > 0 && nums.back() < 0 ) return nums[n - 1] + nums[n - 2] + nums[n - 3];
58+
for ( int i = 0; i < n - 2; ++i )
59+
{
60+
if ( nums[i] * 3 > target ) return min( sum, nums[i] + nums[i + 1] + nums[i + 2] );
61+
if ( i > 0 && nums[i] == nums[i - 1] ) continue;
62+
int j = i + 1, k = n - 1;
63+
while ( j < k )
64+
{
65+
int tmp = nums[i] + nums[j] + nums[k];
66+
if ( tmp == target ) return tmp;
67+
int newDiff = abs( tmp - target );
68+
if ( newDiff < diff )
69+
{
70+
diff = newDiff;
71+
sum = tmp;
72+
}
73+
if ( tmp < target )
74+
{
75+
while ( ++j < k && nums[j] == nums[j - 1] );
76+
}
77+
else
78+
{
79+
while ( j < --k && nums[k] == nums[k + 1] );
80+
}
81+
}
82+
}
83+
return sum;
84+
}
85+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
3+
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
4+
5+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
6+
7+
8+
9+
Example:
10+
11+
Input: "23"
12+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
13+
Note:
14+
15+
Although the above answer is in lexicographical order, your answer could be in any order you want.
16+
17+
18+
Solution one is iterative,
19+
two is recursive
20+
*/
21+
class Solution {
22+
public:
23+
vector<string> letterCombinations(string digits) {
24+
if (digits.empty()) return {};
25+
vector<string> res{""};
26+
vector<string> dict = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
27+
for ( int i = 0; i < digits.size(); ++i )
28+
{
29+
vector<string> temp;
30+
string tp = dict[digits[i] - '0'];
31+
for ( int j = 0; j < tp.size(); ++j )
32+
for ( auto &a: res ) temp.push_back( a + tp[j] );
33+
res = temp;
34+
}
35+
return res;
36+
}
37+
};
38+
39+
class Solution {
40+
public:
41+
vector<string> letterCombinations(string digits) {
42+
if (digits.empty()) return {};
43+
vector<string> res;
44+
vector<string> dict = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
45+
DFSCombine( dict, 0, "", res, digits );
46+
return res;
47+
}
48+
void DFSCombine( vector<string>& dict, int level, string out, vector<string>& res, string digits )
49+
{
50+
if ( level == digits.size() )
51+
{
52+
res.push_back( out );
53+
return;
54+
}
55+
string temp = dict[digits[level] - '0'];
56+
for ( int i = 0; i < temp.size(); ++i )
57+
{
58+
DFSCombine( dict, level + 1, out + temp[i], res, digits );
59+
}
60+
}
61+
};

18. 4Sum.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
3+
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
4+
5+
Note:
6+
7+
The solution set must not contain duplicate quadruplets.
8+
9+
Example:
10+
11+
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
12+
13+
A solution set is:
14+
[
15+
[-1, 0, 0, 1],
16+
[-2, -1, 1, 2],
17+
[-2, 0, 0, 2]
18+
]
19+
20+
21+
Solution one using two pointers, traditional Solution
22+
Two using the characteristic of set
23+
*/
24+
class Solution {
25+
public:
26+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
27+
set<vector<int>> res;
28+
int n = nums.size();
29+
sort( nums.begin(), nums.end() );
30+
if ( nums.empty() || ( target < 0 && nums.front() > 0 ) || ( target > 0 && nums.back() < 0 ) ) return {};
31+
for ( int i = 0; i < n - 3; ++i )
32+
{
33+
for ( int j = i + 1; j < n - 2; ++j )
34+
{
35+
int k = j + 1, z = n - 1;
36+
while ( k < z )
37+
{
38+
int sum = nums[i] + nums[j] + nums[k] + nums[z];
39+
if ( target == sum )
40+
{
41+
vector<int> temp = { nums[i], nums[j], nums[k], nums[z] };
42+
res.insert( temp );
43+
++k; --z;
44+
}
45+
else if ( target < sum )
46+
{
47+
--z;
48+
}
49+
else ++k;
50+
}
51+
}
52+
}
53+
return vector<vector<int>>( res.begin(), res.end() );
54+
}
55+
};
56+
57+
//Recursive
58+
class Solution {
59+
public:
60+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
61+
set<vector<int>> res;
62+
int n = nums.size();
63+
sort( nums.begin(), nums.end() );
64+
if ( nums.empty() || ( target < 0 && nums.front() > 0 ) || ( target > 0 && nums.back() < 0 ) ) return {};
65+
for ( int i = 0; i < n - 3; ++i )
66+
{
67+
for ( int j = i + 1; j < n - 2; ++j )
68+
{
69+
int k = j + 1, z = n - 1;
70+
while ( k < z )
71+
{
72+
int sum = nums[i] + nums[j] + nums[k] + nums[z];
73+
if ( target == sum )
74+
{
75+
vector<int> temp = { nums[i], nums[j], nums[k], nums[z] };
76+
res.insert( temp );
77+
++k; --z;
78+
}
79+
else if ( target < sum )
80+
{
81+
--z;
82+
}
83+
else ++k;
84+
}
85+
}
86+
}
87+
return vector<vector<int>>( res.begin(), res.end() );
88+
}
89+
};

201. Bitwise AND of Numbers Range.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
3+
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
4+
5+
Example 1:
6+
7+
Input: [5,7]
8+
Output: 4
9+
Example 2:
10+
11+
Input: [0,1]
12+
Output: 0
13+
14+
Solution one using mask
15+
Solution two shift m, n, until they are equal
16+
Solution three is recursive
17+
Solution four using n & ( n - 1 )
18+
19+
*/
20+
21+
22+
class Solution {
23+
public:
24+
int rangeBitwiseAnd(int m, int n) {
25+
if ( m == 0 ) return 0;
26+
unsigned int d = 0x7fffffff;
27+
while ((m & d) != (n & d)) {
28+
d <<= 1;
29+
}
30+
return m & d;
31+
}
32+
};
33+
34+
35+
class Solution {
36+
public:
37+
int rangeBitwiseAnd(int m, int n) {
38+
if ( m == 0 || n == 0 ) return 0;
39+
int i = 0;
40+
while ( m != n )
41+
{
42+
m >>= 1;
43+
n >>= 1;
44+
i++;
45+
}
46+
return m << i;
47+
}
48+
};
49+
50+
class Solution {
51+
public:
52+
int rangeBitwiseAnd(int m, int n) {
53+
return (n > m) ? (rangeBitwiseAnd(m / 2, n / 2) << 1) : m;
54+
}
55+
};
56+
57+
class Solution {
58+
public:
59+
int rangeBitwiseAnd(int m, int n) {
60+
while ( n > m )
61+
{
62+
n = n & ( n - 1 );
63+
}
64+
return n;
65+
}
66+
};

303. Range Sum Query - Immutable.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
3+
4+
Example:
5+
Given nums = [-2, 0, 3, -5, 2, -1]
6+
7+
sumRange(0, 2) -> 1
8+
sumRange(2, 5) -> -1
9+
sumRange(0, 5) -> -3
10+
Note:
11+
You may assume that the array does not change.
12+
There are many calls to sumRange function.
13+
14+
15+
Solution is using dp
16+
*/
17+
class NumArray {
18+
public:
19+
NumArray(vector<int>& nums) {
20+
dp.resize( nums.size() + 1, 0 );
21+
for ( int i = 1; i <= nums.size(); ++i )
22+
{
23+
dp[i] = dp[i - 1] + nums[i - 1];
24+
}
25+
}
26+
27+
int sumRange(int i, int j) {
28+
return dp[j + 1] - dp[i];
29+
}
30+
private:
31+
vector<int> dp;
32+
};
33+
34+
/**
35+
* Your NumArray object will be instantiated and called as such:
36+
* NumArray* obj = new NumArray(nums);
37+
* int param_1 = obj->sumRange(i,j);
38+
*/

0 commit comments

Comments
 (0)