Skip to content

Commit e2ee6f2

Browse files
authored
Merge pull request #1274 from PDKhan/main
2 parents 99c80ed + 2fce508 commit e2ee6f2

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

combination-sum/PDKhan.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
void backtrack(vector<int>& candidates, int target, int index, vector<int>& curr, vector<vector<int>>& result){
4+
if(target < 0)
5+
return;
6+
7+
if(target == 0){
8+
result.push_back(curr);
9+
return;
10+
}
11+
12+
for(int i = index; i < candidates.size(); i++){
13+
curr.push_back(candidates[i]);
14+
backtrack(candidates, target-candidates[i], i, curr, result);
15+
curr.pop_back();
16+
}
17+
}
18+
19+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
20+
vector<vector<int>> result;
21+
vector<int> curr;
22+
23+
backtrack(candidates, target, 0, curr, result);
24+
25+
return result;
26+
}
27+
};

decode-ways/PDKhan.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
int n = s.length();
5+
6+
if(n == 0 || s[0] == '0')
7+
return 0;
8+
9+
int next = 1;
10+
int nextnext = 1;
11+
int curr = 0;
12+
13+
for(int i = n - 1; i >= 0; i--){
14+
if(s[i] == '0')
15+
curr = 0;
16+
else{
17+
curr = next;
18+
if(i < n - 1 && (s[i] == '1' || (s[i] == '2' && s[i+1] >= '0' && s[i+1] <= '6')))
19+
curr += nextnext;
20+
}
21+
22+
nextnext = next;
23+
next = curr;
24+
}
25+
26+
return curr;
27+
}
28+
};

maximum-subarray/PDKhan.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int cur = nums[0];
5+
int max = nums[0];
6+
7+
for(int i = 1; i < nums.size(); i++){
8+
if(nums[i] > cur + nums[i])
9+
cur = nums[i];
10+
else
11+
cur = cur + nums[i];
12+
13+
if(max < cur)
14+
max = cur;
15+
}
16+
17+
return max;
18+
}
19+
};

number-of-1-bits/PDKhan.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int hammingWeight(int n) {
4+
int cnt = 0;
5+
6+
while(n){
7+
if(n & 1 == 1)
8+
cnt++;
9+
10+
n >>= 1;
11+
}
12+
13+
return cnt;
14+
}
15+
};

valid-palindrome/PDKhan.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(string s) {
4+
int left = 0;
5+
int right = s.length()-1;
6+
7+
while(left < right){
8+
while(left < right && !isalnum(s[left])) left++;
9+
while(left < right && !isalnum(s[right])) right--;
10+
11+
if(tolower(s[left]) != tolower(s[right]))
12+
return false;
13+
14+
left++;
15+
right--;
16+
}
17+
18+
return true;
19+
}
20+
};

0 commit comments

Comments
 (0)