File tree Expand file tree Collapse file tree 5 files changed +105
-0
lines changed
Expand file tree Collapse file tree 5 files changed +105
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<vector<int >> ans;
4+ vector<int > candids;
5+ int t;
6+
7+ void dfs (int idx, int sum, vector<int > s) {
8+ if (sum > t) return ;
9+ else if (sum == t) ans.push_back (s);
10+
11+ for (int i = idx; i < candids.size (); i++) {
12+ auto next = vector<int >(s);
13+ next.push_back (candids[i]);
14+ dfs (i, sum+candids[i], next);
15+ }
16+ };
17+
18+ vector<vector<int >> combinationSum (vector<int >& candidates, int target) {
19+ candids = candidates;
20+ t = target;
21+ dfs (0 , 0 , {});
22+ return ans;
23+ }
24+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int mem[100 ];
4+ string str;
5+
6+ bool isValid (string s) {
7+ if (s.size () == 1 ) {
8+ return ' 1' <= s[0 ] && s[0 ] <= ' 9' ;
9+ }
10+ else if (s.size () == 2 ) {
11+ if (s[0 ] == ' 1' ) return ' 0' <= s[1 ] && s[1 ] <= ' 9' ;
12+ else if (s[0 ] == ' 2' ) return ' 0' <= s[1 ] && s[1 ] <= ' 6' ;
13+ }
14+ return false ;
15+ }
16+
17+ int dfs (int idx) {
18+ if (idx >= str.size ()) return 1 ;
19+
20+ int & ret = mem[idx];
21+ if (ret != -1 ) return ret;
22+ ret = 0 ;
23+
24+ if (isValid (str.substr (idx, 1 ))){
25+ ret += dfs (idx+1 );
26+ }
27+ if (idx < str.size () - 1 && isValid (str.substr (idx, 2 ))){
28+ ret += dfs (idx+2 );
29+ }
30+ return ret;
31+ }
32+
33+ int numDecodings (string s) {
34+ str = s;
35+ fill (mem, mem+100 , -1 );
36+ return dfs (0 );
37+ }
38+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int maxSubArray (vector<int >& nums) {
4+ int dp[100000 ];
5+ dp[0 ] = max (nums[0 ], -(1 <<30 ));
6+ for (int i = 1 ; i < nums.size (); i++) {
7+ dp[i] = max (dp[i-1 ] + nums[i], nums[i]);
8+ }
9+ int m = dp[0 ];
10+ for (int i = 1 ; i < nums.size (); i++) {
11+ m = max (dp[i], m);
12+ }
13+ return m;
14+ }
15+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int hammingWeight (int n) {
4+ return popcount ((unsigned int )n);
5+ }
6+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool isPalindrome (string s) {
4+ string p;
5+ for (char const & c: s) {
6+ if (' A' <= c && c <= ' Z' ) {
7+ p.push_back (c - ' A' + ' a' );
8+ }
9+ else if (' a' <= c && c <= ' z' ) {
10+ p.push_back (c);
11+ }
12+ else if (' 0' <= c && c <= ' 9' ) {
13+ p.push_back (c);
14+ }
15+ }
16+ int start = 0 , end = p.size ()-1 ;
17+ while (start <= end) {
18+ if (p[start++] != p[end--]) return false ;
19+ }
20+ return true ;
21+ }
22+ };
You can’t perform that action at this time.
0 commit comments