Skip to content

Commit 93c6a9b

Browse files
committed
Merge branch 'main' of https://github.com/neetcode-gh/leetcode into mirvin/move-python-sols-to-dir
2 parents e26371c + 7fc7c8a commit 93c6a9b

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

java/271-Encode-and-Decode-Strings.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@ public class Solution {
22

33
public String encode(List<String> strs) {
44
StringBuilder encodedString = new StringBuilder();
5-
for(String str: strs){
6-
int length = str.length();
7-
encodedString.append(length+"#");
8-
encodedString.append(str);
5+
for (String str : strs) {
6+
encodedString.append(str.length())
7+
.append("#")
8+
.append(str);
99
}
1010
return encodedString.toString();
1111
}
1212

1313
public List<String> decode(String str) {
14-
List<String> decodedStrings = new ArrayList();
15-
for(int i =0;i<str.length();i++){
16-
String length = "";
17-
while(str.charAt(i) != '#'){
18-
length += str.charAt(i);
19-
i++;
20-
}
21-
int wordLength = Integer.parseInt(length);
22-
i++;
2314

24-
String word = "";
25-
for(int j=i;j<wordLength+i;j++){
26-
word += str.charAt(j);
27-
}
28-
decodedStrings.add(word);
29-
i=i+wordLength-1;
15+
List<String> list = new ArrayList<>();
16+
int i = 0;
17+
while (i < str.length()) {
18+
int j = i;
19+
while (str.charAt(j) != '#')
20+
j++;
21+
22+
int length = Integer.valueOf(str.substring(i, j));
23+
i = j + 1 + length;
24+
list.add(str.substring(j + 1, i));
3025
}
31-
return decodedStrings;
26+
return list;
3227
}
3328
}

javascript/739-daily-temperatures.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} temperatures
3+
* @return {number[]}
4+
*/
5+
const dailyTemperatures = function(temperatures) {
6+
const output = Array(temperatures.length).fill(0)
7+
8+
const stack = []
9+
10+
for (let i = 0; i < temperatures.length; i++) {
11+
while (stack.length !== 0 && stack[stack.length - 1][0] < temperatures[i]) {
12+
const [temp, idx] = stack.pop()
13+
output[idx] = i - idx
14+
}
15+
stack.push([temperatures[i], i])
16+
}
17+
18+
return output
19+
};

python/49-Group-Anagrams.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
class Solution:
22
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3-
ans = collections.defaultdict(list)
4-
3+
hashmap = defaultdict(list)
54
for s in strs:
6-
count = [0] * 26
7-
for c in s:
8-
count[ord(c) - ord('a')] += 1
9-
ans[tuple(count)].append(s)
10-
return ans.values()
5+
# keys can be strings, bcz they are immutable.
6+
hashmap[str(sorted(s))].append(s)
7+
return hashmap.values()

typescript/217-Contains-Duplicate.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
const set = new Set();
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (set.has(nums[i])) return true;
6+
else set.add(nums[i]);
7+
}
8+
9+
return false;
10+
}

typescript/242-Valid-Anagrams.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
4+
const store = new Array(26).fill(0);
5+
6+
for (let i = 0; i < s.length; i++) {
7+
store[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
8+
store[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
9+
}
10+
11+
for (let i = 0; i < store.length; i++) {
12+
if (store[i] !== 0) return false;
13+
}
14+
15+
return true;
16+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy