File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed
arrays_java/arrays_app/src/main/java/com/codedifferently/lesson11 Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson11 ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .List ;
4
5
5
6
public class Lesson11 {
@@ -9,14 +10,27 @@ public class Lesson11 {
9
10
* https://leetcode.com/problems/concatenation-of-array
10
11
*/
11
12
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 ;
13
21
}
14
22
15
23
/**
16
24
* Provide the solution to LeetCode 2942 here:
17
25
* https://leetcode.com/problems/find-words-containing-character/
18
26
*/
19
27
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 ;
21
35
}
22
36
}
Original file line number Diff line number Diff line change 3
3
* https://leetcode.com/problems/concatenation-of-array
4
4
*/
5
5
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 ;
7
14
}
8
15
9
16
/**
10
17
* Provide the solution to LeetCode 2942 here:
11
18
* https://leetcode.com/problems/find-words-containing-character/
12
19
*/
13
20
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 ;
15
28
}
You can’t perform that action at this time.
0 commit comments