LeetCode练题——35. Search Insert Position
1、题目
1781214Add to ListShare
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
2、我的解法
# -*- coding: utf-8 -*-
# @Time : 2020/2/4 16:33
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 35. Search Insert Position.py
from typing import List
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
lens=len(nums)
if target in nums:
return nums.index(target)
else:
if target < nums[-1]:
for i in range(lens):
if target < nums[i]:
nums.insert(i,target)
return nums.index(target)
else:
return lens
print(Solution().searchInsert([1,4,5,6,11,11],10))
10528180Add to ListShare
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: 4
Output: "1211"
Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12" which means frequency = 1 and value = 2, the same way "1" is read as "11", so the answer is the concatenation of "12" and "11" which is "1211".
LeetCode练题——35. Search Insert Position的更多相关文章
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- [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 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导致的溢 ...
- 35. Search Insert Position@python
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【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 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- ET框架之自写模块SmartTimerModule
1.代码结构图 2.SmartTimer 模块Entity: using System; namespace ETModel { [ObjectSystem] public class SmartTi ...
- Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int ...
- javasE--基础部分--线程
Day23 本单元目标 一.线程的相关概念 二.线程的创建和启动★ 三.线程的停止 四.线程的常用方法 五.线程的生命周期★ 六.线程的同步 七.线程的通信 八.线程的创建方式三 九.线程的创建方 ...
- mysql(4):性能分析和性能优化
性能分析 慢查询日志分析 ①查询慢查询日志的状态 show global variables like '%slow_query_log%'; ②开启慢查询日志(当mysql重启时会重置) set g ...
- Git 从远端指定分支克隆代码到本地
不指定分支默认是master git clone + clone 地址 # 例如 git clone https://amc-msra.visualstudio.com/xxx/_xx/xxxxxx ...
- centos 7 pip install MySQL-python 报错
pip install MySQL-python 报错 pip install MySQL-python DEPRECATION: Python . Please upgrade your Pytho ...
- ABB 机器人 流水灯and跑马灯
MODULE MainModule VAR signaldi signaldi14; PROC main() //di14_test 数字输入信号 WHILE di14_test = DO ycld; ...
- git 提交的时候 建立排除文件夹或者文件
1.在Git的根仓库下 touch .gitignore 2.编辑这个文件
- noobSTL-1-配置器-1
noobSTL-1-配置器-1 1.要点分析 1.1 可能让你困惑的C++语法 组态 即配置. 临时对象 一种无名对象.有时候会刻意地制造临时对象. 静态常量整数成员在class内部直接初始化 con ...
- 560. 和为K的子数组
Q: A: 1.暴力找所有可能的子数组,n^2个子数组,最长长度n,则n ^3. 2.n^2解法 从1~n-1各起点开始,一直找到结尾,n^2 class Solution { public: int ...