forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3afb84
commit 0207724
Showing
33 changed files
with
862 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
char * longestCommonPrefix(char ** strs, int strsSize){ | ||
int commonPrefixCount = 0; | ||
int firstStrSize = strlen(strs[0]); | ||
|
||
for(int i = 0; i < firstStrSize; i++) | ||
{ | ||
for(int s = 1; s < strsSize; s++) | ||
{ | ||
if(i == strlen(strs[s]) || strs[0][i] != strs[s][i]) | ||
{ | ||
// Add null terminator after the last common prefix char | ||
strs[0][commonPrefixCount] = '\0'; | ||
return strs[0]; | ||
} | ||
} | ||
|
||
commonPrefixCount++; | ||
} | ||
|
||
return strs[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
typedef struct charToWord { | ||
int ch; /* we'll use this field as the key */ | ||
char* word; | ||
UT_hash_handle hh; /* makes this structure hashable */ | ||
}charToWord; | ||
|
||
typedef struct wordToChar { | ||
char* word; /* we'll use this field as the key */ | ||
int ch; | ||
UT_hash_handle hh; /* makes this structure hashable */ | ||
}wordToChar; | ||
|
||
bool wordPattern(char * pattern, char * s){ | ||
charToWord* charToWordMap = NULL; | ||
wordToChar* wordToCharMap = NULL; | ||
char* word = NULL; | ||
|
||
// Get the first word | ||
word = strtok(s, " "); | ||
|
||
for(size_t i = 0; i < strlen(pattern); i++) | ||
{ | ||
charToWord* charToWordEntry = NULL; | ||
wordToChar* wordToCharEntry = NULL; | ||
int ch = pattern[i]; | ||
|
||
// If there is no words left (pattern > s) | ||
if(word == NULL) | ||
{ | ||
return false; | ||
} | ||
|
||
HASH_FIND_INT(charToWordMap, &ch, charToWordEntry); | ||
HASH_FIND_STR(wordToCharMap, word, wordToCharEntry); | ||
|
||
// If the char does exist in the map and the mapping is not the current word | ||
if(charToWordEntry && strcmp(charToWordEntry->word, word) != 0) | ||
{ | ||
return false; | ||
} | ||
|
||
// If the word does exist in the map and the mapping is not the current char | ||
if(wordToCharEntry && wordToCharEntry->ch != ch) | ||
{ | ||
return false; | ||
} | ||
|
||
/* Setup hash entries */ | ||
charToWordEntry = (charToWord*)malloc(sizeof(charToWord)); | ||
charToWordEntry->ch = ch; | ||
charToWordEntry->word = word; | ||
|
||
wordToCharEntry = (wordToChar*)malloc(sizeof(wordToChar)); | ||
wordToCharEntry->word = word; | ||
wordToCharEntry->ch = ch; | ||
|
||
/* Add entries to the hashes */ | ||
HASH_ADD_INT(charToWordMap, ch, charToWordEntry); | ||
HASH_ADD_STR(wordToCharMap, word, wordToCharEntry); | ||
|
||
// Move to the next word | ||
word = strtok(NULL, " "); | ||
} | ||
|
||
// If there is any words left (s > pattern) | ||
if(word != NULL) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Note: The returned array must be malloced, assume caller calls free(). | ||
*/ | ||
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){ | ||
int* disappearedNumbers = (int*)malloc(numsSize * sizeof(int)); // Allocate space for the worst case | ||
*returnSize = 0; // Points to the first empty index in the array | ||
|
||
// Mark available numbers as negative | ||
for(int i = 0; i < numsSize; i++) | ||
{ | ||
int index = abs(nums[i]); | ||
nums[index - 1] = -1 * abs(nums[index - 1]); | ||
} | ||
|
||
// Find unmarked numbers (disappeared numbers) | ||
for(int i = 0; i < numsSize; i++) | ||
{ | ||
if(nums[i] > 0) | ||
{ | ||
disappearedNumbers[(*returnSize)++] = i + 1; | ||
} | ||
} | ||
|
||
return disappearedNumbers; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#define max(x, y) ((x) > (y) ? (x) : (y)) | ||
|
||
typedef struct hash_entry { | ||
int position; /* we'll use this field as the key */ | ||
int gapCount; | ||
UT_hash_handle hh; /* makes this structure hashable */ | ||
} hash_entry; | ||
|
||
int leastBricks(int** wall, int wallSize, int* wallColSize){ | ||
hash_entry* wallGapCountMap = NULL; | ||
|
||
for(int r = 0; r < wallSize; r++) | ||
{ | ||
int position = 0; | ||
for(int b = 0; b < *(wallColSize + r) - 1; b++) | ||
{ | ||
position += wall[r][b]; | ||
|
||
hash_entry* retrievedMapEntry; | ||
HASH_FIND_INT(wallGapCountMap, &position, retrievedMapEntry); | ||
|
||
// If the position already exists in the map then increment its gap count | ||
if(retrievedMapEntry) | ||
{ | ||
retrievedMapEntry->gapCount += 1; | ||
} | ||
else | ||
{ | ||
// If the position doesn't exist in the map then create a new map entry for it and add it to the map | ||
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry)); | ||
mapEntryToAdd->position = position; | ||
mapEntryToAdd->gapCount = 1; | ||
HASH_ADD_INT(wallGapCountMap, position, mapEntryToAdd); | ||
} | ||
} | ||
} | ||
|
||
int maxGap = 0; | ||
for (hash_entry* retrievedMapEntry = wallGapCountMap; retrievedMapEntry != NULL; retrievedMapEntry = retrievedMapEntry->hh.next) | ||
{ | ||
maxGap = max(maxGap, retrievedMapEntry->gapCount); | ||
} | ||
|
||
return wallSize - maxGap; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int removeDuplicates(vector<int>& nums) { | ||
int left = 1; | ||
|
||
for(int right = 1; right < nums.size(); right++){ | ||
if(nums[right] != nums[right - 1]){ | ||
nums[left] = nums[right]; | ||
left++; | ||
} | ||
} | ||
|
||
return left; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public: | ||
int removeElement(vector<int>& nums, int val) { | ||
int n=nums.size(); | ||
int count=0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
if(nums[i]!=val) | ||
{ | ||
swap(nums[i],nums[count]); | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
cpp/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public: | ||
int strStr(string haystack, string needle) { | ||
if(haystack.size()<needle.size()) return -1; | ||
int found=0; | ||
for(int i=0;i<haystack.size()-needle.size()+1;i++){ | ||
if(haystack[i]==needle[0]){ | ||
found=1; | ||
for(int j=1;j<needle.size();j++){ | ||
if(haystack[i+j]!=needle[j]){ | ||
found=0;break; | ||
} | ||
}if(found==1) return i; | ||
} | ||
}return -1; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution { | ||
public: | ||
string addBinary(string a, string b) { | ||
string res; | ||
int maxLen = a.size() > b.size() ? a.size() : b.size(); | ||
unsigned int carry = 0; | ||
|
||
for(int i = 0; i < maxLen; i++) | ||
{ | ||
unsigned int bitA = i < a.size() ? a[a.size() - i - 1] - '0' : 0; | ||
unsigned int bitB = i < b.size() ? b[b.size() - i - 1] - '0' : 0; | ||
|
||
unsigned int total = bitA + bitB + carry; | ||
char sum = '0' + total % 2; | ||
carry = total / 2; | ||
|
||
// Add to the beginning of the string | ||
res.insert(0, 1, sum); | ||
} | ||
|
||
if(carry) | ||
{ | ||
res.insert(0, 1, '1'); | ||
} | ||
|
||
return res; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
public: | ||
void sortColors(vector<int>& nums) { | ||
int p1=0,p2=nums.size()-1; | ||
for(int i=p1;i<=p2;i++) | ||
{ | ||
if(nums[i]==0) | ||
{ | ||
swap(nums[i],nums[p1]); | ||
p1++; | ||
} | ||
if(nums[i]==2) | ||
{ | ||
swap(nums[i],nums[p2]); | ||
p2--; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public: | ||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
int j=0; | ||
int i=0; | ||
if(n==0) return; | ||
if(m==0) | ||
{ | ||
for(int i = 0; i < n; i++){ | ||
nums1[i] = nums2[i]; | ||
} return; | ||
} | ||
while(i<m) | ||
{ | ||
if(nums1[i]>nums2[j]) | ||
{ | ||
swap(nums1[i],nums2[j]); | ||
sort(nums2.begin(),nums2.end()); | ||
} | ||
i++; | ||
} | ||
j=0; | ||
while(i<m+n) | ||
{ | ||
nums1[i] = nums2[j]; | ||
j++; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/*Given two strings s and t, determine if they are isomorphic. | ||
Two strings s and t are isomorphic if the characters in s can be replaced to get t. | ||
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself. | ||
*/ | ||
class Solution { | ||
public: | ||
bool isIsomorphic(string s, string t) { | ||
unordered_map<char,vector<int>>m1; | ||
unordered_map<char,vector<int>>m2; | ||
for(int i=0;i<s.length();i++){ | ||
m1[s[i]].push_back(i); | ||
m2[t[i]].push_back(i); | ||
|
||
if(m1[s[i]]!=m2[t[i]]) | ||
return false; | ||
} | ||
return true; | ||
|
||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution { | ||
public: | ||
void reverseString(vector<char>& s) { | ||
int left = 0, right = s.size() - 1; | ||
|
||
while (left < right){ | ||
swap(s[left], s[right]); | ||
|
||
left++; | ||
right--; | ||
} | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public: | ||
bool checkPossibility(vector<int>& nums) { | ||
int count = 0; | ||
for(int i=1;i<nums.size();i++){ | ||
if(nums[i-1]>nums[i]){ | ||
count++; | ||
if(i>=2&&nums[i-2]>nums[i]){ | ||
nums[i]=nums[i-1]; | ||
} | ||
else{ | ||
nums[i-1]=nums[i]; | ||
} | ||
} | ||
} | ||
return count<=1; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
private: | ||
static bool st(string &a,string &b){ | ||
if(a.size()==b.size()) return a<b; | ||
return a.size()<b.size(); | ||
} | ||
|
||
|
||
public: | ||
string kthLargestNumber(vector<string>& nums, int k) { | ||
sort(nums.begin(),nums.end(),st); | ||
return nums[nums.size()-k]; | ||
|
||
} | ||
}; |
Oops, something went wrong.