[Leetcode]2942. Find Words Containing Character

2024. 7. 28. 07:52java/javaAlgorithm

1. problem : 

https://leetcode.com/problems/find-words-containing-character/

 

2. solution 1 : 

class Solution {
    public List<Integer> findWordsContaining(String[] words, char x) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            if (words[i].indexOf(x) != -1) {
                result.add(i);
            }
        }
        return result;
    }
}

이 방법은 import java.util.List;를 불러와서 List <Integer> result = new ArrayList <>();를 활용해서, 문제를 풀었다. 기존의 int [] result = new int []로 하면 동적배열이 힘들어져, 이 방법을 쓰는 것이다.