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 ...
随机推荐
- Python 分发包中添加额外文件【新手必学】
在制作一个 Python 分发包时经常需要把一些文件添加到包中.最常见的例子是你希望通过 pip install命令安装 Python 包时会在 /etc/ 等目录下自动添加默认配置文件,由此可以 ...
- PHP windoews调用OpenOffice实现word/ppt转PDF
1.安装免费的openOffice软件 2.需要JDK支持 3.安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务.在组件服务—计算机—我的电脑—DCOMP配置中 4. 先 ...
- 解决Macbook Pro蓝牙不可用问题
谷歌搜索了下,在威锋网看到一个帖子,需要关机重置电源管理单元和系统NVRAM恢复出厂设置,具体操作如下:1.关机2.同时按下shift+control+option+power,保持5秒左右3.先按下 ...
- HTML学习(5)标题、水平线、注释
HTML 标题 标题(Heading)是通过 <h1> - <h6> 标签进行定义的. <h1> 定义最大的标题. <h6> 定义最小的标题. 注: 浏 ...
- 操作系统 - epoll中的ET和LT
参考 https://www.quora.com/What-is-meant-by-edge-triggering-and-level-triggering http://man7.org/linux ...
- Qt那点事儿(一)
原文http://www.cnblogs.com/andreitang/archive/2011/08/03/2125815.html 第一回 Signal和Slot是同步的还是异步的? 我们知道Qt ...
- python+tkinter+动画图片+爬虫(查询天气)的GUI图形界面设计
1.完整代码: import time import urllib.request #发送网络请求,获取数据 import gzip #压缩和解压缩模块 import json #解析获得的数据 fr ...
- electron-vue + element-ui构建桌面应用
最近需要用Node.js做一个桌面的应用,了解到electron可以用来做跨平台的桌面应用,而vue可以用来作为界面的解决方案,研究了一会儿如何把他们两个整合到一起使用,遇到了各种问题而放弃,毕竟作为 ...
- IEC 60958 && IEC 61937
IEC 60958 IEC 60958是一种传递数字音频的接口规范,相比I2S,IEC60958通过一根线同时传递时钟信号和数据信号.IEC 60958用来传递两channel,16/20/24bit ...
- LINUX 怎么实现root和普通用户的切换及怎么更改root密码
在linux系统中执行什么命令后可以使$变为#? 先说下$和#在linux系统终端(命令行)中通常代表的什么:$打头的表示这不是在root用户(管理员用户)下执行的命令#打头的和前者相反,即r ...