코린이의 소소한 공부노트

[LeetCode/Easy] 2496. Maximum Value of a String in an Array 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2496. Maximum Value of a String in an Array

무지맘 2023. 5. 5. 23:37

1. Input

1) String[] strs

 

2. Output

1) strs의 각 요소에 대해 다음과 같은 값을 계산했을 때 가장 큰 값을 반환

- 문자열이 숫자로만 이루어져 있다면 해당 문자열의 10진수가 문자열의 값이 된다.

- 그 외의 문자열은 문자열의 길이가 값이 된다.

 

3. Constraint

1) 1 <= strs.length <= 100

2) 1 <= strs[i].length <= 9

3) strs의 요소는 영어 소문자와 숫자들로 이루어져 있다.

 

4. Example

Input: strs = ["alic3","bob","3","4","00000"] -> Output: 5

설명:

- "alic3": 섞임 -> 길이 = 5

- "bob": 문자만 -> 길이 = 3

- "3": 숫자 -> 3

- "4“: 숫자 -> 4

- "00000": 숫자 -> 0

- 따라서 가장 큰 5를 반환한다.

 

5. Code

1) 첫 코드(2023/05/05)

class Solution {
    public int maximumValue(String[] strs) {
        int max = 0;
        for(int i=0 ; i<strs.length ; i++){
            if(strs[i].matches("[0-9]+"))
                max = Math.max(max, Integer.valueOf(strs[i]));
            else
                max = Math.max(max, strs[i].length());
        }
        return max;
    }
}