Skip to content

Commit 62e56a1

Browse files
committed
feat: add lesson11 Java & Ts HW
1 parent 7b3bd25 commit 62e56a1

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codedifferently.lesson11;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class Lesson11 {
@@ -9,14 +10,27 @@ public class Lesson11 {
910
* https://leetcode.com/problems/concatenation-of-array
1011
*/
1112
public int[] getConcatenation(int[] nums) {
12-
return null;
13+
14+
int[] result = new int[nums.length * 2]; // Create a new array with double the size of nums
15+
for (int i = 0; i < nums.length; i++) {
16+
result[i] = nums[i]; // First half of result array gets the elements from nums
17+
result[i + nums.length] =
18+
nums[i]; // Second half of result array gets the same elements from nums
19+
}
20+
return result;
1321
}
1422

1523
/**
1624
* Provide the solution to LeetCode 2942 here:
1725
* https://leetcode.com/problems/find-words-containing-character/
1826
*/
1927
public List<Integer> findWordsContaining(String[] words, char x) {
20-
return null;
28+
List<Integer> result = new ArrayList<>();
29+
for (int i = 0; i < words.length; i++) {
30+
if (words[i].indexOf(x) >= 0) { // Check if character 'x' is present in the word
31+
result.add(i); // Add the index of the word to the result
32+
}
33+
}
34+
return result;
2135
}
2236
}

lesson_11/arrays_ts/src/lesson11.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33
* https://leetcode.com/problems/concatenation-of-array
44
*/
55
export function getConcatenation(nums: number[]): number[] {
6-
return [];
6+
const result: number[] = new Array(nums.length * 2); // Create a new array of double the length of nums
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
result[i] = nums[i]; // First half of the result array
10+
result[i + nums.length] = nums[i]; // Second half of the result array
11+
}
12+
13+
return result;
714
}
815

916
/**
1017
* Provide the solution to LeetCode 2942 here:
1118
* https://leetcode.com/problems/find-words-containing-character/
1219
*/
1320
export function findWordsContaining(words: string[], x: string): number[] {
14-
return [];
21+
const result: number[] = [];
22+
for (let i = 0; i < words.length; i++) {
23+
if (words[i].includes(x)) { // Check if character 'x' is present in the word
24+
result.push(i); // Add the index of the word to the result
25+
}
26+
}
27+
return result;
1528
}

0 commit comments

Comments
 (0)