Skip to content

Commit 5287d6a

Browse files
solves ranges summary
1 parent 7e51b4f commit 5287d6a

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-143/1571-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-143/1571-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-143/1571-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-161/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-161/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-161/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -68,6 +68,7 @@
6868
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | [![Java](assets/java.png)](src/ContainsDuplicateII.java) [![Python](assets/python.png)](python/contains_duplicate_ii.py) |
6969
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | [![Java](assets/java.png)](src/MyStack.java) [![Python](assets/python.png)](python/implement_stack_using_queues.py) |
7070
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | [![Java](assets/java.png)](src/InvertBinaryTree.java) [![Python](assets/python.png)](python/invert_binary_tree.py) |
71+
| 226 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [![Java](assets/java.png)](src/SummaryRanges.java) [![Python](assets/python.png)](python/summary_ranges.py) |
7172
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | [![Java](assets/java.png)](src/PowerOf2.java) [![Python](assets/python.png)](python/is_power_of_2.py) |
7273
| 232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks) | [![Java](assets/java.png)](src/MyQueue.java) [![Python](assets/python.png)](python/implement_queue_using_stacks.py) |
7374
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | [![Java](assets/java.png)](src/PalindromeLinkedList.java) [![Python](assets/python.png)](python/palindrome_linked_list.py) |

Diff for: python/summary_ranges.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def getRangeString(self, start: int, end: int) -> str:
6+
return f'{start}' if start == end else f'{start}->{end}'
7+
8+
def summaryRanges(self, array: List[int]) -> List[str]:
9+
if len(array) == 0: return []
10+
result = []
11+
startIndex, index = 0, 1
12+
while index < len(array):
13+
if array[index] > array[startIndex] + (index - startIndex):
14+
result.append(self.getRangeString(array[startIndex], array[index - 1]))
15+
startIndex = index
16+
index += 1
17+
result.append(self.getRangeString(array[startIndex], array[len(array) - 1]))
18+
return result

Diff for: src/RobotReturnToOrigin.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ public boolean judgeCircle(String moves) {
33
int vertical = 0, horizontal = 0;
44
for (int index = 0 ; index < moves.length() ; index++) {
55
switch (moves.charAt(index)) {
6-
case 'U' -> vertical++;
7-
case 'R' -> horizontal++;
8-
case 'D' -> vertical--;
9-
case 'L' -> horizontal--;
6+
case 'U': vertical++; break;
7+
case 'R': horizontal++; break;
8+
case 'D': vertical--; break;
9+
case 'L': horizontal--; break;
1010
}
1111
}
1212
return vertical == 0 && horizontal == 0;

Diff for: src/SummaryRanges.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class SummaryRanges {
5+
6+
private String getRangeString(int start, int end) {
7+
return end == start ? start + "" : start + "->" + end;
8+
}
9+
10+
public List<String> summaryRanges(int[] array) {
11+
if (array.length == 0) return new ArrayList<>();
12+
List<String> result = new ArrayList<>();
13+
int startIndex = 0;
14+
for (int index = 1 ; index < array.length ; index++) {
15+
if (array[index] > array[startIndex] + (index - startIndex)) {
16+
result.add(getRangeString(array[startIndex], array[index - 1]));
17+
startIndex = index;
18+
}
19+
}
20+
result.add(getRangeString(array[startIndex], array[array.length - 1]));
21+
return result;
22+
}
23+
}

Diff for: src/TwoSumIVInputIsABST.java

-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import java.util.HashSet;
22
import java.util.Set;
33

4-
class TreeNode {
5-
int val;
6-
TreeNode left;
7-
TreeNode right;
8-
TreeNode() {}
9-
TreeNode(int val) { this.val = val; }
10-
TreeNode(int val, TreeNode left, TreeNode right) {
11-
this.val = val;
12-
this.left = left;
13-
this.right = right;
14-
}
15-
}
164

175
public class TwoSumIVInputIsABST {
186
Set<Integer> elements = new HashSet<>();

0 commit comments

Comments
 (0)