刷题之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. ...
随机推荐
- Jquery元素筛选、html()和text()和val三者区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Adapter之GridAdapter
前言: 在我们写界面的时候想让我们展示的页面是网格的,这是我们可以使用GridAdapter,这个和listView的使用有相似之处,如果学过ListView的话还是很简单的 正文: 下面我们来看看G ...
- 51nod 1416:两点 深搜
1416 两点 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 福克斯在玩一款手机解迷游戏,这个游戏叫做" ...
- MinGW下编译curl-7.60.0时, 发生ERROR_FILE_NOT_FOUND undeclared
在编译curl-7.60.0时, 遇到ERROR_FILE_NOT_FOUND undeclared 这个情况, 就没法编译成功!! 下载了以往的版本, 发现是从curl-7.59.0版本开始才有 t ...
- 学习进度05(billbill长评爬取02)
今天下雪了,是个看<白色相簿2>的好日子. 昨天我们获取所有长评url,今天要解析这些url获取更多的信息随便,点开一个,我们需要的数据有标题,时间,内容.点赞数和评论先不弄了. 解析js ...
- Linux gcc(ar命令)打包库到另一个库中的另外一种方法
最近的项目中需要在Libcurl写一个wrapper,最好的办法是把我的wrapper和libcurl包在一起,做一个新的静态库 但是很遗憾,直接用以下命令产生的libmywrapper.a 是不能用 ...
- 【LeetCode 】验证回文串
[问题]给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写.说明:本题中,我们将空字符串定义为有效的回文串. 示例 : 输入: "A man, a plan, a ...
- Python基础笔记:高级特性:切片、迭代、列表生成式、生成器、迭代器
题记: 在python中,代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好. 1行代码能实现的功能,绝不写5行代码. 请始终牢记:代码越少,开发效率越高. 切片 >>&g ...
- c# 多张图片合成一张图片
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- ServletConfig详解
ServletConfig是Servlet中的init()方法的参数类型,服务器会在调用init()方法时传递ServletConfig对象给init()方法. ServletConfig对象封装 ...