๋ฌธ์
์ฝ๋
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans="";
for(int i=0; i<strs[0].length()+1; i++){
string check = strs[0].substr(0,i);
bool flag=true;
for(int k=1; k<strs.size(); k++){
if(strs[k].substr(0,i)!=check){
flag = false;
break;
}
}
if(flag) ans=check;
}
if(strs.size()==1) return strs.front();
return ans;
}
};
ํ์ด
์ฒซ ๋ฒ์งธ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์๋ฅผ ๋ถํ ํด ๊ฐ๋ฉด์ ๋ค๋ฅธ ๋ชจ๋ ๋ฌธ์๋ค์ ์ฒดํฌํ๋๋ฐ, ๋ถํ ํ ๋ฌธ์๊ฐ ๋ค๋ฅธ ๋ชจ๋ ๋ฌธ์๋ค์ ๋ถํ ๊ณผ ๊ฐ๋ค๋ฉด ์ถ๋ ฅํฉ๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Length of Last Word (0) | 2021.11.14 |
---|---|
[Leetcode,c++] Container With Most Water (0) | 2021.11.14 |
[Leetcode,c++] Remove Duplicates from Sorted Array (0) | 2021.11.14 |
[Leetcode,c++] Permutations (0) | 2021.11.10 |
[Leetcode,c++] Best Time to Buy and Sell Stock (0) | 2021.11.10 |
[Leetcode,c++] Climbing Stairs (0) | 2021.11.09 |
๋๊ธ