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. Centos7源码部署apache/httpd服务

    httpd:是一个提供网站服务的程序 监听端口:80 环境准备: Linux CentOS7.3系统 使用一台服务端,一台客户端即可: 一.安装httpd 1:安装 [root@localhost ~ ...

  2. Python模块(导入,内置,自定义,开源)

    目录: 模块介绍 自定义模块 内置模块 开源模块 一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用p ...

  3. jquery 子元素 后代元素 兄弟元素 相邻元素

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  4. py3+requests+json+xlwt,爬取拉勾招聘信息

    在拉勾搜索职位时,通过谷歌F12抓取请求信息 发现请求是一个post请求,参数为: 返回的是json数据 有了上面的基础,我们就可以构造请求了 然后对获取到的响应反序列化,这样就获取到了json格式的 ...

  5. zabbix-agent主动模式和proxy

    一.zabbix代理模式,缓解服务端压力zabbix_proxy.conf配置如下 more zabbix_proxy.conf | grep -v ^# | grep -v ^$ Server=za ...

  6. Tensorflow细节-P309-高维向量可视化

    import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import os from tensorflow ...

  7. Greenplum 资源队列(转载)

    1.创建资源队列语法 Command:     CREATE RESOURCE QUEUEDescription: create a new resource queue for workload m ...

  8. Redis存储List

    list中数据可以重复,查询快,增删慢. 存储结构: 1.向List存取数据: 查询list中的全部元素: 第一个进入a,第二个进入b,并将a向后移一位. 上图为从右边插入元素的情况. 2.从两头取出 ...

  9. 3-开发共享版APP(接入指南)-设备接入说明:使用隐藏配置

    https://www.cnblogs.com/yangfengwu/p/11273226.html 该APP安装包下载链接: http://www.mnif.cn/appapk/IotDevelop ...

  10. 洛谷 题解 P3984 【高兴的津津】

    潇洒の开始 大水题一枚/小声说话 先吐槽一下: 为什么津津经历了魔鬼训练就可以Au日常QWQ,人家也是早起晚睡的好吧 (谁说魔鬼训练就一定是早起晚睡的) 思路 津津的高兴是持续m天的,但是一旦在这m中 ...