Skip to content

Commit e5976c3

Browse files
committed
1117
1 parent 4f0ab02 commit e5976c3

7 files changed

Lines changed: 113 additions & 8 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Feel free to submit pull requests, add issues and be a contributer.
2525
| Website | Title | Solution | Time | Space | Difficulty | Note|
2626
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
2727
| Careercup | [Permutation Casewise](https://www.careercup.com/question?id=6255535581036544) | [Java](./java/PermuteCasewise.java) | _O(2^n)_ | _O(1)_ | Medium | |
28+
| Leetcode | [50. Pow(x, n)](https://leetcode.com/problems/powx-n/description/) | [Java](./java/myPow.java) | _O(n)_ | _O(1)_ | Medium | |
29+
| Leetcode | [137. Single Number II](https://leetcode.com/problems/single-number-ii/description/) | [Java](./java/singleNumberii.java) | _O(n)_ | _O(1)_ | Medium | |
2830
| Leetcode | [190. Reverse Bits](https://leetcode.com/problems/reverse-bits/description/) | [Java](./java/reverseBits.java) | _O(n)_ | _O(n)_ | Easy | |
2931
| Leetcode | [191. Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/) | [Java](./java/hammingWeight.java) | _O(n)_ | _O(1)_ | Easy | |
3032
| Leetcode | [260. Single Number III](https://leetcode.com/problems/single-number-iii/description/) | [Java](./java/singleNumber.java) | _O(n)_ | _O(1)_ | Medium | |
@@ -64,6 +66,7 @@ Feel free to submit pull requests, add issues and be a contributer.
6466
## Strings
6567
| Website | Title | Solution | Time | Space | Difficulty | Note|
6668
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
69+
| Leetcode | [6. ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/description/) | [Java](./java/ZigZag.java) | _O(n)_ | _O(n)_ | Medium | |
6770
| Leetcode | [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) | [Java](./java/isPalindrome.java) | _O(n)_ | _O(1)_ | Easy | |
6871
| Leetcode | [139. Word Break](https://leetcode.com/problems/word-break/description/) | [Java](./java/wordBreak.java) | _O(n^2)_ | _O(n)_ | Medium | |
6972
| Leetcode | [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/) | [Java](./java/reverseWords1.java) | _O(n)_ | _O(1)_ | Easy | |
@@ -120,6 +123,7 @@ Feel free to submit pull requests, add issues and be a contributer.
120123
| Leetcode | [110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/description/) | [Java](./java/isBalanced.java) | O(n) | O(1) | Easy | |
121124
| Leetcode | [111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) | [Java](./java/minDepth.java) | O(n) | O(1) | Easy | |
122125
| Leetcode | [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) | [Java](./java/preorderTraversal.java) | O(n) | O(n) | Medium | |
126+
| Leetcode | [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/description/) | [Java](./java/containsNearbyAlmostDuplicate.java) | O(nlogk) | O(k) | Medium | |
123127
| Leetcode | [285. Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/description/) | [Java](./java/inorderSuccessor.java) | O(h) | O(1) | Medium | |
124128
| Leetcode | [298. Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/description/) | [Java](./java/longestConsecutive.java) | O(n) | O(n) | Medium | |
125129
| Leetcode | [314. Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/description/) | [Java](./java/verticalOrder.java) | O(n) | O(n) | Medium | |
@@ -178,6 +182,7 @@ Feel free to submit pull requests, add issues and be a contributer.
178182
| Leetcode | [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) | [Java](./java/isValid.java) | _O(n)_ | _O(n)_ | Easy | |
179183
| Leetcode | [155. Min Stack](https://leetcode.com/problems/min-stack/description/) | [Java](./java/MinStack.java) | _O(n)_ | _O(1)_ | Easy | |
180184
| Leetcode | [232. Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/) | [Java](./java/QueueUsingStacks.java) | _O(n)_ | _O(n)_ | Easy | |
185+
| Leetcode | [388. Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/description/) | [Java](./java/lengthLongestPath.java) | _O(n)_ | _O(n)_ | Medium | |
181186
| Leetcode | [496. Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/description/) | [Java](./java/nextGreaterElement.java) | _O(mn)_ | _O(m+n)_ | Easy | |
182187
| Leetcode | [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/description/) | [Java](./java/nextGreaterElements.java) | _O(n)_ | _O(n)_ | Medium | |
183188

@@ -208,6 +213,7 @@ Feel free to submit pull requests, add issues and be a contributer.
208213
## TwoPointers
209214
| Website | Title | Solution | Time | Space | Difficulty | Note|
210215
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
216+
| Leetcode | [11. Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) | [Java](./java/maxArea.java) | _O(n)_ | _O(1)_ | Medium | |
211217
| Leetcode | [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/description/) | [Java](./java/minSubArrayLen.java) | _O(n)_ | _O(1)_ | Medium | |
212218
| Leetcode | [259. 3Sum Smaller](https://leetcode.com/problems/3sum-smaller/description/) | [Java](./java/threeSumSmaller.java) | _O(n)_ | _O(1)_ | Medium | |
213219
| Leetcode | [360. Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/description/) | [Java](./java/sortTransformedArray.java) | _O(n)_ | _O(1)_ | Medium | |

java/ZigZag.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ass Solution {
2+
public String convert(String s, int numRows) {
3+
char [] c = s.toCharArray();
4+
StringBuilder [] sb = new StringBuilder[numRows];
5+
6+
for(int i=0; i<numRows; i++)
7+
sb[i] = new StringBuilder();
8+
9+
int i=0;
10+
while(i < c.length)
11+
{
12+
for(int j=0; j<numRows && i<c.length; j++)
13+
sb[j].append(c[i++]);
14+
for(int j=numRows-2; j>=1 && i<c.length; j--)
15+
sb[j].append(c[i++]);
16+
}
17+
18+
for(int j=1; j<numRows; j++)
19+
sb[0].append(sb[j]);
20+
21+
return sb[0].toString();
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
if(nums == null || t < 0 || k < 0) return false;
4+
5+
TreeSet<Integer> set = new TreeSet<>();
6+
7+
for(int i=0; i<nums.length; i++)
8+
{
9+
if(i-k-1 >= 0)
10+
set.remove(nums[i-k-1]);
11+
int n = nums[i];
12+
13+
if(set.floor(n) != null && n <= t+set.floor(n) || set.ceiling(n) != null && n + t >= set.ceiling(n))
14+
return true;
15+
16+
set.add(n);
17+
}
18+
return false;
19+
}
20+
}

java/lengthLongestPath.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ass Solution {
2+
public int lengthLongestPath(String input) {
3+
4+
if(input == null) return 0;
5+
6+
HashMap<Integer, Integer> map = new HashMap<>();
7+
map.put(0,0);
8+
9+
int result = 0;
10+
11+
for(String s:input.split("\n"))
12+
{
13+
int level = s.lastIndexOf("\t")+1;
14+
int len = s.length() - level;
15+
if(s.contains("."))
16+
result = Math.max(result, map.get(level)+len);
17+
else
18+
map.put(level+1, map.get(level)+len+1);
19+
}
20+
21+
return result;
22+
}
23+
}

java/maxArea.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int left = 0, right = height.length - 1, maxarea = 0;
4+
5+
while(left < right)
6+
{
7+
maxarea = Math.max(maxarea, Math.min(height[left], height[right]) * (right - left));
8+
if(height[left] < height[right])
9+
left++;
10+
else
11+
right--;
12+
}
13+
14+
return maxarea;
15+
}
16+
}

java/myPow.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public double myPow(double x, int n) {
33

4-
if(n==0) return 1;
5-
6-
if(n<0)
4+
double ans = 1;
5+
long absN = Math.abs((long)n);
6+
7+
while(absN > 0)
78
{
8-
n = -n;
9-
x = 1/x;
9+
if((absN & 1) == 1) ans *=x;
10+
absN >>= 1;
11+
x *= x;
1012
}
11-
12-
return n%2 == 0? myPow(x*x, n/2): x*myPow(x*x, n/2);
13-
13+
return n <0 ? 1/ans:ans;
1414
}
1515
}

java/singleNumberii.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int x1=0, x2=0, mask=0;
4+
5+
for(int num:nums)
6+
{
7+
x2 ^= x1 & num;
8+
x1 ^= num;
9+
10+
mask = ~(x1 & x2);
11+
x2 &= mask;
12+
x1 &= mask;
13+
}
14+
15+
return x1;
16+
}
17+
}

0 commit comments

Comments
 (0)