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 ...
随机推荐
- iOS_21团购_地图功能
终于效果图: 右下角的回到用户位置button: MapController控制器, 是主控制器左側dock上面的[地图]button相应的控制器, 继承自ShowDealDetailControll ...
- 怎样通过反编译工具与插件 查看java *.class 文件源代码
Java Decompiler[java 反编译]:开发了反编译工具.能够方便查看*.class 文件源代码.以下介绍几种查看源代码的方式:工具&插件 1.JD-GUI JD-GUI 是显示 ...
- uva 10534 Wavio Sequence LIS
// uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...
- 编写函数int count_number_string(char str[])和函数int maxnum_string(char str[])
题目如图: 这里不再赘述 代码: //字符串中统计与查询 //杨鑫 #include <stdio.h> #include <stdlib.h> #include <st ...
- Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA
简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...
- JAVA设计模式之【原型模式】
1.案例一 学生复制 package Prototype; /** * Created by Jim on 2016/10/1. */ public class Student implements ...
- dubbo 解决既是消费者又是提供者 Duplicate application configs 的问题
首先 有应用A 是 提供者 应用B 来实现既是消费者又是提供者 在应用 B 这边新建两个xml dubbo-consumer.xml 消费者 <!-- 自动扫描注解:通过dubbo实现 - ...
- vue中的事件修饰符
vue提倡的是在方法中只有对数据的处理,所以提供了事件修饰符用于DOM的事件处理,常用的事件修饰符有以下几个: (1). stop:阻止冒泡(通俗讲就是阻止事件向上级DOM元素传递) 点击内层div的 ...
- 「JavaSE 重新出发」05.02 泛型数组列表、包装类
泛型数组列表 ArrayList 是一个采用类型参数(type parameter)的泛型类(generic class). java ArrayList<Employee> staff ...
- 关于css3背景图片渐变的规则
1. Webkit引擎的CSS3径向渐变语法 Webkit引擎下的老版本语法:-webkit-gradient([<type>],[<position> || & ...