您的位置:首页 > 编程语言

leetcode 题解代码整理 6-10题

2015-08-05 15:11 495 查看


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"
.

题意:按之字形

给如图的字符串,和行数N,求横向输出每排字符串的结果
思路:对于每个位置的字符可用公式求出在原字符串中的位置,逐位输出即可,注意(“A”,2)这种数据和空串

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

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

ans="";

int k=0;
int key=2*(numRows-1);
for (int i=0;i<len;i+=key)
ans+=s[i];

int temp=key;
for (int j=1;j<numRows-1;j++)
{
temp-=2;
for (int i=j;i<len;i+=key)
{
ans+=s[i];
if (i+temp<len)
ans+=s[i+temp];
}
}

for (int i=numRows-1;i<len;i+=key)
ans+=s[i];

return ans;

}
};


Reverse
Integer

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321
int型数字反转输出,注意处理翻转后越界的情况
class Solution
{
public:
int reverse(int x)
{
if (overflow(x)==true)
return 0;
int ret=0;
while (x!=0)
{
ret=ret*10+x%10;
x/=10;
}
return ret;
}

private:
bool overflow(int x)
{
if (x/1000000000==0)
return false;
else
if (x == INT_MIN)
return true;

x=abs(x);
for (int cmp=463847412;cmp!=0;cmp/=10,x/=10)
if (x%10>cmp%10)
return true;
else
if (x%10<cmp%10)
return false;
return false;
}
};



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.
模拟atoi函数,注意读入前导空串和非法字符以及超界情况

class Solution
{
public:
int myAtoi(string str)
{
int len,i,flag,j;
long long temp;
len=str.length();
if (len==0)
return 0;

i=0;
while (str[i]==' ' )
i++;

flag=1;
if (str[i]=='+')
i++;
else
if (str[i]=='-')
{
i++;
flag=-1;
}

temp=0;
for (j=i;j<len;j++)
{
if (str[j]<'0' || str[j]>'9') break;
temp=temp*10+str[j]-'0';

if (temp>INT_MAX)
{
if (flag==1) return INT_MAX;
else return INT_MIN;
}
}
temp*=flag;
return (int)temp;

}
};


Palindrome Number

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

判断数字是否为回文串

class Solution {
public:
bool isPalindrome(int x)
{
int n=0;
int num[20];
if (x<0) return false;
while (x!=0)
{
num[n++]=x%10;
x/=10;
}
n--;
int i=0;
while (i<n)
if (num[i++]!=num[n--]) return false;
return true;
}
};


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


正则表达式的匹配,只需要考虑“.'和”*“即可

递归求解

class Solution
{
public:
bool isMatch(string s, string p)
{
if (p.size()==0)
{
if (s.size()==0) return true;
else return false;
}

if (p[1]!='*')
{
if (p[0]==s[0] || (p[0]=='.' && s.size()!=0))
return isMatch(s.substr(1),p.substr(1));
else
return false;
}
else
{
int a=0;
while (p[0]==s[a] || (p[0]=='.' && s.size()!=0))
{
if (isMatch(s.substr(a),p.substr(2)))
return true;
a++;
//   if (a==s.size()) break;
}

return false;
}

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: