Skip to content

Commit b94b4ad

Browse files
authored
Add C++ Solutions (#92)
* Create _461.cpp * refactor 461 * Create _15.cpp * Create _4.cpp * Update README.md added c++ solution for 4 and 15 * Create _3.cpp * Create _7.cpp * refactor readme Add C++ solutions for 3 and 7 * refactor 3.cpp
1 parent c3e4f46 commit b94b4ad

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -978,11 +978,11 @@ _If you like this project, please leave me a star._ ★
978978
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_10.java)||Hard|DP
979979
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_9.java), [C++](../master/cpp/_9.cpp)| | Easy
980980
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_8.java)| |Medium
981-
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_7.java) | [:tv:](https://youtu.be/tm1Yrb_SfBM) |Easy |
981+
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_7.java), [C++](../master/cpp/_7.cpp) | [:tv:](https://youtu.be/tm1Yrb_SfBM) |Easy |
982982
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_6.java) | |Easy |
983983
|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5.java) | |Medium|
984984
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_4.java), [C++](../master/cpp/_4.cpp) | |Hard | Divide and Conquer
985-
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_3.java) | |Medium | HashMap, Sliding Window
985+
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_3.java), [C++](../master/cpp/_3.cpp) | |Medium | HashMap, Sliding Window
986986
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_2.java) | |Medium | LinkedList
987987
|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Java](../master/src/main/java/com/fishercoder/solutions/_1.java), [C++](../master/cpp/_1.cpp), [Javascript](../master/javascript/_1.js)|[:tv:](https://www.youtube.com/watch?v=kPXOr6pW8KM&t=)|Easy| HashMap
988988

Diff for: cpp/_3.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Solution 1: Using Array Frequency Method
2+
3+
class Solution {
4+
public:
5+
int lengthOfLongestSubstring(string s) {
6+
7+
if(s.find_first_not_of(' ') != string::npos){
8+
// There's a non-space && visit array ~ 127 ascii chars.
9+
int visit[150];
10+
memset(visit,0,sizeof(visit));
11+
12+
int mx=0,x=0;
13+
for(int i=0;i<s.size();i++){
14+
if(visit[s[i]]==0){
15+
visit[s[i]]++;
16+
x++;
17+
}else{
18+
mx = max(mx,x);
19+
//back to (i-x) index for relooping from the double occurance
20+
if(x>0) i-=x;
21+
x=0;
22+
memset(visit,0,sizeof(visit));
23+
}
24+
}
25+
mx = max(x,mx);
26+
return mx;
27+
}else{
28+
// There's a only white-spaces.
29+
if(s.size()>0) return 1;
30+
else return 0;
31+
}
32+
33+
}
34+
};
35+
36+
//Solutio 2: Using Map Method (High Memory and Time)
37+
38+
class Solution {
39+
public:
40+
int lengthOfLongestSubstring(string s) {
41+
42+
unordered_map <char,int> ump;
43+
44+
int mx=0,x=0;
45+
for(int i=0;i<s.size();i++){
46+
if(ump[s[i]]==0){
47+
ump[s[i]]++;
48+
x++;
49+
}else{
50+
mx = max(mx,x);
51+
//back to (i-x) index for relooping from the double occurance
52+
if(x>0) i-=x;
53+
x=0;
54+
ump.clear();
55+
}
56+
}
57+
mx = max(x,mx);
58+
return mx;
59+
60+
}
61+
};

Diff for: cpp/_7.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Faster than 100% submissions
2+
3+
class Solution {
4+
public:
5+
int reverse(int x) {
6+
7+
long int res = 0;
8+
9+
bool isMinus = false;
10+
//To add the minus sign if any negative number
11+
if(x<0){
12+
isMinus = true;
13+
}
14+
15+
//convert into positive number
16+
x = abs(x);
17+
18+
//Function to reverse number
19+
while(x>0){
20+
int last = x%10;
21+
res = res*10 + last;
22+
x/=10;
23+
}
24+
25+
//Adding minus sign in the result
26+
if(isMinus){
27+
res *= -1;
28+
}
29+
//Range of int
30+
int mn = -2147483648, mx = 2147483647;
31+
32+
//Checking if is in range of int
33+
if(res > mn and res < mx){
34+
res = res;
35+
}else{
36+
res = 0;
37+
}
38+
39+
return res;
40+
}
41+
};

0 commit comments

Comments
 (0)