#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. [叁]Pomelo游戏server编程分享 之 server结构与配置分析

    网络部署结构 我们先看一下Pomeloserver网络部署情况,直接上图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3RiaW56aQ==/font/ ...

  3. ZooKeeper分布式集群部署及问题

    ZooKeeper为分布式应用系统提供了高性能服务,在许多常见的集群服务中被广泛使用,最常见的当属HBase集群了,其他的还有Solr集群.Hadoop-2中的HA自己主动故障转移等. 本文主要介绍了 ...

  4. Spring-SpringJdbcTemlate配置介绍

    使用spring的jdbcTemplate进一步操作JDBC 一.普通配置  SpringJdbcTemplate连接数据库并操作数据 1.applicationContext.xml 1.1 建立D ...

  5. Ubuntu14.04编译WebRTC For Android代码 2014-07-24

    整整快一年没有写博客了.近期基于Google开源的WebRTC项目做了一款音视频聊天的即时通信项目,期间在下载WebRTC代码时就碰到了一些问题.在此以作记录,也希望可以帮助到正在下载编译WebRTC ...

  6. 在Kali上安装打印机

    在Kali 2.0上安装打印机 最近在玩儿渗透测试,就把自己的办公电脑做成了Kali,可是发现办公室的网络打印机没办法正常使用,上网查了一下,把整个过程简单的记录一下,省的忘记了 1.安装cups a ...

  7. angularjs1-7,http,location

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherlock ...

  9. Vmware 安装samba

    samba是什么samba是什么?能干什么? samba 是基于SMB协议(ServerMessage Block,信息服务块)的开源软件,samba也可以是SMB协议的商标.SMB是一种Linux. ...

  10. 1.matlab基础准备及入门

    1.1 Command Window(命令行窗口)运用入门 1 计算器的用法 2 数值变量与表达式 3. 计算结果的图形表示 代码及注释 function [ output_args ] = Unti ...