This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 24 ms, faster than 89.44% | |
//Memory Usage: 21.1 MB, less than 56.68% | |
class Solution { | |
public: | |
TreeNode* sortedArrayToBST(vector<int>& nums) { | |
return sortedArrayToBST(nums,0,nums.size()); | |
} | |
TreeNode* sortedArrayToBST(vector<int>& nums,int start,int end){ | |
if(end <= start) return NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 20 ms, faster than 99.86% | |
//Memory Usage: 20.8 MB, less than 21.53% | |
class Solution { | |
public: | |
bool isValidBST(TreeNode* root) { | |
TreeNode* prev = NULL; | |
return validate(root,prev); | |
} | |
bool validate(TreeNode* node,TreeNode* &prev){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 8 ms, faster than 100.00% | |
//Memory Usage: 13.9 MB, less than 55.99% | |
class Solution { | |
public: | |
vector<vector<int>> levelOrder(TreeNode* root) { | |
vector<vector<int>> result; | |
if(!root) return result; | |
queue<TreeNode*> q; | |
q.push(root); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 8 ms, faster than 100.00% | |
//Memory Usage: 9.7 MB, less than 54.37% | |
class Solution { | |
public: | |
ListNode* removeNthFromEnd(ListNode* head, int n) { | |
ListNode* start = new ListNode(0); | |
start->next = head; | |
ListNode* fast = start; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 12 ms, faster than 100.00% | |
//Memory Usage: 9.2 MB, less than 42.80% | |
class Solution { | |
public: | |
void deleteNode(ListNode* node) { | |
node->val = node->next->val; | |
node->next = node->next->next; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 1632 ms, faster than 5.88% | |
//Memory Usage: 9.6 MB, less than 26.21% | |
class Solution { | |
public: | |
int strStr(string haystack, string needle) { | |
if(needle.length() == 0) return 0; | |
int res; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 8 ms, faster than 99.70% | |
//Memory Usage: 9.5 MB, less than 74.22% | |
class Solution { | |
public: | |
string longestCommonPrefix(vector<string>& strs) { | |
if(strs.size() == 0) return ""; | |
string str; | |
int count = 0; | |
int j = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 4 ms, faster than 100.00% | |
//Memory Usage: 8.6 MB, less than 95.80% | |
class Solution { | |
public: | |
string countAndSay(int n) { | |
string res = "1",cur = "1"; | |
while(--n){ | |
char ch = res[0]; | |
int count = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 8 ms, faster than 99.85% | |
//Memory Usage: 9.1 MB, less than 77.05% | |
class Solution { | |
public: | |
bool isPalindrome(string s) { | |
for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide | |
while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric | |
while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric | |
if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 12 ms, faster than 97.85% | |
//Memory Usage: 9.2 MB, less than 58.97% | |
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if(s.length() != t.length()) return false; | |
int counts[26] = {0}; | |
for(int i = 0;i < s.length();++i){ | |
counts[s[i] - 'a']++; |