leetcode 第6-8题
来源:互联网 发布:自考和网络教育认可度 编辑:程序博客网 时间:2024/04/30 02:24
ZigZag Conversion 该题是个找规律题,每行的字符位置距离呈现周期性(d=2*nRnows-2)变化然后处理好每个周期内的情况即可。代码如下
class Solution {
public:
string convert(string s, int nRows) {
if(nRows==1)return s;
int k,len=s.size();
string result="";
int d=2*nRows-2;
for(int j=0;j<len;j+=d)
result+=s[j];
for(int i=1;i<nRows-1;i++){
k=d-2*(i);
for(int j=i;j<len;j+=d){
result+=s[j];
if(j+k<len)
result+=s[j+k];
}
}
for(int j=nRows-1;j<len;j+=d)
result+=s[j];
return result;
}
};
Reverse Integer 与String to Integer (atoi) 都可以看成是对字符数组与整数之间相互转化,其中atoi 函数,开始写的时候,总是容易或略比如空格这些特殊字符需要处理的情况代码依次如下:
class Solution {
public:
int reverse(int x) {
int flag=1;
string s="";
if(x<0){x=-x;flag=-1;}
int i=0;
while(x)
{
s+=('0'+(x%10));
x=x/10;
i++;
}
for(int i=0;i<s.size();i++){
x=x*10+s[i]-'0';}
return flag*x;
}
};
class Solution {//atoi
public:
int atoi(const char *str) {
long long result=0;int flag=1;int tmp=0;
for(int i=0;i<strlen(str);i++)
{ if(tmp==0){
if(str[i]=='+'||str[i]=='-'||('0'<=str[i]&&str[i]<='9'))tmp=1;
if(str[i]==' '||str[i]=='\n'||str[i]=='\t')continue;
if(i<strlen(str)-1){
if((str[i]=='+')&&'0'<=str[i+1]&&str[i+1]<='9')continue;
if(str[i]=='-'&&'0'<=str[i+1]&&str[i+1]<='9'){flag=-1;continue;}
}
}
if(str[i]=='+'||str[i]=='-'||('0'<=str[i]&&str[i]<='9'))tmp=1;
if('0'<=str[i]&&str[i]<='9')result=result*10+str[i]-'0';
else break;
}
if(result>2147483647&&flag>0) return 2147483647*flag;
if(result>2147483647&&flag<0)return 2147483648*flag;
return flag*result;
}
};
- leetcode 第6-8题
- leetcode第8题
- 【leetcode】第6题:ZigZag
- 【LeetCode】LeetCode——第6题:ZigZag Conversion
- LeetCode 第8题:String to Integer
- [LeetCode 第6题] -- Insertion Sort List
- leetcode第6题—zigZag
- leetcode第6题ZigZag Conversion
- Leetcode 第6题ZigZag Conversion
- Leetcode 第189题
- leetcode第11题
- LeetCode第1题
- LeetCode第2题
- LeetCode第3题
- 第四周leetcode题
- LeetCode 第2题
- Leetcode第38题
- leetcode 第2题
- 2014-第九周项目二
- UVa - 494 - Kindergarten Counting Game 题解
- C++的io学习
- VS2010出现'VMDebuger' failed to load解决方案
- android 使用httpget访问并获取http资源(httpget的使用)
- leetcode 第6-8题
- 数据仓库是什么
- 工作点滴4 - 思科设备环路测试 - Cisco Loopback Tests
- 正规式与有限自动机之间的转换
- TCP大文件传输
- DSP改变网络广告投放模式
- Nginx学习笔记(二十):内存池分析
- 用Java分析C源代码中头文件使用频率
- 链栈的Push和Pop