标签:
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 RAnd 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 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 digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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; } };
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; } };
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; } };
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; } } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u011932355/article/details/47297337