-
Notifications
You must be signed in to change notification settings - Fork 0
Submission for week 2 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
You need to convert all the solution files with |
sorry... my bad. now converted files to .cpp. |
int strStr(string haystack, string needle){ | ||
if(needle == "") return 0; | ||
else { | ||
int haystackLength = haystack.length(); | ||
int needleLength = needle.length(); | ||
for(int i = 0; i <= (haystackLength - needleLength); i++){ | ||
if(haystack[i] == needle[0] && | ||
haystack.substr(i, needleLength) == needle){ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int strStr(string haystack, string needle){ | |
if(needle == "") return 0; | |
else { | |
int haystackLength = haystack.length(); | |
int needleLength = needle.length(); | |
for(int i = 0; i <= (haystackLength - needleLength); i++){ | |
if(haystack[i] == needle[0] && | |
haystack.substr(i, needleLength) == needle){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
} | |
int strStr(string haystack, string needle){ | |
for(int i = 0; i <= (haystack.length()- needle.length()); i++){ | |
if(haystack.substr(i, needleLength) == needle){ | |
return i; | |
} | |
} | |
return -1; | |
} |
It does the same work, but check the readability and concise-ness.
if(strs.size()) == 1){ | ||
return strs[0]; | ||
} | ||
else{ | ||
int numberOfStrings = strs.size(); | ||
string currentLCP = strs[0]; | ||
for(int i = 1; i < numberOfStrings; i++){ | ||
int j; | ||
for(int j = 0; j < strs[i].length(); j++){ | ||
if(strs[i][j] != currentLCP[j]) break; | ||
} | ||
currentLCP = currentLCP.substr(0, j); | ||
} | ||
return currentLCP; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the optimal one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, is the first if
condition necessary? Don't handle special cases too often in every program you write. It's not a good practice.
unsigned long long getHash(string& str){ | ||
unsigned long long hashValue = 1; | ||
for(auto ch: str){ | ||
hashValue = hashValue * (257 + (ch - 'a')); | ||
} | ||
return hashValue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better not to use unsigned long long
. Refer to my this post.
if(numsSize == 1){ | ||
return to_string(nums[0]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, same mistake!
week02/largest_number.md
Outdated
if(numsSize == 1){ | ||
return to_string(nums[0]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again.
for(int i = 0; i < stringLength; i++){ | ||
charCount[s[i] - 'a']++; | ||
} | ||
for(int i = 0; i < stringLength; i++){ | ||
charCount[t[i] - 'a']--; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for(int i = 0; i < stringLength; i++){ | |
charCount[s[i] - 'a']++; | |
} | |
for(int i = 0; i < stringLength; i++){ | |
charCount[t[i] - 'a']--; | |
} | |
for(int i = 0; i < stringLength; i++){ | |
charCount[s[i] - 'a']++; | |
charCount[t[i] - 'a']--; | |
} |
else if(s[i] == ')'){ | ||
if(stackTop == 0) return false; // if stack is already empty return false. | ||
top--; | ||
if(myStack[stackTop] != '(') return false; | ||
myStack.pop_back(); | ||
} | ||
else if(s[i] == '}'){ | ||
if(stackTop == 0) return false; // if stack is already empty return false. | ||
top--; | ||
if(myStack[stackTop] != '{') return false; | ||
myStack.pop_back(); | ||
} | ||
else if(s[i] == ']'){ | ||
if(stackTop == 0) return false; // if stack is already empty return false. | ||
top--; | ||
if(myStack[stackTop] != '[') return false; | ||
myStack.pop_back(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many repetitions! See this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have completed my review. You can find (if any) suggestions in this thread. Feel free to merge this branch with master/main. You should keep this branch too for future reference.
This is submission for week 2.