Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added .DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions leetcode/leetcode/0001-two-sum/0001-two-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for(int i=0;i<nums.size()-1;i++){
for(int j=i+1;j<nums.size();j++){
if(nums[i]+nums[j]==target){
return{i,j};
}
}
}
return {};

}
};
17 changes: 17 additions & 0 deletions leetcode/leetcode/0001-two-sum/0001-two-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();

int[] ans = {-1,-1};

for(int i = 0;i<nums.length;i++){
if(map.containsKey(target-nums[i])){
ans[0] = i;
ans[1] = map.get(target-nums[i]);
return ans;
}
map.put(nums[i],i);
}
return ans;
}
}
1 change: 1 addition & 0 deletions leetcode/leetcode/0001-two-sum/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
41 changes: 41 additions & 0 deletions leetcode/leetcode/0001-two-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2><a href="https://leetcode.com/problems/two-sum">1. Two Sum</a></h2><h3>Easy</h3><hr><p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>

<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>

<p>You can return the answer in any order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,7,11,15], target = 9
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,4], target = 6
<strong>Output:</strong> [1,2]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,3], target = 6
<strong>Output:</strong> [0,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
<li><strong>Only one valid answer exists.</strong></li>
</ul>

<p>&nbsp;</p>
<strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code><font face="monospace">&nbsp;</font>time complexity?
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] freq = new boolean[256];
int start = 0;
int end = 0;
int max = 0;
while(end<s.length()){
if(freq[(int) s.charAt(end)]){
freq[(int) s.charAt(start)] = false;
start++;
}
else{
freq[(int) s.charAt(end)] = true;
end++;
}
max = Math.max(max,end-start);
}
return max;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h2><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters">3. Longest Substring Without Repeating Characters</a></h2><h3>Medium</h3><hr><p>Given a string <code>s</code>, find the length of the <strong>longest</strong> <span data-keyword="substring-nonempty"><strong>substring</strong></span> without duplicate characters.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcabcbb&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;abc&quot;, with the length of 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bbbbb&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The answer is &quot;b&quot;, with the length of 1.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;pwwkew&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;wke&quot;, with the length of 3.
Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence and not a substring.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>s</code> consists of English letters, digits, symbols and spaces.</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public String longestPalindrome(String s) {
int n = s.length();
boolean[][] dp = new boolean[n][n];
int[] ans = new int[]{0, 0};

for (int i = 0; i < n; i++) {
dp[i][i] = true;
}

for (int i = 0; i < n - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
dp[i][i + 1] = true;
ans[0] = i;
ans[1] = i + 1;
}
}

for (int diff = 2; diff < n; diff++) {
for (int i = 0; i < n - diff; i++) {
int j = i + diff;
if (s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1]) {
dp[i][j] = true;
ans[0] = i;
ans[1] = j;
}
}
}

int i = ans[0];
int j = ans[1];
return s.substring(i, j + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24 changes: 24 additions & 0 deletions leetcode/leetcode/0005-longest-palindromic-substring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h2><a href="https://leetcode.com/problems/longest-palindromic-substring/">5. Longest Palindromic Substring</a></h2><h3>Medium</h3><hr><div><p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre><strong>Input:</strong> s = "babad"
<strong>Output:</strong> "bab"
<strong>Explanation:</strong> "aba" is also a valid answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre><strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> "bb"
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s</code> consist of only digits and English letters.</li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public boolean check(ArrayList<Integer> list){
int low = 0;
int high = list.size()-1;
while(low<high){
if(list.get(low) != list.get(high)){
return false;
}
low++;
high--;
}
return true;
}
public boolean isPalindrome(int x) {
if(x<0) return false;
ArrayList<Integer> list = new ArrayList<>();
while(x!=0){
list.add(x%10);
x = x/10;
}
return check(list);
}
}
1 change: 1 addition & 0 deletions leetcode/leetcode/0009-palindrome-number/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int maxArea(int[] height) {

// Initilization the two pointer
int low = 0;
int high = height.length -1;

int ans = Integer.MIN_VALUE;
while(low<high){
// finding the min height for the water level
int currHeight = Math.min(height[low],height[high]);

// finding the width
int width = high-low;

// calculating the area and updating the maximum area
ans = Math.max(ans,currHeight*width);

// Changing the pointer which has minimum height
if(height[low]>height[high]){
high--;
} else{
low++;
}
}
return ans;
}
}
1 change: 1 addition & 0 deletions leetcode/leetcode/0011-container-with-most-water/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
32 changes: 32 additions & 0 deletions leetcode/leetcode/0011-container-with-most-water/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h2><a href="https://leetcode.com/problems/container-with-most-water">11. Container With Most Water</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>height</code> of length <code>n</code>. There are <code>n</code> vertical lines drawn such that the two endpoints of the <code>i<sup>th</sup></code> line are <code>(i, 0)</code> and <code>(i, height[i])</code>.</p>

<p>Find two lines that together with the x-axis form a container, such that the container contains the most water.</p>

<p>Return <em>the maximum amount of water a container can store</em>.</p>

<p><strong>Notice</strong> that you may not slant the container.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" />
<pre>
<strong>Input:</strong> height = [1,8,6,2,5,4,8,3,7]
<strong>Output:</strong> 49
<strong>Explanation:</strong> The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> height = [1,1]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == height.length</code></li>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= height[i] &lt;= 10<sup>4</sup></code></li>
</ul>
77 changes: 77 additions & 0 deletions leetcode/leetcode/0013-roman-to-integer/0013-roman-to-integer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Solution {
public int romanToInt(String s) {
int ans = 0;
int index = 0;

while(index<s.length()){
char c = s.charAt(index);
if(c == 'I'){
if(index+1<s.length()){
if(s.charAt(index+1) == 'V'){
ans+=4;
index+=2;
}else if(s.charAt(index+1) == 'X'){
ans+=9;
index+=2;
}else {
ans+=1;
index+=1;
}
}else{
ans+=1;
index+=1;
}
}else if(c== 'V'){
ans+=5;
index++;

}else if(c == 'X'){
if(index+1<s.length()){
if(s.charAt(index+1) == 'L'){
ans+=40;
index+=2;
}else if(s.charAt(index+1) == 'C'){
ans+=90;
index+=2;
}else {
ans+=10;
index+=1;
}
}else{
ans+=10;
index+=1;
}

}else if(c == 'L'){
ans+=50;
index++;

}else if(c == 'C'){
if(index+1<s.length()){
if(s.charAt(index+1) == 'D'){
ans+=400;
index+=2;
}else if(s.charAt(index+1) == 'M'){
ans+=900;
index+=2;
}else {
ans+=100;
index+=1;
}
}else{
ans+=100;
index+=1;
}

}else if(c == 'D'){
ans+=500;
index++;

}else if(c == 'M'){
ans+=1000;
index++;
}
}
return ans;
}
}
1 change: 1 addition & 0 deletions leetcode/leetcode/0013-roman-to-integer/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Loading