#58 Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters '
'
, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, 

Given s = "Hello World",

return 5.

int lengthOfLastWord(char* s) {
int count = 0;
int len = strlen(s);
int i = 0,j = len-1; while(s[j]==' ')//忽略空格
j--;
while(j>=i)
{
if(s[j] != ' ')
count++;
else
break;
j--;
}
return count;
}

#66 Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

给定存储在字符串中的非负数,返回其+1后的结果(字符串形式)

当然字符串存储的特点是方便大数计算。所以是不能先从字符串转换为数字再+1的。

/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int i = digitsSize-1;
int *result;//返回字符串
digits[i]++;//加1
/*假设>=10就进位*/
while(digits[i]>=10 && i>=1)
{
digits[i] = digits[i]-10;
i--;
digits[i]++;
}
/*推断最高位是否产生进位--是否添加字符串长度*/
if(digits[0]>=10)
{
*returnSize = digitsSize+1;
result = (int *)malloc(sizeof(int)*(digitsSize+1));
digits[0] = digits[0]-10;
for(i=0;i<digitsSize;i++)
result[i+1] = digits[i];
result[0] = 1;
}
else
{
*returnSize = digitsSize;
result = digits;
} return result;
}

#67 Add Binary

Given two binary strings, return their sum (also a binary string).

For example,

a = "11"

b = "1"

Return "100".

存储在字符串中的二进制数相加。

以下的代码还能够优化,边相加边进行进位推断。

測试时。a。b採用数组形式,否则假设指向字符串常量,是不容许改动字符串中的值。从而导致错误。

char* addBinary(char* a, char* b) {
int i,j = 0,k = 0;
int len1 = strlen(a);
int len2 = strlen(b);
char *p,*q,*r;
int len_max = (len1>=len2) ? len1:len2;
int len_min = (len1<=len2) ? len1:len2; if(len1 == 0)
return b;
if(len2 == 0)
return a;
//指针p指向 a/b中长度较长的 q指向较短的
if(len1 >= len2)
{
p = a;
q = b;
}
else
{
p = b;
q = a;
}
//p=p+q---先相加----数的低位放在存储的高位
for(j = len_min-1; j >= 0; j--)
{
p[len_max-1-k] += (q[j] - '0');
k++;
}
//推断是否最高位有进位
for(i = len_max-1; i >= 1; i--)
{
if(p[i] >= '2')
{
p[i] -= 2;
p[i-1]++;
}
}
//推断最高位
if( p[0]-'0'<2 )
return p;//位数不变
else
{
p[0] -= 2;//溢出
r = (char *)malloc(sizeof(char)*(len_max+2));
r[0] = '1';//进位
for(i = 1; i <= len_max; i++)
r[i] = p[i-1];
r[len_max+1]='\0';
return r;
}
}

#70 Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

类似这种问题有非常多,如猴子摘桃。走台阶,实质都是裴波那切数列。

f(n) = f(n-1)+f(n-2).

直接使用递归,例如以下。可是提交结果显示 Time Limit Exceeded

int climbStairs(int n) {
if(n==1)
return 1;
if(n==2)
return 2;
if(n>2)
return climbStairs(n-1)+climbStairs(n-2);
}

由于使用递归时,会反复计算。

DP算法就是保存中间结果来避免计算反复子问题。改进例如以下:

int climbStairs(int n) {
int i,*a;
if(n==1)
return 1;
if(n==2)
return 2;
if(n>2)
{
a=(int *)malloc(sizeof(int)*n);
a[0]=1;
a[1]=2;
for(i=2;i<n;i++)
a[i]=a[i-1]+a[i-2];
return a[n-1];
}
}

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

  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. 初探swift语言的学习笔记十(block)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/35783341 转载请注明出处 假设觉得文章对你有所帮助,请通过留言 ...

  2. js+ canvas 实现人物走动

    在网上看了一篇管道工玛利亚走动的图片,感觉人物走动的太生涩了,就写了一下代码改动一下: js 代码: //定义数组图片集合 var marios = new Array("image/QQ截 ...

  3. 调用imagemagick做响应图片

    设计出图后经常需要改下尺寸放在别的项目上使用,每次都是设计手工处理,其实图片服务可以做更多事情,比如借助强大的im,可以通过url控制图片尺寸 var childProcess = require(' ...

  4. shell编程01—shell基础

    01.学习shell编程需要的知识储备 1.vi.vim编辑器的命令,vimrc设置 2.命令基础,100多个命令 3.基础.高端的网络服务,nfs,rsync,inotify,lanmp,sersy ...

  5. Neo4j下执行cypher-shell时,Connection refused问题解决?

    不多说,直接上干货!  问题现象 root@zhouls-/bin# ls cypher-shell neo4j neo4j-admin neo4j-import neo4j-shell tools ...

  6. jsoup HTML parser hello world examples--转

    原文地址:http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/ Jsoup, a HTML parser, its “j ...

  7. 七牛上图片总是net::ERR_NAME_NOT_RESOLVED

    七牛上图片总是net::ERR_NAME_NOT_RESOLVED >> php这个答案描述的挺清楚的:http://www.goodpm.net/postreply/php/101000 ...

  8. XP访问WIN10共享打印机提示错误:操作无法完成,拒绝访问

    XP系统添加打印机--连接到此计算机的本地打印机(取消自动检测)--创建新端口(LOCAL port)----输入端口名\\计算机名\打印机名.(例如:\\adubei\\HP lasjet 1020 ...

  9. Unity中 Animator 与Animation 区别

    ①Animation和Animator 虽然都是控制动画的播放,但是它们的用法和相关语法都是大有不同的.Animation 控制一个动画的播放,而Animator是多个动画之间相互切换,并且Anima ...

  10. ZBrush中Local模式的旋转

    刚接触ZBrush®的小伙伴可能对Local(局部)有了简单的了解,但是大多数人对它的认识还是比较模糊的,那么在本文中小编将对local命令做详细说明.此工具可以控制视图的旋转轴心点的位置,默认情况下 ...