문제
문제 설명
- 입력값
- item[i] = [typei, color, namei]
- ruleKey = type
- ruleValue = type의 값*/
- 문제 설명
입력값이 주어질 때 ruleKey와 ruleValue에 맞는 items[] List의 개수를 반환하라. - 예시
- Example 1:
Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
Output: 1
Explanation: There is only one item matching the given rule, which is ["computer","silver","lenovo"]. - Example 2:
Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
Output: 2
Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"] and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.
제약 조건
- 1 <= items.length <= 104
- 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
- ruleKey is equal to either "type", "color", or "name".
- All strings consist only of lowercase letters.
풀이 방법
- List를 순회하며 ruleKey의 값이 ruleValue인 List 찾기
- 찾았다면 ruleValue값과 같은지 비교 후 같다면 result++
if (ruleKey == "type", "color", "name")
then if (item[0].equals(ruleValue), item[1]equals(ruleValue), item[2]equals(ruleValue))
result++
코드
- 내 풀이(O(n))
/** 헷갈리거나 아쉬운 부분
- switch case가 아닌 if else를 사용했음
- item.get().equals(ruleValue)가 아닌 item.get() == ruleValue로 해서 틀림
*/
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int result = 0;
for (List<String> item : items) {
switch (ruleKey) {
case "type":
if (item.get(0).equals(ruleValue))
result++;
break;
case "color":
if (item.get(1).equals(ruleValue))
result++;
break;
case "name":
if (item.get(2).equals(ruleValue))
result++;
break;
}
}
return result;
}
}
헷갈린 부분
- item.get().equals(ruleValue)가 아닌 item.get() == ruleValue를 사용했다.
JAVA에서 문자열은 String이라는 타입을 사용한다. 자바에서 String은 데이터 타입이기 보단 클래스에 가깝다.
그런데 JAVA에서 String은 클래스이기 때문에 참조형이지만, 기본형처럼 직접 할당해서 사용할 수 있다.
아래 예시를 보자
String str1 = "Hello"; // 기본 자료형처럼 직접 할당
String str2 = "Hello"; // 기본 자료형처럼 직접 할당
String str3 = new String("Hello"); // 생성자를 통한 참조형 문자열
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // true
str3는 String 객체를 선언했다. 때문에 str3는 데이터 타입이 아닌 String 클래스의 객체이다.
str1 str2는 new String
이 아닌 기본 자료형처럼 직접 할당을 했다.
직접 할당의 경우 Data를 할당 할 시 JVM 메모리 상수풀이란 곳에 "Hello" 값을 str에 할당한다.
str1이 JVM 상수풀 메모리 공간에 "Hello"를 할당한다. 이후 str2는 새로 메모리를 생성하지 않고 str1이 가리키고 있는 "Hello"값을 가리키게 된다.
결국 str1과 str2는 같은 값이지만 str3는 다른 값이 된다.
오늘 회고
List와 자바에서의 String에 대해 더욱 깊게 알게 되었다.
지금까지는 그냥 막 사용했는데 앞으론 자료형 하나하나 공부하면서 사용해야겠다.
자바.. 역시 복잡해..
'코딩 테스트 > 99클럽' 카테고리의 다른 글
[99클럽] 코테 스터디 32일차 TIL - 정렬(2089. Find Target Indices After Sorting Array) (0) | 2024.06.21 |
---|---|
[99클럽] 코테 스터디 29일차 TIL - 문자열(1528. Shuffle String) (0) | 2024.06.18 |
[99클럽] 코테 스터디 26일차 TIL - 배열(1512. Number of Good Pairs) (0) | 2024.06.15 |
[99클럽] 코테 스터디 23일차 TIL - 그래(1791. Find Center of Star Graph) (0) | 2024.06.14 |
[99클럽] 코테 스터디 25일차 TIL - 배열(1470. Shuffle the Array) (0) | 2024.06.14 |