File tree Expand file tree Collapse file tree 22 files changed +311
-0
lines changed Expand file tree Collapse file tree 22 files changed +311
-0
lines changed Original file line number Diff line number Diff line change
1
+ bool isSameTree (TreeNode* p, TreeNode* q) {
2
+ if (p == NULL && q == NULL )
3
+ return true ;
4
+ else if (p == NULL && q != NULL )
5
+ return false ;
6
+ else if (p != NULL && q == NULL )
7
+ return false ;
8
+ else
9
+ {
10
+ return p->val == q->val && isSameTree (p->left , q->left ) && isSameTree (p->right , q->right );
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ int maxDepth (TreeNode* root) {
2
+ if (root == 0 )
3
+ return 0 ;
4
+ int leftdepth = maxDepth (root->left );
5
+ int rightdepth = maxDepth (root->right );
6
+
7
+ return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1 ;
8
+ }
Original file line number Diff line number Diff line change
1
+ bool isPalindrome (string s) {
2
+ int len = s.size ();
3
+ int i = 0 , j = len - 1 ;
4
+ while (i < j)
5
+ {
6
+ while (i < j && !isalnum (s[i]))
7
+ ++i;
8
+ if (i == j)
9
+ return true ;
10
+ while (i < j && !isalnum (s[j]))
11
+ --j;
12
+ if (i == j)
13
+ return true ;
14
+ if (!(s[i] == s[j] || tolower (s[i]) == tolower (s[j])))
15
+ return false ;
16
+ else
17
+ {
18
+ ++i;
19
+ --j;
20
+ }
21
+ }
22
+ return true ;
23
+ }
Original file line number Diff line number Diff line change
1
+ int singleNumber (vector<int >& nums) {
2
+ int res = nums[0 ];
3
+ int len = nums.size ();
4
+ for (int i = 1 ; i < len; ++i)
5
+ res ^= nums[i];
6
+ return res;
7
+ }
Original file line number Diff line number Diff line change
1
+ int titleToNumber (string s) {
2
+ int len = s.size ();
3
+ int sum = 0 ;
4
+ for (int i = 0 ; i < len; ++i)
5
+ sum = sum * 26 + s[i] - ' A' + 1 ;
6
+ return sum;
7
+ }
Original file line number Diff line number Diff line change
1
+ TreeNode* invertTree (TreeNode* root) {
2
+ if (root == NULL )
3
+ return NULL ;
4
+ TreeNode* node = invertTree (root->left );
5
+ root->left = invertTree (root->right );
6
+ root->right = node;
7
+ return root;
8
+ }
Original file line number Diff line number Diff line change
1
+ void deleteNode (ListNode* node) {
2
+ ListNode* p = node;
3
+ ListNode* q = node->next ;
4
+ while (q->next != NULL )
5
+ {
6
+ p->val = q->val ;
7
+ p = q;
8
+ q = q->next ;
9
+ }
10
+ p->val = q->val ;
11
+ delete q;
12
+ p->next = NULL ;
13
+ }
Original file line number Diff line number Diff line change
1
+ bool isAnagram (string s, string t) {
2
+ int alpha1[26 ] = {0 };
3
+ int alpha2[26 ] = {0 };
4
+ int len1 = s.size ();
5
+ int len2 = t.size ();
6
+ for (int i = 0 ; i < len1; ++i)
7
+ alpha1[s[i] - ' a' ]++;
8
+ for (int i = 0 ; i < len2; ++i)
9
+ alpha2[t[i] - ' a' ]++;
10
+ for (int i = 0 ; i < 26 ; ++i)
11
+ if (alpha1[i] != alpha2[i])
12
+ return false ;
13
+ return true ;
14
+ }
Original file line number Diff line number Diff line change
1
+ int addDigits (int num) {
2
+ if (num == 0 )
3
+ return 0 ;
4
+ return num % 9 == 0 ? 9 : num % 9 ;
5
+ }
Original file line number Diff line number Diff line change
1
+ void moveZeroes (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ int i = 0 , j = 0 ;
4
+ while (i < len && j < len)
5
+ {
6
+ while (i < len && nums[i] != 0 )
7
+ i++;
8
+ j = i + 1 ;
9
+ while (j < len && nums[j] == 0 )
10
+ j++;
11
+ if (j >= len)
12
+ break ;
13
+ swap (nums[i], nums[j]);
14
+ i++;
15
+ j++;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ bool canWinNim (int n) {
2
+ return n % 4 != 0 ;
3
+ }
Original file line number Diff line number Diff line change
1
+ string reverseString (string s) {
2
+ reverse (s.begin (), s.end ());
3
+ return s;
4
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > intersection (vector<int >& nums1, vector<int >& nums2) {
2
+ map<int , int > mp;
3
+ vector<int > res;
4
+ int len1 = nums1.size ();
5
+ for (int i = 0 ; i < len1; ++i)
6
+ mp[nums1[i]]++;
7
+ sort (nums2.begin (), nums2.end ());
8
+ int len2 = nums2.size ();
9
+ int j = 0 ;
10
+ while (j < len2)
11
+ {
12
+ int val = nums2[j];
13
+ if (mp.find (val) != mp.end ())
14
+ {
15
+ res.push_back (val);
16
+ }
17
+ while (j < len2 && nums2[j] == val)
18
+ ++j;
19
+ }
20
+ return res;
21
+ }
Original file line number Diff line number Diff line change
1
+ int getSum (int a, int b) {
2
+ while (b != 0 )
3
+ {
4
+ int tmp = a ^ b;
5
+ b = (a & b) << 1 ;
6
+ a = tmp;
7
+ }
8
+ return a;
9
+ }
Original file line number Diff line number Diff line change
1
+ bool canConstruct (string ransomNote, string magazine) {
2
+ int alpha1[26 ] = {0 };
3
+ int alpha2[26 ] = {0 };
4
+ int len1 = ransomNote.size ();
5
+ int len2 = magazine.size ();
6
+ if (len1 > len2)
7
+ return false ;
8
+ for (int i = 0 ; i < len1; ++i)
9
+ alpha1[ransomNote[i] - ' a' ]++;
10
+ for (int i = 0 ; i < len2; ++i)
11
+ alpha2[magazine[i] - ' a' ]++;
12
+ for (int i = 0 ; i < 26 ; ++i)
13
+ if (alpha1[i] > alpha2[i])
14
+ return false ;
15
+
16
+ return true ;
17
+ }
Original file line number Diff line number Diff line change
1
+ int firstUniqChar (string s) {
2
+ int alpha[26 ] = {0 };
3
+ int len = s.size ();
4
+ for (int i = 0 ; i < len; ++i)
5
+ alpha[s[i] - ' a' ]++;
6
+ for (int i = 0 ; i < len; ++i)
7
+ if (alpha[s[i] - ' a' ] == 1 )
8
+ return i;
9
+ return -1 ;
10
+ }
Original file line number Diff line number Diff line change
1
+ char findTheDifference (string s, string t) {
2
+ int alpha1[26 ] = {0 };
3
+ int alpha2[26 ] = {0 };
4
+
5
+ int len1 = s.size ();
6
+ int len2 = t.size ();
7
+ int i = 0 ;
8
+ for (i = 0 ; i < len1; ++i)
9
+ alpha1[s[i] - ' a' ]++;
10
+ for (i = 0 ; i < len2; ++i)
11
+ alpha2[t[i] - ' a' ]++;
12
+ for (i = 0 ; i < 26 ; ++i)
13
+ if (alpha1[i] < alpha2[i])
14
+ break ;
15
+ return i + ' a' ;
16
+ }
Original file line number Diff line number Diff line change
1
+ enum Side {LEFT, RIGHT};
2
+ int sumOfLeftLeaves (TreeNode* root) {
3
+ if (root == NULL )
4
+ return 0 ;
5
+ return dosumOfLeftLeaves (root->left , LEFT) + dosumOfLeftLeaves (root->right , RIGHT);
6
+ }
7
+ int dosumOfLeftLeaves (TreeNode* node, enum Side side)
8
+ {
9
+ if (node == NULL )
10
+ return 0 ;
11
+ else if (node->left == NULL && node->right == NULL )
12
+ return side == LEFT ? node->val : 0 ;
13
+ else
14
+ return dosumOfLeftLeaves (node->left , LEFT) + dosumOfLeftLeaves (node->right , RIGHT);
15
+ }
Original file line number Diff line number Diff line change
1
+ vector<string> fizzBuzz (int n) {
2
+ vector<string> res;
3
+ for (int i = 1 ; i <= n; ++i)
4
+ {
5
+ if (i % 3 == 0 && i % 5 != 0 )
6
+ res.push_back (" Fizz" );
7
+ else if (i % 3 != 0 && i % 5 == 0 )
8
+ res.push_back (" Buzz" );
9
+ else if (i % 15 == 0 )
10
+ res.push_back (" FizzBuzz" );
11
+ else
12
+ res.push_back (int2str (i));
13
+ }
14
+ return res;
15
+ }
16
+
17
+ string int2str (int n)
18
+ {
19
+ string s;
20
+ while (n != 0 )
21
+ {
22
+ s.push_back (n % 10 + ' 0' );
23
+ n /= 10 ;
24
+ }
25
+ reverse (s.begin (), s.end ());
26
+ return s;
27
+ }
Original file line number Diff line number Diff line change
1
+ int findContentChildren (vector<int >& g, vector<int >& s) {
2
+ sort (g.begin (), g.end ());
3
+ sort (s.begin (), s.end ());
4
+
5
+ int len1 = g.size ();
6
+ int len2 = s.size ();
7
+
8
+ int i = 0 , j = 0 ;
9
+ int total = 0 ;
10
+ while (i < len1)
11
+ {
12
+ while (j < len2 && s[j] < g[i])
13
+ ++j;
14
+ if (j == len2)
15
+ break ;
16
+ ++total;
17
+ ++j;
18
+ ++i;
19
+ }
20
+
21
+ return total;
22
+ }
Original file line number Diff line number Diff line change
1
+ int islandPerimeter (vector<vector<int >>& grid) {
2
+ int row = grid.size ();
3
+ if (row == 0 )
4
+ return 0 ;
5
+ int col = grid[0 ].size ();
6
+
7
+ int sum = 0 , cnt = 0 ;
8
+ for (int i = 0 ; i < row; ++i)
9
+ {
10
+ for (int j = 0 ; j < col; ++j)
11
+ {
12
+ if (grid[i][j] == 1 )
13
+ {
14
+ ++sum;
15
+ if (j == 0 && i != row - 1 )
16
+ cnt += grid[i][j] == 1 && grid[i + 1 ][j] == 1 ;
17
+ else if (j != 0 && i == row - 1 )
18
+ cnt += grid[i][j] == 1 && grid[i][j - 1 ] == 1 ;
19
+ else if (j != 0 && i != row - 1 )
20
+ cnt += (grid[i][j] == 1 && grid[i][j - 1 ] == 1 ) + (grid[i][j] == 1 && grid[i + 1 ][j]);
21
+ }
22
+ }
23
+ }
24
+
25
+ return sum * 4 - cnt * 2 ;
26
+ }
Original file line number Diff line number Diff line change
1
+ int findMaxConsecutiveOnes (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ int count = 0 , subcount = 0 ;
4
+ int i = 0 ;
5
+ while (i < len)
6
+ {
7
+ while (i < len && nums[i] == 0 )
8
+ ++i;
9
+ if (i == len)
10
+ {
11
+ if (subcount > count)
12
+ count = subcount;
13
+ break ;
14
+ }
15
+ while (i < len && nums[i] == 1 )
16
+ {
17
+ ++i;
18
+ ++subcount;
19
+ }
20
+ if (subcount > count)
21
+ {
22
+ count = subcount;
23
+ }
24
+ subcount = 0 ;
25
+ }
26
+ return count;
27
+ }
You can’t perform that action at this time.
0 commit comments