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
//Brute Force for two sum problems | |
class Solution { | |
public: | |
vector<int> twoSum(vector<int>& nums, int target) { | |
vector<int> Result; | |
int len = nums.size(); | |
for(int i = 0; i< len; i++) | |
{ | |
for(int j = i+1; j< len; j++) | |
{ |
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> twoSum(vector<int>& nums, int target) { | |
vector<int> Result; | |
unordered_map<int, int> hash; | |
for(int i =0 ;i< nums.size(); i++) | |
{ | |
int numOfComplement = target - nums[i]; | |
if(hash.find(numOfComplement) != hash.end()) | |
{ |
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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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 isAllUnique(string &s, int start, int end) | |
{ | |
unordered_set<char> hash; | |
for(int i = start; i <= end; i++) | |
{ | |
if(hash.find(s[i]) != hash.end()) | |
return false; | |
hash.insert(s[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: | |
int lengthOfLongestSubstring(string s) { | |
unordered_map<char, int> set; | |
int n = s.size(); | |
int i=0, j=0, maxLength = 0; | |
while(i<n&&j<n) | |
{ | |
if(set.find(s[j])!=set.end()) | |
{ |
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 lengthOfLongestSubstring(string s) { | |
vector<int> dict(256,-1); | |
int maxLength = 0, start = -1; | |
for(int i =0; i<s.size(); i++) | |
{ | |
if(dict[s[i]] > start) | |
start = dict[s[i]]; | |
dict[s[i]] = 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: | |
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { | |
int pre = -1, current = -1; | |
int N1 = nums1.size(), N2 = nums2.size(); | |
int aStart = 0, bStart = 0; | |
int total = N1 + N2; | |
for(int i=0; i<total/2 + 1; i++) | |
{ | |
pre = current; |
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: | |
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { | |
int N1 = nums1.size(), N2 = nums2.size(); | |
int total = N1 + N2; | |
int left = (total+1)/2; | |
int right = (total + 2) / 2; | |
//If the final array is odd, the median will calculate twice | |
return (getKth(nums1, 0, N1, nums2, 0, N2, left) + getKth(nums1, 0, N1, nums2, 0, N2, right))/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
class Solution { | |
public: | |
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { | |
int N1 = nums1.size(), N2 = nums2.size(); | |
if(N1 > N2) | |
return findMedianSortedArrays(nums2, nums1); | |
int iMin = 0, iMax = N1; | |
while(iMin <= iMax) | |
{ |
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 isPalindrome(string &s) | |
{ | |
int len = s.size(); | |
for(int i = 0; i< len/2; i++) | |
{ | |
if(s[i] != s[len - i - 1]) | |
return false; | |
} |
OlderNewer