近期開始刷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. 【Codeforces Round #446 (Div. 2) A】Greed

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long l ...

  2. amazeui学习笔记--css(常用组件14)--缩略图Thumbnail

    amazeui学习笔记--css(常用组件14)--缩略图Thumbnail 一.总结 1.基本样式:在 <img> 添加 .am-thumbnail 类:也可以在 <img> ...

  3. C++组合数(combination)的实现

    实现: (nm) 既需要计算组合的总数 (32)=3: 也需要分别获得每一种组合的情形,用于穷举搜索: 1, 2; 1, 3; 2, 3 1. 递归实现 // picked + toPick == m ...

  4. 3.Docker安装【Docker每天5分钟】

    原文:3.Docker安装[Docker每天5分钟] Docker给PaaS世界带来的“降维打击”,其实是提供了一种非常便利的打包机制.该机制打包了应用运行所需要的整个操作系统,从而保证了本地环境和云 ...

  5. C语言深度剖析-----最终的胜利

    进军C++ 初始OOP   抽象 封装 封装的好处,改名只需改封装 小结 面试题 指针运算 打印11,16,29,28,26 调试经验 printf定义,可变参数无法判断实际参数的类型 安全编程 数组 ...

  6. stm32单片机时钟中断的配置

    原作者:http://www.eeworld.com.cn/mcu/article_2016082828940.html 配置流程:   1:系统时钟初始化,包括系统时钟和要开放的IO口或者功能的时钟 ...

  7. 安装及XShell软件的配置

    Linux系统centOS7在虚拟机下的安装及XShell软件的配置   前面的话 本文将详细介绍Linux系统centOS7在虚拟机下的安装 准备工作 [系统下载] 在安装centOS7之前,首先在 ...

  8. 常用MVC框架

    J2EE开常用的SSH或SSI框架,对应解决表示层.业务逻辑层.持久化层的问题,其中对表示层的解决方案最多,常见的有Struts1/2,Spring MVC等,实际上都是在最底层的Servlet规范中 ...

  9. iOS开发RunLoop学习:四:RunLoop的应用和RunLoop的面试题

    一:RunLoop的应用 #import "ViewController.h" @interface ViewController () /** 注释 */ @property ( ...

  10. javascript中定义事件的三种方式 分类: C1_HTML/JS/JQUERY 2014-08-07 10:27 634人阅读 评论(0) 收藏

    在javascript中,可以为某个元素指定事件,指定的方式有以下三种: 1.在html中,使用onclick属性 2.在javascript中,使用onclick属性 3.在javascipt中,使 ...