This file contains 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
class Solution { | |
public: | |
int longestPalindrome(string s) { | |
unordered_map<int, int> mp; | |
for (char& ch : s) | |
mp[ch]++; | |
bool odd_cnt = false; | |
int res = 0; |
This file contains 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
int search(vector<int> &nums, int target) { | |
// Write your code here. | |
int start =0; | |
int end = nums.size()-1; | |
int mid = start+(end-start)/2; | |
while(start<=end){ | |
if(nums[mid]==target) | |
return mid; |
This file contains 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
class Solution { | |
public: | |
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | |
unordered_set<int> uset(nums1.begin(), nums1.end()); | |
vector<int> res; | |
for (int& num : nums2) { | |
if (uset.find(num) != uset.end()) { | |
res.push_back(num); | |
uset.erase(num); |
This file contains 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
class Solution { | |
public: | |
vector<int> targetIndices(vector<int>& nums, int target) { | |
sort(nums.begin(), nums.end()); | |
int n = nums.size(); | |
vector<int> res; | |
for (int i = 0; i < n; i++) { | |
if (nums[i] == target) |
This file contains 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
// Approach 1 | |
// Complexity: O(n^2) | |
class Solution { | |
public: | |
int countPairs(vector<int>& nums, int target) { | |
int result = 0; | |
for(int i=0; i<nums.size(); i++){ | |
for(int j=i+1; j<nums.size();j++){ | |
if(nums[i]+nums[j]<target){ |
This file contains 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
class Solution { | |
public: | |
vector<string> summaryRanges(vector<int>& nums) { | |
int n = nums.size(); | |
vector<string> res; | |
for (int i = 0; i < n; i++) { | |
int start = nums[i]; |
This file contains 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
class Solution { | |
public: | |
bool isArraySpecial(vector<int>& nums) { | |
int n = nums.size(); | |
if (n == 1) | |
return true; | |
for (int i = 0; i < n - 1; i += 1) { | |
if (nums[i] % 2 == 0 && nums[i + 1] % 2 != 0) |
This file contains 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
// With memorization | |
class Solution { | |
private: | |
int t[101]; | |
int solve(vector<int>& nums, int idx, int& n) { | |
if (idx >= n) | |
return 0; | |
if (t[idx] != -1) { | |
return t[idx]; |
This file contains 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
class Solution { | |
public: | |
vector<int> rightSideView(TreeNode* root) { | |
vector<int> res; | |
if (root == NULL) | |
return {}; | |
queue<TreeNode*> que; | |
que.push(root); |
NewerOlder