LeetCode_String_Easy

String Easy

Zigzag Conversion

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: “PAHNAPLSIIGYIR”

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.

class Solution {
public:
    string convert(string s, int numRows) {
     int len=s.length();
        string res;

        if(numRows==1 || numRows>=len)
        return s;

        string ctmp="";
        int itmp=0;
        int count=0;
        int tt=0;

       for(int i=0;i<numRows;i++){

            count=i;
            itmp =1;

            while(count<len){

                ctmp=s.at(count);
                res.append(ctmp);

                if((i==0) || (i==(numRows-1))){
                    tt=2*(numRows-1);
                }
                else{
                    if(itmp%2==1)
                        tt=2*((numRows-1)-i);
                    else 
                        tt=2*i;
                     itmp++;
                }

                count=count+tt;

            }
        }
        return res;   
    }
};
Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = “11”
b = “1”
Return “100”.

class Solution {
public:
    string addBinary(string a, string b) {
        string res;
        char ts='0';
        int lenA=a.length();
        int lenB=b.length();

        string tts="";
        int diff=abs(lenA-lenB);
        while(diff--){
            if(lenA>lenB)
                b.insert(0,"0");
            else
                a.insert(0,"0");
        }
        int len=max(lenA,lenB);

        while(len>0){
            len--;
            if( a.at(len)!=b.at(len)){
                if(ts=='0'){
                    ts='0';
                    res.insert(0,"1");
                }else if(ts=='1'){
                    ts='1';
                    res.insert(0,"0");
                }           
            }
            else if(a.at(len)==b.at(len) && ts=='1'){
                if(a.at(len)=='0'){
                    ts='0';
                    res.insert(0,"1");
                }
                if(a.at(len)=='1'){
                    ts='1';
                    res.insert(0,"1");
                }
            }else if(a.at(len)==b.at(len) && ts=='0'){
                if(a.at(len)=='0'){
                    ts='0';
                    res.insert(0,"0");
                }
                if(a.at(len)=='1'){
                    ts='1';
                    res.insert(0,"0");
                }
            }
        }

        if(ts=='1')
            res.insert(0,"1");
        return res;   
    }
};
Valid Parentheses

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        for (int i=0;i<s.size();i++){                    
            if ((s[i]=='(') ||(s[i]=='[') ||(s[i]=='{')) {st.push(s[i]);}
            else{   
                if (st.empty()){return false;}
                if ((s[i]==')') && (st.top()!='(')) {return false;}
                if ((s[i]=='{') && (st.top()!='}')) {return false;}
                if ((s[i]=='[') && (st.top()!=']')) {return false;}
                st.pop();
            }

        }
        return st.empty();
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值