ice rabbit programming

[LeetCode] Longest Common Prefix 본문

PS/LeetCode

[LeetCode] Longest Common Prefix

판교토끼 2020. 4. 15. 15:36

https://leetcode.com/problems/longest-common-prefix/

[

Longest Common Prefix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

](https://leetcode.com/problems/longest-common-prefix/)

겹치는 가장 긴 prefix를 구하는 문제이다. 단순하게 풀었다. 이 문제도 C#

// C#
public class Solution {
    public string LongestCommonPrefix(string[] strs) {
        if(strs.Length<1)
            return "";
        string result="";
        string source=strs[0];
        for(int i=0;i<source.Length;i++) {
            char cmpChar = source[i];
            for(int j=1;j<strs.Length;j++) {
                if(i>=strs[j].Length)
                    return result;
                if(strs[j][i]!=cmpChar)
                    return result;
            }
            result+=cmpChar;
        }
        return result;
    }
}

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] Maximum Subarray  (0) 2020.04.15
[LeetCode] Valid Parenthese  (0) 2020.04.15
[LeetCode] Remove Element  (0) 2020.04.15
[LeetCode] Remove Duplicates From Sorted Array  (0) 2020.04.15
[LeetCode] 9. Palindrome Number  (0) 2020.04.12