151.翻转字符串里的单词 卡码网:55.右旋转字符串

81 阅读1分钟

151.翻转字符串里的单词 卡码网:55.右旋转字符串

151.翻转字符串里的单词

题目链接 :

151. 反转字符串中的单词 - 力扣(LeetCode)

Code :

class Solution {
public:
    string reverseWords(string s) {
​
        // 单词 级  翻转  ,  而 不是  单词 内  翻转    
​
        // 栈  
​
        
​
        stack<string> stack_For_Word ;
​
        // 清理
​
        //   / 跳 至  第一个  有效  字符 / 字母    
​
        int i_Work = 0 ;
​
        while(s[i_Work] == ' ' )
        {
            i_Work ++ ;
        }
​
​
        //string str_Cache_For_Word = "" ;
​
        while(s[i_Work] != '\0')
        {
            while(s[i_Work] == ' ' )
            {
                i_Work ++ ;
            }
​
            int Find = 0 ;
​
            string str_Cache_For_Word = "";
​
            while(s[i_Work] != ' ' && s[i_Work] != '\0' )
            {
                str_Cache_For_Word += s[i_Work] ;
​
                i_Work ++ ;
​
                Find = 1 ;
​
            }
​
            if(Find == 1 )
            {
                stack_For_Word.push(str_Cache_For_Word);
                
            }
​
            
​
            // “ 空 串  被 添加 进去 了 ”    
​
            // need  Check  
​
​
​
            // i_Work ++ ;
        }
​
​
        string str_For_Return = "";
​
​
​
        while(stack_For_Word.empty() != true)
        {
            string str_Cache_For_Word = stack_For_Word.top();
​
            str_For_Return += str_Cache_For_Word ;
​
            stack_For_Word.pop();
​
            if(stack_For_Word.empty() != true)
            {
                str_For_Return += " " ;
            }
​
​
        }
​
​
        return str_For_Return ;
​
​
    }
};

卡码网:55.右旋转字符串

题目链接 :

55. 右旋字符串(第八期模拟笔试) (kamacoder.com)

Code :

#include<iostream>
#include<string>
​
using namespace std ; 
​
​
int main()
{
    
    int k ;
​
    string s ;
​
    string str_Receive = "" ;
​
    string str_Cache = "" ;
​
    int ptr_2;
    int ptr_1;
​
    int i ;
​
    int num_Count_s_Length = 0 ; 
    int record_Pace_Walk = 0 ;
​
    cin>>k;
​
    cin>>s;
​
​
    ptr_2 = 0 ;
    ptr_1 = 0 ;
​
    //for(i = 0 ; i < k ; i++ )
    //{
    //  ptr_2 ++ ;
    //}
​
    // “ 算法 ”    
​
    ptr_2 = k ;
​
​
    while( s[ptr_2] != '\0' )
    {
        
        str_Cache += s[ptr_1] ;
​
​
        ptr_2 ++ ;
        ptr_1 ++ ;
        
        record_Pace_Walk ++ ;
​
    
    }
​
    
    num_Count_s_Length = record_Pace_Walk + k ;
​
​
    while( s[ptr_1] != '\0' )
    {
        str_Receive += s[ptr_1] ;
​
        ptr_1 ++ ;
    
    }
​
​
    // or  
​
    //for( i = 0 ; i < k ; i++ )
    //{
    //  str_Receive += s[ptr_1] ;
​
    //}
​
​
    str_Receive += str_Cache ;
​
​
    cout<<str_Receive;
​
​
    
    return 0 ;
​
​
}