Leetcode--easy系列4
#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的更多相关文章
- 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 ...
随机推荐
- Android中添加自己的模块 【转】
本文转载自:http://wallage.blog.163.com/blog/static/17389624201021791333695/ 转:http://blog.csdn.net/yili_x ...
- ES等待任务——是master节点上的task任务
等待中的任务编辑 有一些任务只能由主节点去处理,比如创建一个新的 索引或者在集群中移动分片.由于一个集群中只能有一个主节点,所以只有这一节点可以处理集群级别的元数据变动.在 99.9999% 的时间里 ...
- Java-杂项: Java中Array和ArrayList区别
ylbtech-Java-杂项: Java中Array和ArrayList区别 1.返回顶部 1. 1)精辟阐述:可以将 ArrayList想象成一种“会自动扩增容量的Array”. 2)Array( ...
- vue-quill-editor 禁止编辑
每天学习一点点,知识财富涨点点 因为权限问题需要对富文本vue-quill-editor进行禁止编辑,因为也不是专业前端,处理起问题来还是只有看文档和百度,发现对这个提问不是很多,可能很多大牛都不会出 ...
- Extjs 常见错误
http://blog.csdn.net/lc448986375/article/details/8082014
- 【转】C# ABP WebApi与Swagger UI的集成
以前在做WebAPI调用测试时,一直在使用Fiddler测试工具了,而且这个用起来比较繁琐,需要各种配置,并且不直观,还有一点是还得弄明白URL地址和要传递的参数,然后才能调用. 最近新入职,公司里 ...
- webapi+DataTables
webapi + datatables 前言 之前写过一个关于DataTables的记录,是之前做webform的时候从后台一次性生成html代码,有很多弊端,就不多说了. 这次把最近研究的DataT ...
- canvas实现刮刮卡效果
canvas实现刮刮卡效果 实现步骤: 设置页面背景图,即刮刮卡底部图片 绘制canvas 刮刮卡顶部图片drawImage 绑定事件 addEventListener touchstart.tou ...
- vue2.0中关于active-class
一.首先,active-class是什么, active-class是vue-router模块的router-link组件中的属性,用来做选中样式的切换: 相关可查阅文档:https://router ...
- js获取浏览器缩放比例
前几天在做项目的时候遇到浏览器缩放比例不为100%时,出来的页面不正常,于是找到了方法获取其比例来通知用户 function detectZoom (){ , screen = window.scre ...