Skip to content

Commit 522243b

Browse files
committed
FEATURE: Create concatenation function & indexing function.
1 parent 7b3bd25 commit 522243b

File tree

1 file changed

+29
-2
lines changed
  • lesson_11/arrays_java/arrays_app/src/main/java/com/codedifferently/lesson11

1 file changed

+29
-2
lines changed
Lines changed: 29 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,40 @@ public class Lesson11 {
910
* https://leetcode.com/problems/concatenation-of-array
1011
*/
1112
public int[] getConcatenation(int[] nums) {
12-
return null;
13+
int n = nums.length; // Puts length of nums case into n.
14+
int[] ans = new int[2 * n]; // Declares ans as an array with 6 values.
15+
16+
// Puts whatever is placed on the index number in each num[i] position and copies it to the
17+
// ans[i] position > depending on what [i] is at the time.
18+
for (int i = 0; i < n; i++) {
19+
ans[i] = nums[i];
20+
}
21+
22+
// Same thing as the last one, but with the starting position being on whatever index n is on.
23+
// In this case, it's starting at index 3, aka, position 4. This is where the concatenation part happens.
24+
for (int i = 0; i < n; i++) {
25+
ans[i + n] = nums[i];
26+
}
27+
// Side note...I'm not sure if this happens after the first 3 numbers have been added, during
28+
// each loop, or after.
29+
30+
return ans;
1331
}
1432

1533
/**
1634
* Provide the solution to LeetCode 2942 here:
1735
* https://leetcode.com/problems/find-words-containing-character/
1836
*/
1937
public List<Integer> findWordsContaining(String[] words, char x) {
20-
return null;
38+
List<Integer> result = new ArrayList<>();
39+
40+
// Iterating through the array.
41+
for (int i = 0; i < words.length; i++) {
42+
if (words[i].indexOf(x) != -1) { // If a particular index contains x, and it does not strictly equal -1 (aka, an index that doesn't exist)...
43+
result.add(i); // Then append the index at the end of the list.
44+
}
45+
}
46+
47+
return result; // Return the index of x in an array.
2148
}
2249
}

0 commit comments

Comments
 (0)