LeetCode問題解および詳細コメント(継続更新(c/c++実装))

30342 ワード

文書ディレクトリ
  • 間違いがあれば指摘してください.ありがとうございます.
  • 一、文字列タイプ
  • 1、(LeetCode)415.文字列加算
  • 2、文字列反転
  • 3、(LeetCode)917.反転文字
  • のみ
  • 4、(leetcode)387.文字列の最初の一意文字
  • 5、(牛客)文字列の最後の単語の長さを計算し、単語は
  • をスペースで区切る
    間違いがあれば指摘してください.ありがとうございます.
    一、文字列タイプ
    1、(LeetCode)415.文字列加算
    タイトル
    class Solution {
    public:
        int addCarry(int num1, int num2, int &carry){
            int sum = num1 + num2 + carry;
            if(sum >= 10){
                sum -= 10;
                carry = 1;
            } else{
                carry = 0;
            }
            return sum;
        }
        string addStrings(string num1, string num2) {
            //                   
            reverse(num1.begin(),num1.end());
            reverse(num2.begin(),num2.end());
            //        
            int i, j, carry = 0;//carry    
            int sum = 0;
            string result;//    
            while(i < num1.size() && j < num2.size()){
                sum = addCarry(num1[i]-'0',num2[j]-'0',carry);
                result += (sum+'0');
                i++;
                j++;
            }
            //             ,     1 
            while(i<num1.size()){
                sum = addCarry(num1[i]-'0',0,carry);
                result += (sum+'0');
                i++;
            }
            //     2 
            while(j<num2.size()){
                sum = addCarry(0,num2[j]-'0',carry);
                result += (sum+'0');
                j++;
            }
            //    carry 1   :
            if(carry > 0){
                result +=(carry+'0');
            }
            //     
             reverse(result.begin(),result.end());
             return result;
        }
    };
    

    2、文字列の反転
    class Solution {
    public:
    	//      
        void swap(char& a,char& b){
            char temp = a;
            a = b;
            b = temp;
        }
        string reverseOnlyLetters(string S) {
            int len  = S.size();//       
            int left = 0;//       
            int right = len-1;//       
            while(left < right){
                swap(S[left],S[right]);
                left++;
                right--;
            }
            return S;
        }
    };
    

    3、(LeetCode)917.反転文字のみ
    タイトル
    class Solution {
    public:
    	//      
        void swap(char& a,char& b){
            char temp = a;
            a = b;
            b = temp;
        }
        string reverseOnlyLetters(string S) {
            int len  = S.size();//       
            //           1      
            if(len<=1){
                return S;
            }
            int left = 0;//       
            int right = len-1;//       
            //                   
            while(left < right){
                //                   
                while(left<right){
                    //                          ++
                    if((S[left] >= 'a'&& S[left] <='z' ) ||( S[left] >= 'A'&& S[left] <= 'Z' )){
                        break;
                    }else{
                        left++;
                    }
                }
                while(left<right){
                //                          ++
                   if((S[right] >= 'a'&& S[right] <='z' )||(S[right] >= 'A'&& S[right] <= 'Z')){
                        break;
                    }else{
                        right--;
                    }
                }
                swap(S[left],S[right]);//    
                //      
                left++;
                right--;
            }
            return S;
        }
    };
    

    4、(leetcode)387.文字列の最初の一意文字
    タイトル方法1:
    class Solution {
    public:
        int firstUniqChar(string s) {
            int count[256]  = {0};//    256      (          256)
            //                   1        
            for(int i = 0;i<s.size();++i){
                count[s[i]]++;
            }
            //                    count【】 ==1     i
            for(int i = 0;i<s.size();++i){
                if(count[s[i]] == 1){
                    return i;
                }
            }
            return -1;
        }
    };
    

    方法2:下付き文字が等しい場合はreturnを繰り返さない
     for(int i = 0;i<s.size();++i){
                int index = s.find(s[i]);
                int reverseIndex = s.rfind(s[i]);
                if(index == reverseIndex){
                    return i;
                }
            }
            return -1;
    

    5、(牛客)文字列の最後の単語の長さを計算し、単語をスペースで区切る
    タイトル:
    #include
    #include
    using namespace std;
    
    int GetStringLen(string str){
        if(str.size() == 0){
            return 0;
        }
        int spaceIndex = str.rfind(' ');//           
        //                  ,                   
        if(spaceIndex  == string::npos){
            return str.size();
        }
        //                       1            
        return str.size()- spaceIndex -1;
    }
    
    int main(){
        string str;
        getline(cin,str);
        int len = GetStringLen(str);
        cout<<len;
        return 0;
    }