Skip to content

Commit 593392a

Browse files
committed
Amazon Interview Question (Longest Subscrting without repeating chars)
1 parent 8848e83 commit 593392a

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

LeetCode/1.two-sum.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=1 lang=cpp
3+
*
4+
* [1] Two Sum
5+
*/
6+
7+
// @lc code=start
8+
#include<bits/stdc++.h>
9+
class Solution {
10+
public:
11+
vector<int> twoSum(vector<int>& nums, int target) {
12+
map<int, int> id;
13+
for(int i = 0; i < nums.size(); i++) {
14+
int make = target - nums[i];
15+
if(id[make]) {
16+
return {id[make]-1, i};
17+
}
18+
id[nums[i]] = i+1;
19+
}
20+
return {-1, -1};
21+
}
22+
};
23+
// @lc code=end
24+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=3 lang=cpp
3+
*
4+
* [3] Longest Substring Without Repeating Characters
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int lengthOfLongestSubstring(string s) {
11+
int n = s.size();
12+
if(s.size()==0)return 0;
13+
int i,j;
14+
i=0, j=0;
15+
vector<int> cnt(326, 0);
16+
cnt[s[0]]++;
17+
int ans=1;
18+
while(1){
19+
if(j==n-1) break;
20+
if(cnt[s[j+1]] == 0) j++, cnt[s[j]]++, ans=max(ans,j-i+1);
21+
else {
22+
cnt[s[i++]]--;
23+
}
24+
}
25+
return ans;
26+
}
27+
};
28+
// @lc code=end
29+

LeetCode/7.reverse-integer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=7 lang=cpp
3+
*
4+
* [7] Reverse Integer
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int reverse(int x) {
11+
long long rev = 0;
12+
long long limit = (1LL<<31);
13+
while(x) {
14+
int d = x%10;
15+
x /= 10;
16+
rev = 10*rev + d;
17+
}
18+
if(rev >= limit or rev < -limit)
19+
return 0;
20+
return rev;
21+
}
22+
};
23+
// @lc code=end
24+

0 commit comments

Comments
 (0)