刷题之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. ...
随机推荐
- JAVA实现单例模式的四种方法和一些特点
JAVA实现单例模式的四种方法和一些特点,需要的朋友可以参考一下 一.饿汉式单例类 复制代码 代码如下: public class Singleton { private Sing ...
- Vue3中的Proxy作用在哪里?
目录 前言 Vue没有Proxy会怎么样? proxy开始 前言 在讲解Proxy之前,我们有些前置知识点是必要掌握的: Object相关静态函数 Reflect相关静态函数 简单说明知识盲点 名称 ...
- spring中的@Transactional注解
前几天灿哥问我,在做程序的时候,有没有考虑到事务,如果一个函数在中间执行过程中报错了,它会回滚么?我查了一查,spring确实有这样一个注解,能快速帮助我们配置事务管理.下面我就简单介绍一下这个注解. ...
- 机器学习中 为何要使用 独热编码 one-hot
背景 接触tensorflow时,学习到mnist,发现处理数据的时候采取one-hot编码,想起以前搞FPGA状态机遇到过格雷码与独热码. 解析: 将离散型特征使用one-hot编码,确实会让特征之 ...
- 第1节 IMPALA:3、impala软件的下载和linux磁盘的挂载
1. impala安装软件下载: http://archive.cloudera.com/cdh5/repo-as-tarball/5.14.0/ 2. linux磁盘的挂载: [root@node0 ...
- springMVC,spring和Hibernate整合(重要)
springMVC,spring和Hibernate整合 https://my.oschina.net/hugohxb/blog/184715 第一步:搭建一个springmvc工程,需要的jar有: ...
- app1----攻防世界
啥也不说把题目下载下来,在模拟器里运行一下 输入正确的key就是flag 继续下一步分析,可以使用Androidkiller分析,我喜欢使用jeb这里我就使用jeb进行分析 找到MainActivit ...
- Springboot注解使用总结
使用Spring boot已经有段时间了,但是对很多注解的使用经常会遇到模糊甚至不解的地方,这次有时间便总结一下. 注解(Annotation)概念 注解是Java5开始对元数据的支持,注解与注释是有 ...
- Http与Https协议规范
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- Java基础学习总结(二)
Java语言的特点: Java语言是简单的 Java语言是面向对象的 Java语言是跨平台(操作系统)的(即一次编写,到处运行) Java是高性能的 运行Java程序要安装和配置JDK jdk是什么? ...