力扣题-11.18
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:14.最长公共前缀
解题思想:先找到最小的字符串长度,然后进行字符串的遍历即可
class Solution(object):def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""temp=''minlen=len(strs[0])for i in range(len(strs)):if len(strs[i])<minlen:minlen = len(strs[i])for i in range(minlen):now = strs[0][i]flag=1for j in range(len(strs)):if strs[j][i]!=now:flag = 0breakif flag ==1:temp =temp+nowelif flag ==0:breakreturn temp
class Solution {
public:string longestCommonPrefix(vector<string>& strs) {string temp = "";int minlen = strs[0].size();for(int i=0;i<strs.size();i++){if(strs[i].size()<minlen){minlen = strs[i].size();}}for(int i=0;i<minlen;i++){char now = strs[0][i];int flag = 1;for(int j=0;j<strs.size();j++){if(strs[j][i]!=now){flag = 0;break;}}if(flag ==1){temp =temp+now;}else{break;}}return temp;}
};
力扣题2:434.字符串中的单词数
解题思想:进行遍历即可
class Solution(object):def countSegments(self, s):""":type s: str:rtype: int"""temp = s.split()return len(temp)
class Solution {
public:int countSegments(string s) {int result = 0;int flag=0;for(int i=0;i<s.size();i++){if(s[i]==' ' && flag ==1){flag=0;}else if(s[i]!=' ' && flag==0){result++;flag=1;}else if(s[i]!=' '&& flag==1){continue;}else if(s[i]==' ' && flag==0){continue;}}return result;}
};