Leetcode--easy系列1
近期開始刷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的更多相关文章
- hdu 2049 不easy系列之(4)——考新郎
不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)
不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu1465不easy系列之中的一个(错排)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/37512659 转载请注明出 ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- leetcode easy problem set
*勿以浮沙筑高台* 持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...
- [Leetcode] Sum 系列
Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...
- 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 ...
- 决战Leetcode: easy part(51-96)
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...
随机推荐
- java线程——详解Callable、Future和FutureTask
回顾: 接上篇博客 java线程--三种创建线程的方式,这篇博客主要介绍第三种方式Callable和Future.比较继承Thread类和实现Runnable接口,接口更加灵活,使用更广泛.但这两种方 ...
- C# mongodb帮助类
这是在C#连接MongoDB的帮助类,所使用的驱动是在Vs2015的Nuget管理器中下载的mongodb驱动. 下载第一个,会自动下载下面的两个,不要删除. 在配置文件中配置连接字符串connStr ...
- 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)
也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...
- [React] Style a React component with styled-components
In this lesson, we remove the mapping between a React component and the styles applied to it via cla ...
- IIS服务器设置http自动跳转https方法
很多站长在部署SSL证书后,网站实现https加密访问,但考虑到用户习惯了http访问,很多外链也是http访问形式,所以需要在IIS服务器配置http自动跳转https,避免用户通过http访问不到 ...
- FragmentPagerAdapter和FragmentStatePagerAdapter的差别
ViewPager同意用户通过左右滑动显示不同页面的数据.而这些页面须要PagerAdapter管理. 经常使用的有FragmentPagerAdapter和FragmentStatePagerAda ...
- DMA 如何查看它有没有传输完成 传输完成再开启
摘自:https://blog.csdn.net/zzwdkxx/article/details/9026173 /////////////////////////////////////////// ...
- VC6.0 MFC中WebBrowser控件禁止新窗口弹出的解决办法
http://blog.csdn.net/gnorth/article/details/7258293 分类: WebBrowser MFC 禁止新窗口2012-02-14 15:25 1787人阅读 ...
- [Angular] Alternative Themes - Learn the Host-Context Selector
To add alernative theme, we can use :host-context() selector from Angular. //au-fa-input-red-theme.c ...
- mac系统创建.开头文件.htaccess
thinkphp5 隐藏index.php的时候需要用的.htaccess文件,但是mac默认不让创建这种文件 感谢 https://blog.csdn.net/gyz413977349/articl ...