문제로

내가 푼 것

못품

답안1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public String longestCommonPrefix(String[] strs) {

        StringBuilder ans = new StringBuilder();

        Arrays.sort(strs);

        String first = strs[0];
        String last = strs[strs.length-1];
        for(int i = 0; i < Math.min(first.length(), last.length()); i++){
            if(first.charAt(i) != last.charAt(i)) {
                return ans.toString();
            }
            ans.append(first.charAt(i));
        }

        return ans.toString();
    }
}

참고

문서로(java.util.Arrays){: target="_blank"}

1
2
static void sort(int[] a)
// Sorts the specified array into ascending numerical order.

문서로(JLS - Array Members){: target="_blank"}

1
2
3
4
5
6
7
10.7. Array Members (배열 멤버)
배열 타입의 모든 인스턴스는  가지 멤버를 갖는다:

length 필드:
public final length라는 이름의 인스턴스 필드로, 배열의 길이를 나타내는 int 타입의 값을 갖는다.
 필드는 읽기 전용이며, 배열이 생성될  결정된 길이를 나타낸다.
by chatGPT

문서로(java.lang.Math){: target="_blank"}

1
2
static int min(int a, int b)
// Returns the smaller of two int values.

문서로(java.lang.StringBuilder){: target="_blank"}

1
2
3
4
StringBuilder append(char c)
// Appends the string representation of the char argument to this sequence.
String toString()
// Returns a string representing the data in this sequence.