File tree Expand file tree Collapse file tree 5 files changed +117
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ void dfs (Node* node, unordered_map<Node*, Node*>& map){
4+ if (map.count (node))
5+ return ;
6+
7+ map[node] = new Node (node->val );
8+
9+ for (int i = 0 ; i < node->neighbors .size (); i++)
10+ dfs (node->neighbors [i], map);
11+ }
12+
13+ Node* cloneGraph (Node* node) {
14+ if (node == NULL )
15+ return NULL ;
16+
17+ unordered_map<Node*, Node*> map;
18+
19+ dfs (node, map);
20+
21+ for (auto & x : map){
22+ Node* org = x.first ;
23+ Node* dst = x.second ;
24+ for (int i = 0 ; i < org->neighbors .size (); i++){
25+ dst->neighbors .push_back (map[org->neighbors [i]]);
26+ }
27+ }
28+
29+ return map[node];
30+ }
31+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int longestCommonSubsequence (string text1, string text2) {
4+ int t1_len = text1.length ();
5+ int t2_len = text2.length ();
6+ vector<vector<int >> dp (t1_len + 1 , vector (t2_len + 1 , 0 ));
7+
8+ for (int i = 1 ; i <= t1_len; i++){
9+ for (int j = 1 ; j <= t2_len; j++){
10+ if (text1[i-1 ] == text2[j-1 ])
11+ dp[i][j] = dp[i-1 ][j-1 ] + 1 ;
12+ else
13+ dp[i][j] = max (dp[i - 1 ][j], dp[i][j - 1 ]);
14+ }
15+ }
16+
17+ return dp[t1_len][t2_len];
18+ }
19+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int characterReplacement (string s, int k) {
4+ int start = 0 ;
5+ int max_len = 0 ;
6+ int max_cnt = 0 ;
7+ int map[26 ] = {0 };
8+
9+ for (int end = 0 ; end < s.length (); end++){
10+ map[s[end] - ' A' ]++;
11+
12+ max_cnt = max (max_cnt, map[s[end] - ' A' ]);
13+
14+ while (end - start + 1 - max_cnt > k){
15+ map[s[start] - ' A' ]--;
16+ start++;
17+ }
18+
19+ max_len = max (max_len, end - start + 1 );
20+ }
21+
22+ return max_len;
23+ }
24+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int countSubstrings (string s) {
4+ int cnt = 0 ;
5+
6+ for (int i = 0 ; i < s.length (); i++){
7+ int start = i;
8+ int end = i;
9+
10+ while (0 <= start && end < s.length () && s[start] == s[end]){
11+ cnt++;
12+ start--;
13+ end++;
14+ }
15+
16+ start = i;
17+ end = i + 1 ;
18+
19+ while (0 <= start && end < s.length () && s[start] == s[end]){
20+ cnt++;
21+ start--;
22+ end++;
23+ }
24+ }
25+
26+ return cnt;
27+ }
28+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ uint32_t reverseBits (uint32_t n) {
4+ uint32_t result = 0 ;
5+
6+ for (int i = 0 ; i < 32 ; i++){
7+ result <<= 1 ;
8+
9+ result |= (n & 1 );
10+ n >>= 1 ;
11+ }
12+
13+ return result;
14+ }
15+ };
You can’t perform that action at this time.
0 commit comments