技术标签: ZigZag Conversion Palindrome Number String to Integer 数据结构与算法 Reverse Integer leetcode easy
最近开始刷Leetcode题目,花了一个星期先完成了easy难度级别的题目,easy级别的题目思路比较简单,但不一定就直接AC了,主要是问题要考虑完全,特别是对特殊情况的处理。
#6 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"
这个题主要是将一个Z型排列的字符串变为按行排列。关键是:找到位置关系。第一行和最后一行由于奇数列少一个元素,要特别处理。
1输出第一行
2输出中间其他行
3输出最后一行
char* convert(char* s, int numRows) {
int i,j;
int slen=0;
char *ps;
char *temp;
slen=strlen(s);
ps = (char *)malloc(sizeof(char)*(slen+1));
temp = ps;
if(slen==1||numRows==1)
return s;
for(i=0; i<numRows; i++)
{
if(i==0 || i==numRows-1)//第一行或最后一行单独处理
{
for(j = 0; i+j <slen ; )
{
*temp = s[i+j];
temp++;
j = j + 2*(numRows-1);
}
}
else
{ //中间行
for(j = 0;j<slen;j++)
{
//偶数列 (i,j)对应 j*(numRows-1)+i
if( (j&1 == 0) && ((j*(numRows-1)+i) < slen) )
{
*temp = s[j*(numRows-1)+i];
temp++;
}
//奇数列
else if( ( j&1 == 1) && ((numRows-1)*(j+1)-i) < slen )
{
*temp = s[(numRows-1)*(j+1)-i];
temp++;
}
}
}
}
ps[slen]='\0';
return ps;
}
#7 Reverse Interger
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
int reverse(int x) {
int out = 0;
while(x!=0)
{
if( abs(out) > 214748364 )
return 0;
out = out*10 + x%10;<pre name="code" class="cpp">//也可以用if(out/10 != pre_out) return 0;
x /= 10; } return out;}
int reverse(int x) {
int y=(x>0)?x:(-1*x);
int a[10]={0};//依次存储个位 十位。。。最大20亿。。多少位?
int i,len=0;
unsigned int out=0;
while(y!=0)
{
a[len] = y%10;
y=y/10;
if(y!=0)
len++;
}
for(i=0;i<=len;i++)
{
out = out + (int)(a[i]*pow(10,len-i));
if( out > INT_MAX )
return 0;
}
return (x>0)?out:(-1*out);
}
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.
这个题可谓经典,用字符串存储int,输出数字,前前后后写了上十个版本才AC。要考虑的特例情况主要如下:
case | input --string | excepted--int |
1 | “ +1” | 1 |
2 | "+-2" | 0 |
3 | “ 010” | 10 |
4 | “ -0012a42” | -12 |
5 | " +0 123" | 0 |
6 | "99999999999" | 0 |
要考虑的问题有 1 符号位,数分为有符号和无符号数,字符串可能带符号,符号有且只能有一个且只能出现在第一个非空格位置处;
2字符串前面有空格;字符串中间有空格等
3数字越界,无效输入
4必须为数字
/*
*4ms
*/
int myAtoi(char* str) {
int i=0,j=0,r=0;
int flag=1;//符号位 1或-1
int count=0;//符号位个数
int old;
if(str=="")
return 0;
while(str[j]==' ') //前面的空格 不处理
j++;
for(i=j;str[i]!='\0';i++)
{
if(str[i]=='-')
{
flag = -1;
count++;
if(count>1)//连续出现符号位
return 0;
continue;
}
else if(str[i]=='+')
{
count++;
if(count>1)//连续出现符号位
return 0;
continue;
}
else if(str[i]>=48 && str[i]<=57)//数字
{
old = r;
r = r*10 + str[i]-48;
if(r/10 != old)//越界
{
if(flag==-1)
return INT_MIN;
else
return INT_MAX;
}
}
else if((str[i]<48||str[i]>57))//遇到非数字(包含中间的空格)
return flag*r;
}
return flag*r;
}
int myAtoi(char* str) {
int result = 0;
int last_re;
int sign;
if(str == NULL)
return -1;
while(*str==' ')
str++;
sign = (*str == '-')? -1 : 1;
if(*str == '+'|| *str == '-' )
str++;
while(*str >='0' && *str <='9')
{
last_re = result;
result = result * 10 + (*str - '0');
if(result/10 != last_re)
{
if(sign == -1)
return INT_MIN;
else
return INT_MAX;
}
str++;
}
return result*sign;
}
Determine whether an integer is a palindrome. Do this without extra space.
这个题可以利用 题#7反转数字来判断
bool isPalindrome(int x) {
/*
int a = x, h=1;
while (a/h >=10)
h = h*10;
if( a<0 )
return false;
while(a>0)
{
if(a/h != a%10)
return false;
a = a%h; //去掉前面已经比较位
a = a/10; //去掉后面已经比较位
h = h/100; //一次去掉2位,h的长度也减2位
}
return true;
*/
int a = x, r = 0;
if(a<0) //特殊情况
return false;
while(a>0)
{
r = r*10 + a%10;
a = a/10;
}
return r==x;
}
文章浏览阅读1.5w次。AVFrame:( This structure describes decoded (raw) audio or video data. AVFrame must be allocated using av_frame_alloc(). Note that this only allocates the AVFrame itself, the buffers for the data mus_天天av
文章浏览阅读3.5k次,点赞2次,收藏12次。解题思路分析:1.100元兑换10元纸币,可以兑换10张,但每种纸币都要有,所以最多只能兑换9张,最少兑换1张。则初始值为1;循环条件小于10或者小于等于9。2.100元兑换5元纸币,可以兑换20,但每种纸币都要有,所以最多只能兑换19张,最少兑换1张。初始值为1;循环条件小于20或者小于等于19。3.100元兑换1元纸币,可以兑换100张,但每种纸币都要有,所以最多只能兑换99张,最少兑换1张。则初始值为1;循环条件小于100或者小于等于99。_编程把100元换成1元5元10元
文章浏览阅读450次。1、允许用户最多尝试三次2、每尝试三次后,如果还没猜对,就问用户是否继续玩,如果回答Y,y,就继续猜三次,以此往复,如果回答N,n,就直接退出times=0count=3while times<=3:age=int(input(‘请输入年龄:’))if age == 18:print(‘猜对了’)breakelif age > 18:print(‘猜大了’)else:print(‘猜小了’)times+=1if times3:choose = input(‘继续猜Y_找人猜三次年龄
文章浏览阅读152次。这套题实在是太神仙了。。做了我好久。。。好多题都是去搜题解才会的 TAT。剩的那道题先咕着,如果省选没有退役就来填吧。「SDOI2017」龙与地下城题意丢 \(Y\) 次骰子,骰子有 \(X\) 面,每一面的概率均等,取值为 \([0, X)\) ,问最后取值在 \([a, b]\) 之间的概率。一个浮点数,绝对误差不超过 \(0.013579\) 为正确。数据范围每组数据有 \...
文章浏览阅读1.1k次,点赞36次,收藏25次。阅读引言: 本文将会从环境sqlite3的安装、数据库的基础知识、sqlite3命令、以及sqlite的sql语句最后还有一个完整的代码实例, 相信仔细学习完这篇内容之后大家一定能有所收获。
文章浏览阅读51次。UnicodeString templateSavePath = ChangeFileExt(ExtractFilePath(Application->ExeName),"tmp.doc");IdAntiFreeze1->OnlyWhenIdle = false;//设置使程序有反应.TMemoryStream *templateStream ;templateStre..._c++webserver下载文件
文章浏览阅读8.3k次,点赞10次,收藏41次。一、第一天1、创建战舰、侦察潜艇、鱼雷潜艇、水雷潜艇、水雷、深水炸弹类完整代码:package day01;//战舰public class Battleship { int width; int height; int x; int y; int speed; int life; void move(){ System.out.println("战舰移动"); }}package day01;//侦察潜艇_java潜艇大战
文章浏览阅读940次。表单校验的基本步骤_表单校验
文章浏览阅读4.5k次。libOpenBlas.dll缺失依赖解决办法 intellij idea 1.dll文件缺失依赖,报错:“找不到指定模块”2.下载depends查看dll缺失文件3.下载缺失依赖libopenblas.dll出错起因由于java web项目需要调用openBlas库来进行运算,就下载了预编译的libopenblas文件进行调用,首先遇到路径出错问题、之后又是dll文件缺失依赖问题,以下是解决..._libopenblas.dll
文章浏览阅读251次,点赞3次,收藏10次。这里实现的心跳检测机制是一个基础版的,心跳包的主要作用是用于检测用户端是否存活,有助于我们及时判断用户端是否存在断线的问题。在我之前开发过的项目中,有一个基于物联网在线直播抓娃娃的项目,其中就有需要实时监控设备在线状态的需求,该需求就是使用心跳包来实现的。实际上心跳检测技术,应用更广泛的是实时通信、或设备管理的场景偏多。
文章浏览阅读714次。Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.There are 6 scopes available:compileThis is the default scop_maven dependent scope
文章浏览阅读3.6k次。TCP 头部结构信息_tcp头部包含哪些信息