Skip to content

Commit 9575d95

Browse files
committed
Added solution for 7. Reverse Integer
1 parent 9c262fc commit 9575d95

File tree

8 files changed

+173
-50
lines changed

8 files changed

+173
-50
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
*.swp
2-
2+
*.class
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
1+
Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
22

33

44

5-
Example 1:
5+
Example 1:
66

7-
Input: arr = [5,5,4], k = 1
8-
Output: 1
9-
Explanation: Remove the single 4, only 5 is left.
10-
Example 2:
11-
Input: arr = [4,3,1,1,3,3,2], k = 3
12-
Output: 2
13-
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
7+
Input: arr = [5,5,4], k = 1
8+
Output: 1
9+
Explanation: Remove the single 4, only 5 is left.
10+
Example 2:
11+
Input: arr = [4,3,1,1,3,3,2], k = 3
12+
Output: 2
13+
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
1414

1515

16-
Constraints:
16+
Constraints:
1717

18-
1 <= arr.length <= 10^5
19-
1 <= arr[i] <= 10^9
20-
0 <= k <= arr.length
18+
1 <= arr.length <= 10^5
19+
1 <= arr[i] <= 10^9
20+
0 <= k <= arr.length
2121

2222

23-
## Hint
24-
Imagine you have a bunch of numbers written down in a list (that's your array of integers). Now, someone tells you to pick out some numbers from that list, but you can only take away a certain number of them (that's your k). Your task is to figure out how to take away numbers so that you end up with the fewest different numbers left in your list. So, basically, you're trying to minimize the number of different numbers you have in your list after removing some.
23+
## Hint
24+
Imagine you have a bunch of numbers written down in a list (that's your array of integers).
25+
Now, someone tells you to pick out some numbers from that list, but you can only take away a certain number of them (that's your k).
26+
Your task is to figure out how to take away numbers so that you end up with the fewest different numbers left in your list.
27+
So, basically, you're trying to minimize the number of different numbers you have in your list after removing some.
2528

26-
In order to achieve that we have to keep the numbers with maximum duplicates
29+
In order to achieve that we have to keep the numbers with maximum duplicates
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
You are given an array of positive integers nums of length n.
1+
You are given an array of positive integers nums of length n.
22

3-
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
3+
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
44

5-
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.
5+
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.
66

7-
The perimeter of a polygon is the sum of lengths of its sides.
7+
The perimeter of a polygon is the sum of lengths of its sides.
88

9-
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
9+
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
1010

11-
12-
13-
Example 1:
11+
Example 1:
1412

15-
Input: nums = [5,5,5]
16-
Output: 15
17-
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
18-
Example 2:
13+
Input: nums = [5,5,5]
14+
Output: 15
15+
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
16+
Example 2:
1917

20-
Input: nums = [1,12,1,2,5,50,3]
21-
Output: 12
22-
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
23-
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
24-
It can be shown that the largest possible perimeter is 12.
25-
Example 3:
18+
Input: nums = [1,12,1,2,5,50,3]
19+
Output: 12
20+
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
21+
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
22+
It can be shown that the largest possible perimeter is 12.
23+
Example 3:
2624

27-
Input: nums = [5,5,50]
28-
Output: -1
29-
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
25+
Input: nums = [5,5,50]
26+
Output: -1
27+
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
3028

29+
Constraints:
3130

32-
Constraints:
33-
34-
3 <= n <= 105
35-
1 <= nums[i] <= 109
36-
31+
3 <= n <= 105
32+
1 <= nums[i] <= 109
3733

38-
## Hint
39-
Only the longest side's sum should be smaller than the sum of all the other sides. So looping in reverse order will get the job done.
34+
## Hint
35+
Only the longest side's sum should be smaller than the sum of all the other sides. So looping in reverse order will get the job done.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
5+
public static void main(String[] args) {
6+
String s = "pwwkew";
7+
System.out.println(lengthOfLongestSubstring(s));
8+
}
9+
10+
public static int lengthOfLongestSubstring(String s) {
11+
int pointer_one = 0;
12+
int pointer_two = 0;
13+
int max = 0;
14+
15+
HashSet<Character> hashSet = new HashSet();
16+
17+
while(pointer_two < s.length()){
18+
if(!hashSet.contains(s.charAt(pointer_two))){
19+
hashSet.add(s.charAt(pointer_two));
20+
pointer_two++;
21+
max = Math.max(hashSet.size(), max);
22+
}else{
23+
hashSet.remove(s.charAt(pointer_one));
24+
pointer_one++;
25+
}
26+
}
27+
System.out.println(hashSet);
28+
return max;
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Given a string s, find the length of the longest substring without repeating characters.
2+
3+
Example 1:
4+
5+
Input: s = "abcabcbb"
6+
Output: 3
7+
Explanation: The answer is "abc", with the length of 3.
8+
Example 2:
9+
10+
Input: s = "bbbbb"
11+
Output: 1
12+
Explanation: The answer is "b", with the length of 1.
13+
Example 3:
14+
15+
Input: s = "pwwkew"
16+
Output: 3
17+
Explanation: The answer is "wke", with the length of 3.
18+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
19+
20+
Constraints:
21+
22+
0 <= s.length <= 5 * 104
23+
s consists of English letters, digits, symbols and spaces.

leetcode/medium/5. Longest Palindromic Substring/question renamed to leetcode/medium/5. Longest Palindromic Substring/question.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Constraints:
1818
1 <= s.length <= 1000
1919
s consist of only digits and English letters.
2020

21-
Hint:
22-
There are three ways to solve it
23-
1. O(n3) -> for each of all the substring see if its palindrome or not
24-
2. O(n2) -> Expand From middle Approach
25-
3. O(n) -> Manacher's Algorithm -> Very hard
21+
Hint:
22+
There are three ways to solve it
23+
1. O(n3) -> for each of all the substring see if its palindrome or not
24+
2. O(n2) -> Expand From middle Approach -> Check Nick White's video
25+
3. O(n) -> Manacher's Algorithm -> Very hard
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
3+
public static void main(String[] args) {
4+
int num = 1234567890;
5+
System.out.println(reverse(num));
6+
}
7+
8+
public static int reverse(int x) {
9+
10+
int reversedNum = 0;
11+
int max = Integer.MAX_VALUE;
12+
int min = Integer.MIN_VALUE;
13+
14+
while(x != 0){
15+
int digit = x % 10;
16+
17+
// this is to make sure that the digit we are going to add to the reversedNum doesn't exceed 2147483647
18+
// reversedNum > max/10 -> the current reversedNum shouldn't be greater than 214748364
19+
// If the reversedNUm is equal to 214748364, we have to make sure that the digit is not greater than 7
20+
if(reversedNum > max/10 || (reversedNum == max/10 && digit > max%10))
21+
return 0;
22+
23+
// this is to make sure that the digit we are going to add to the reversedNum doesn't go below -2147483647
24+
if(reversedNum < min/10 || (reversedNum == min/10 && digit < min%10))
25+
return 0;
26+
27+
x = x/10;
28+
reversedNum = reversedNum * 10 + digit;
29+
}
30+
return reversedNum;
31+
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
2+
3+
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
4+
5+
Example 1:
6+
7+
Input: x = 123
8+
Output: 321
9+
Example 2:
10+
11+
Input: x = -123
12+
Output: -321
13+
Example 3:
14+
15+
Input: x = 120
16+
Output: 21
17+
18+
Constraints:
19+
20+
-231 <= x <= 231 - 1
21+
22+
## Hint:
23+
Formula for reversing is
24+
25+
Example:-
26+
```
27+
public static int reverse(int num){
28+
29+
int result = 0;
30+
while(num != 0){
31+
int digit = num % 10;
32+
result = result * 10 + digit;
33+
num = num/10;
34+
}
35+
36+
return result;
37+
}
38+
```

0 commit comments

Comments
 (0)