Leetcode- 06-10题 题解

6ZigZag 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) {
        if(numRows == 1) return s;
        int len = s.length(), i = 0, f = 0, h = 0;
        string ans[numRows];
        for(int i = 0; i < numRows; i++) ans[i] = "";
        while(i < len)
        {
            ans[h] += s[i];
            i++;
            if(!f) h++;
            else h--;
            if(h == numRows) f = !f, h -= 2;
            if(h == -1) f = !f, h += 2;
        }
        string res = "";
        for(int i = 0; i < numRows; i++) res += ans[i];
        return res;
    }
};


7 Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output:  321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

题意:求逆序数,注意溢出,溢出返回0

代码:

//求逆序数,注意范围为[-(1<<31), (1<<31)-1]

class Solution {
public:
    int reverse(long long x) {
        long long ans = 0;
        int f = x > 0 ? 1 : -1;
        x *= f;
        while(x)
        {
            ans = ans*10+x%10;
            x/=10;
        }
        long long s = 1ll<<31;
        ans *= f;
        if(ans >= s || ans < -s) return 0;
        return ans;
    }
};
8 String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

题意:给你一个字符串,把它转换成int的数,>= 1<<31 输出 (1<<31)-1, <= -(1<<31)输出  -(1<<31),注意符号问题,总之,这个题很坑~

代码:

//这个题虽然简单,但需要注意读题,很坑啊


class Solution {
public:
    int myAtoi(string str) {
        int len = str.length(), i = 0;
        vector<int> x;
        int f = 0;
        while(i < len)
        {
            if(str[i] == ' ')
            {
                if(f) break;
                i++;
                continue;
            }

            if(str[i] == '+')
            {
                if(f == 0) f = 1;
                else break;
            }
            else if(str[i] == '-')
            {
                if(f == 0) f = -1;
                else break;
            }
            else if(isdigit(str[i]))
            {
                if(f == 0) f = 1;
                x.push_back(str[i] - '0');
            }
            else break;
            i++;
        }
        long long ans = 0, s = 1ll<<31;
        for(int i = 0; i < x.size(); i++)
        {
            ans = ans*10+x[i];
            if(ans >= s) break;
        }

        ans *= f;
        if(ans >= s) return (int)(s-1);
        if(ans < -s) return (int)(-s);
        return (int)ans;
    }
};
9 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

题意:问这个数是不是回文数,不能申请多余的空间,比如申请一个字符串存逆序数。注意负数不是回文数,溢出问题等。

代码:

//负数不是回文数,注意溢出
class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        long long xx = 0,  xxx = x;
        while(x)
        {
            xx = xx*10+x%10;
            x/=10;
        }
        if(xx == xxx) return true;
        return false;
    }
};
10 Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

题意:给你两个字符串,问这两个字符串是否匹配,  ‘.’  可以匹配任何字符, '*'  和它的前面一个字符X,组成一个0个X,或者XXXXXXX...........

解析:倒着匹配

代码:

//借鉴dml学长博客:http://littleblank.net/archives/1045/

class Solution {
public:
    bool check(string s, int i, string p, int j)
    {
        if(i == -1 && j == -1) return true;
        if(j == -1) return false;
        if(i == -1)
        {
            if(!(j&1)) return false;
            for(int k = 0; k <= j; k++)
            {
                if(k&1)
                {
                    if(p[k] != '*') return false;
                }
            }
            return true;
        }

        if(p[j] == '*')
        {
            if(p[j-1] == s[i] || p[j-1] == '.')
            {
                if(check(s, i-1, p, j)) return true;
            }
            return check(s, i, p, j-2);
        }
        if(s[i] == p[j] || p[j] == '.') return check(s, i-1, p, j-1);
        return false;
    }
    bool isMatch(string s, string p) {
        return check(s, s.length()-1, p, p.length()-1);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值