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的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  3. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  4. 【leetcode刷题笔记】Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  5. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

  6. C#LeetCode刷题之#28-实现strStr()(Implement strStr())

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3895 访问. 实现 strStr() 函数. 给定一个 hays ...

  7. leetcode第27题--Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  8. leecode刷题(17)-- 实现StrStr

    leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...

  9. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

随机推荐

  1. 前端学习笔记系列一:5 在项目中引入阿里图标icon

    进入到阿里的图标库网站,里面有上百万种icon,https://www.iconfont.cn,需要注册一个帐号,然后进入到这个页面,在这里点击右下角的带加号的图标,创建一个新的项目,名称与你要使用图 ...

  2. jdk环境

    安装方式一  jdk环境 干净的环境 将tar包解压到 /usr/local下 版本为jdk-8u211-linux-x64.tar.gz  路径为/usr/local/jdk1.8.0_211 /u ...

  3. 在React中随机生成图形验证码

    各个方法 在输入框中定义一个位置存放图形 完整代码 方便复制粘贴 import React, { Component } from 'react'; import styles from './lef ...

  4. sass计算高度

    页面布局时,有时候需要两个div充满父div空间,设定一个div尺寸后,可以使用css计算高度设置另一个尺寸: <style> .wrap{ width:1000px; } .left{ ...

  5. Matplotlib 教程

    Matplotlib是python中最流行的数据绘图库,使用matplotlib,您可以绘制任何类型的图形. 本教程的目标是让您轻松学会使用matplotlib绘制复杂的图形. 预备知识 熟悉Pyth ...

  6. firewalld学习-zone的使用和配置

    原文地址:http://www.excelib.com/article/290/show zone文件标签名词解释 target:目标,这个前面学生也已经给大家介绍过了,可以理解为默认行为,有四个可选 ...

  7. C++面试常见问题——10派生类的构造与析构

    派生类的构造与析构 派生类的构造 派生时构造函数与析构函数不会被继承,需要重新定义派生类的构造函数与析构函数.派生类对象包含了基类对象的值,创建派生类时首先会调用基类的构造函数,若派生类中含有其它类对 ...

  8. JS取整方法

    1.toFixed方法 定义:toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 例如:将数据Num保留2位小数,则表示为:toFixed(Num):但是其四舍五入的规则与数学 ...

  9. 002.Delphi插件之QPlugins,菜单插件

    运行之后的效果如下, 图一 图二 主界面代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages, System.SysUt ...

  10. Problem J. Joseph’s Problem 约瑟夫问题--余数之和

    链接:https://vjudge.net/problem/UVA-1363 题意:给出n  k,当 i 属于 1~n 时 ,求解 n% i 的和 n 和 k 的范围都是 1 到 10^9; 商相同 ...