近期開始刷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

这个题我写了例如以下2种方法

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);
}

#8 String  to Interger (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.

这个题可谓经典,用字符串存储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;
}

#9 Palindrome Number

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;
}

然后写它下一次。

Leetcode--easy系列1的更多相关文章

  1. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  3. HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)

    不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  4. hdu1465不easy系列之中的一个(错排)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/37512659 转载请注明出 ...

  5. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  6. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  7. leetcode easy problem set

     *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...

  8. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  9. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  10. 决战Leetcode: easy part(51-96)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. GO语言学习(八)Go 语言常量

    Go 语言常量 常量是一个简单值的标识符,在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型. 常量的定义格式: const identifier ...

  2. 【CS Round #48 (Div. 2 only)】Water Volume

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举0在哪个位置就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> us ...

  3. Linux 内核源代码分析 chap 2 存储管理 (5)

    物理页面分配 linux 内核 2.4 中有 2 个版本号的物理页面分配函数 alloc_pages(). 一个在 mm/numa.c 中, 还有一个在 mm/page_alloc.c 中, 依据条件 ...

  4. Oracle中暂时表空间的清理

    作者:iamlaosong Oracle暂时表空间主要用来做查询和存放一些缓冲区数据. 暂时表空间消耗的主要原因是须要对查询的中间结果进行排序.暂时表空间的主要作用: 索引create或rebuild ...

  5. MVC中url路由规则

    Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法 MapR ...

  6. 每日技术总结:jquery datetimepicker,微博QQ好友QQ空间微信等分享接口

    前言: 1.jquery datetimepicker 今天遇到一个日期控件格式的问题,默认选中显示的并不是设定的值,而是当天的日期.于是去查了一遍文档. 参考文章:jquery datetimepi ...

  7. 37、mipg-streamer的使用讲解

    讲解mjpg-streamer 其功能: 1.控制摄像头采集数据(通过ioctl采集数据,所有不支持CMOS,CMOS之前写驱动的时候是通过read,所有需要修改mjpg-streamer的源码或者C ...

  8. 3、应用层常用lib函数使用说明

    1.mmap函数 void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset); start:映射区的开始地 ...

  9. 21、IIS声卡驱动程序

    声卡芯片的数据通道一般都是IIS接口,但是控制音量等控制信息的接口都不相同 (新内核在linux-3.4.2\sound\soc\codecs\uda134x.c) uda134x_codec_pro ...

  10. [Angular2 Form] Reactive Form, show error message for one field

    <form [formGroup]="reactiveForm" novalidate autocomplete="off"> <div cl ...