PS/LeetCode
[LeetCode] Longest Common Prefix
판교토끼
2020. 4. 15. 15:36
728x90
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;
}
}
728x90