# LeetCode刷题记录——day7

class Solution {
public:
string reverse(string s){
string ans;
for(int i=s.length()-1;i>=0;i--){
ans=ans+s[i];
}
return ans;
}
string reverseWords(string s) {
string temp = "";
string ans;
int len = s.length();
int began = s.length() - 1, end = 0;
int sign = 1;
while (s[began] == ' ') {
began--;
}
while (s[end] == ' ') {
end++;
}
for(int i=began;i>=end;i--){
if(s[i]!=' '){
temp=temp+s[i];
sign=0;
}
if(s[i]==' '&&sign==0){
ans=ans+reverse(temp)+" ";
temp="";
sign=1;
}
}
ans=ans+reverse(temp);
return ans;
}
};


2、https://leetcode.cn/problems/zigzag-conversion/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
vector<string> temp(numRows);
int len=s.length();
int sign_cir=0,sign_num=0;
for(int i=0;i<len;i++){
if(sign_num<0){
sign_num=1;
sign_cir++;
}else if(sign_num>numRows-1){
sign_num=numRows-2;
sign_cir++;
}
if(sign_cir%2==0){
temp[sign_num]+=s[i];
sign_num++;
}else{
temp[sign_num]+=s[i];
sign_num--;
}
}
string ans;
for(int i=0;i<numRows;i++)
ans+=temp[i];
return ans;
}
};

posted @ 2024-03-27 19:20  想成为编程高手的阿曼  阅读(1)  评论(0编辑  收藏  举报