Leetcode Week1

来源:互联网 发布:布道者软件 编辑:程序博客网 时间:2024/06/10 17:36
/*
1.将Z型字符串转换成可读类型 
输入 P   A   H   N
A P L S I I G
Y   I   R


输出 PAHNAPLSIIGYIR


例子:convert("PAYPALISHIRING", 3) 输出 "PAHNAPLSIIGYIR".*/


/*

方法1:

class Solution {public:    string res;    string convert(string s, int numRows) {        int n = numRows;        if (n<=1) return s;                int unit = 2*n-2;        int num = s.size() / unit;                for(int i=0;i<n;++i){            for(int j=0;j<=num;++j){                if(j*unit+i<s.size() && (i==0 || i==n-1)){                    res.push_back(s[j*unit+i]);                }else{                    if(j*unit+i < s.size())                        res.push_back(s[j*unit+i]);                    if(j*unit+unit-i<s.size())                        res.push_back(s[j*unit+unit-i]);                }            }        }        return res;    }};

方法2:

class Solution {public:    string res;    string convert(string s, int numRows) {        if (numRows <= 1) return s;                string res;        for(int i=0;i<numRows;++i){            res += convertline(i,numRows,s,i!=numRows-1);        }        return res;    }        string convertline(int num,int nRows,string s, bool downstairs){        string str;        int cur = num;                while(cur<s.size()){            str.push_back(s[cur]);                        if (downstairs){                cur += 2*(nRows-1-cur%(nRows-1));            }else{                cur += 2*(nRows-1-cur%(nRows-1));            }                 };        return str;    }            };

/*
2.转换数字的顺序,reverse
Reverse digits of an integer.


Example1: x = 123, return 321
Example2: x = -123, return -321
*/

方法1:适合数字较小的

class Solution {public:    int reverse(int x) {        char str[1000000];        sprintf(str,"%d",x);        int len = strlen(str);        vector<int> res;        int result = 0;                if (x>=0){            for (int i=len-1;i>=0;--i){                int a = x/(pow(10,i));                x = x-a*pow(10,i);                res.push_back(a);            }                        for (int j=res.size()-1;j>=0;--j){                result += res[j]*pow(10,j);            }                        return result;        }else {            int abs_x = -x;            for (int i=len-2;i>=0;--i){                int a = abs_x/(pow(10,i));                abs_x = abs_x-a*pow(10,i);                res.push_back(a);            }                        for (int j=res.size()-1;j>=0;--j){                result += res[j]*pow(10,j);            }                        return -result;        }                }};

方法2:

class Solution {public:    int reverse(int x) {        bool reverse = false;        if(x<0){            reverse = true;            x = -x;        }                int result = 0;        while(x>0){            int temp = x%10;                        if (INT_MAX/10 < result) return 0;            if (INT_MAX/10 == result && INT_MAX%10<temp) return 0;                        result *= 10;            result += temp;            x /= 10;        }                if(reverse){            return -result;        }else{            return result;        }    }};


0 0