LeetCode第28题

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

翻译:

返回大字符串中第一次找到给定小字符串的下标

思路:

String有自带API:indexOf

代码:

class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}

我们来看下SDK源码

public int indexOf(String str) {
return indexOf(str, 0);
} public int indexOf(String str, int fromIndex) {
return indexOf(this, str, fromIndex);
} static int indexOf(String source,
String target,
int fromIndex) {
if (fromIndex >= source.count) {
return (target.count == 0 ? source.count : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (target.count == 0) {
return fromIndex;
} char first = target.charAt(0);
int max = (source.count - target.count); for (int i = fromIndex; i <= max; i++) {
/* Look for first character. */
if (source.charAt(i)!= first) {
while (++i <= max && source.charAt(i) != first);
} /* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + target.count - 1;
for (int k = 1; j < end && source.charAt(j)
== target.charAt(k); j++, k++); if (j == end) {
/* Found whole string. */
return i;
}
}
}
return -1;
}

思路也很简单和暴力,就是遍历对比字符

LeetCode第35题

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

翻译:

给定一个有序数组和一个给定值,在数组中找到这个值所在的下标;如果没有这个值,那么返回他应该插入的下标位置

思路:

和选择排序或插入排序思路相似,就是将定值和已经排好序的数组遍历比较大小

代码:

class Solution {
public int searchInsert(int[] nums, int target) {
int j= 0;
for(int i = 0;i<nums.length;i++){
if(nums[i] < target){
j++;
}
}
return j;
}
}

如果数组元素比给定值要小,那么J就+1;如果数组元素比给定值要大,J不操作;那么下标比J小的元素值就比给定值要小,即J就是我们所需要的下标

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-28/35】Implement strStr()/Search Insert Position的更多相关文章

  1. LeetCode(28)Implement strStr()

    题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...

  2. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

  5. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  6. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  7. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  8. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  9. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

随机推荐

  1. ArrayList 与LinkedList 的区别及分别的优缺点

    ArrayList,与LinkedList都是属于实现了List接口的类.首先从名字前缀开始看  ,Array表示数组,Link表示链表. 所以ArrayList底层是基于动态数组的.而LinkedL ...

  2. VS2005编译QT4.8.2

    为什么要编译? 因为安装安装版的QT4.8.2,vs2005编译报错. 1.下载QT4.8.2,qt-everywhere-opensource-src-4.8.2.zip,下载vs-AddIn1.1 ...

  3. CentOS7.6编译安装redis5.0

    yum install gcc wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xvf redis-5.0.0.tar.gz ...

  4. PC端自适应布局

    截止目前,国内绝大多数内容为主的网站(知乎,果壳,V2EX,网易新闻等)均使用内容区定宽布局,大多数电商网站(网易考拉,京东,聚美优品)也使用了内容区定宽的布局,也有些网站使用了自适应布局: 天猫 内 ...

  5. Spring框架的JDBC模板技术和事物

    Spring框架的JDBC模板技术         技术分析之Spring框架的JDBC模板技术概述  1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单     ...

  6. 一个Tomcat下部署多个项目异常:org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean 的解决方法

    内容简介 在测试服务器上Tomcat下部署两个Spring boot项目,总是一个能启动成功,另一个启动不成功.这两个war包单独部署均能正常启动. 查看日志:启动时报出 org.springfram ...

  7. mssql 清理死锁

    -存储过程 我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句.SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用 ...

  8. Git的个人总结

    Git Git简史: 同生活中的许多伟大事物一样,Git 诞生于一个极富纷争大举创新的年代. Linux 内核开源项目有着为数众多的参与者. 绝大多数的 Linux 内核维护工作都花在了提交补丁和保存 ...

  9. LeetCode 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  10. 数组(定义、遍历、冒泡排序、合并和Join 方法)

    一.数组的定义 1.理解:数组指一组数据,有序的数据,可以一次性存储多个数据,将多个元素(通常统一类型)按照一定的顺序排列放到一个集合里 2.通过构造函数创建数组: var 数组名=new Arrar ...