【LeetCode算法-28/35】Implement strStr()/Search Insert Position
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的更多相关文章
- LeetCode(28)Implement strStr()
题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
- 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导致的溢 ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 【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 ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
随机推荐
- mysql 增加列
alter table t_zhaosheng_chengji_is_finish add season_id int(4) default '1' not null COMMENT '招生季ID';
- 功能更新 | medini analyze — 符合ISO 26262的功能安全平台工具
汽车电子电气系统的功能安全随着智能驾驶.新能源等新兴技术的发展而愈发受到重视.在国际功能安全标准ISO 26262的落地过程中遇到了很多的棘手问题:如何正确而有效地实施HARA以得到合 ...
- datagrid中的排序
sortable的属性设置为true后就能看到标志 属性名称 属性值类型 描述 默认值 sortable boolean 如果为true,则允许列使用排序. undefined order strin ...
- 项目Alpha冲刺(团队)-第八天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第八天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...
- C++定义和初始化数组以及memset的使用(转)
一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...
- LeetCode 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- Why We Changed YugaByte DB Licensing to 100% Open Source
转自:https://blog.yugabyte.com/why-we-changed-yugabyte-db-licensing-to-100-open-source/ 主要说明了YugaByte ...
- 嘉立创制作PCB流程
现在做板子基本上是选择嘉立创和捷配,今天看一下嘉立创如何下PCB和STM贴片单,改天再写一下捷配的下单 我喜欢用下单助手,比较方便 注意需要把自己的板子的PCB文件用压缩软件生成压缩包文件,名字自己取 ...
- 洛谷 题解 P1828 【香甜的黄油 Sweet Butter】
潇洒の开始 第一步:食用头文件和定义变量, 变量干什么用的说的很清楚 #include<iostream> #include<cstdio> #include<cstri ...
- 56、Spark Streaming: transform以及实时黑名单过滤案例实战
一.transform以及实时黑名单过滤案例实战 1.概述 transform操作,应用在DStream上时,可以用于执行任意的RDD到RDD的转换操作.它可以用于实现,DStream API中所没有 ...