【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 ...
随机推荐
- 微信小程序转义解析渲染html
今天开发小程序时,想调用商品详情字段,发现大部分是用编辑器编辑的html原生标签,无法在小程序直接使用. 后面自己使用正则和字符串替换,效果也不佳. 最后在网上找到了wx-mina-html-view ...
- 持久化JS存储
<script src="../../lib/persist-min.js"></script> //测试一下本地化存储器 var store = new ...
- UDS诊断学习笔记
定义介绍: UDS(Unified Diagnostic Service)诊断的诊断服务.诊断协议是面向整车所有ECU(电控单元)的一种诊断通信协议,是诊断服务的规范化标准. UDS本质上是一系列服务 ...
- E. Little Pony and Expected Maximum(组合期望)
题目描述: Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megaby ...
- 2019安徽省程序设计竞赛 I.你的名字(序列自动机)
这题和今年南昌邀请网络预选赛M题很像啊,不过主串数量不是一个了 都是在主串中判断子串是不是属于主串的一个子序列 #include <iostream> #include <cstri ...
- win10设置以管理员身份开机启动
首先是右键程序,然后设置了管理员权限启动.但是在这样设置之后原先的开机机启动就失效了. 在谷歌之后发现有人通过计划任务开机启动.于是就试了试.别人设置的是用户登录时,我改成了开机就启动.就是这样一改, ...
- modbus-poll和modbus-slave工具的学习使用——环境搭建
在modbus的学习工具中,非modbus-poll和modbus-slave莫属了,在电脑上模拟的过程中,两者缺一不可 ,当然还需要虚拟串口工具:Configure Virtual Serial P ...
- 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛
Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...
- HDU-5215 Cycle(边双/判奇偶环)
题目 HDU-5215 Cycle 网上那个啥dfs的垃圾做法随便弄组数据已经hack掉了 做法 纯奇环偶环通过dfs树上,染色判断(由于偶环可能有两个奇环,通过一点相交,dfs树上并不能判完) 两环 ...
- HttpClient 发送请求和参数
发送请求 没有参数 private static void getData() { String timeStamp = String.valueOf(System.currentTimeMillis ...