刷题之Implement strStr()、Climbing Stairs
public class Solution {
public int strStr(String haystack, String needle) {
int big = haystack.length();
int sub = needle.length();
if(big==0 && sub==0) return 0;
if(big==0 && sub!=0) return -1;
int index = big-sub;
for(int i=0;i<=index;i++){
if(haystack.substring(i,sub+i).equals(needle))return i;
}
return -1;
}
}
是int index = haystack.indexOf(needle)的另一种做法
Climbing Stairs
1)递归调用(容易内存溢出):
public class Solution {
public int climbStairs(int n) {
if(n<1)return 0;
if(n==1)return 1;
if(n==2)return 2;
return climbStairs(n-1)+climbStairs(n-2);
}
}
2)类似斐波那契数列,开头1,2.代替1.1
public class Solution {
public int climbStairs(int n) {
if(n<1)return 0;
if(n==1)return 1;
if(n==2)return 2;
int nums = 0;
int one = 1;
int two = 2;
for(int i=3;i<=n;i++){
nums = one+two;
one = two;
two = nums;
}
return nums;
}
}
https://leetcode.com/problems/climbing-stairs/#/description
刷题之Implement strStr()、Climbing Stairs的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
- C#LeetCode刷题之#28-实现strStr()(Implement strStr())
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3895 访问. 实现 strStr() 函数. 给定一个 hays ...
- leetcode第27题--Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leecode刷题(17)-- 实现StrStr
leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
随机推荐
- Mixin类的实现
python类的多重继承由于C3算法的原因导致实现时需要提前规划先后顺序才能正常使用. 这会让人在python中使用多重继承时感到十分的麻烦. 而Mixin类则为我们带来了自由的多重继承和插拔式的舒适 ...
- 公用表表达式(CTE) with as 片段
1.为什么用CTE,而不用表变量, declare @t table(CountryRegionCode nvarchar(3)) insert into @t(CountryRegionCode) ...
- git+jenkins jar包代码的发布加新建项目
1.本地仓库 java开发 把代码上传上来 ,问一下他要上传到的主机ip , 分支 2.本地 , 设置-->仓库 更新数据,让他同步到南阳gitlab, 若没有这个项目,需要创建相同名字的项目 ...
- POJ 1979 Red and Black 四方向棋盘搜索
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 50913 Accepted: 27001 D ...
- Ayoub's function
思维,就是反过来想,题解太强了 #include <bits/stdc++.h> using namespace std; int main() { long long t; cin> ...
- Python测试进阶——(1)安装Python测试相关模块
安装python 安装pip yum -y install epel-release yum -y install python-pip 安装psutil 参考:https://www.cnblogs ...
- BubbleSort
看见了一些乱乱的东西,就想着整理一下,基础的冒泡排序 //BubbleSort #include<iostream> using namespace std; void BubbleSor ...
- python 文本文件操作
文件操作三步走:打开.读写.关闭. open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, close ...
- HDU 4960 Handling the past 2014 多校9 线段树
首先确定的基本思想是按时间离散化后来建线段树,对于每个操作插入到相应的时间点上 但是难就难在那个pop操作,我之前对pop操作的处理是找到离他最近的那个点删掉,但是这样对于后面的peak操作,如果时间 ...
- springcloud--ruul(路由网关)
Zuul的主要功能就是路由转发和过滤器 实例: 1:添加依赖pom.xml: <dependencies> <dependency> <groupId>org.sp ...