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 ...
随机推荐
- JavaScript对象之属性标签
本文介绍一下js对象的属性标签(configurable.writable.enumerable.value.get.set)的使用. 上图的要点为: 1.Object.getOwnPropertyD ...
- 【Unity|C#】基础篇(0)——C#与.NET框架
[学习资料] <C#图解教程>(第1章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...
- STM32F103之定时器学习记录
/==============翻译STM32F103开发手册定时器部分========================/ 14 高级控制计时器(TIM1和TIM8) 14.1 TIM1和TIM8介绍 ...
- 1.4 面试问题整理: ATM机取款
流程图:
- Day4 注解 泛型
注解是什么 注释是绑定到程序源代码元素的元数据,对它们运行的代码的操作没有影响. https://blog.csdn.net/SDDDLLL/article/details/93509699 他们 ...
- Python记:通用的序列操作之成员资格(听起来倒是有些抽象的!)
______________________________永远守护这一尘不染的真心! 要检查特定的值是否包含在序列中,可使用运算符in.它检查是否满足指定的条件,并返回相应的值:满足时返回True, ...
- docker为镜像添加SSH服务
启动并进入容器中 这里用db1容器完成实验. 安装openssh服务和修改sshd配置文件 安装openssh yum install openssh-server openssh-clients - ...
- c#显示行号设置方法
工具→选项→文本编辑器→c#→常规→显示→勾选行号 反之取消即可.
- 【做题笔记】P1969 积木大赛
非常感谢 rxz 大佬提供的思路. 首先放个图(rxz 画的) 采用贪心的策略:对于一个期望高度 \(h_i\) ,如果大于 \(h_{i-1}\),那么最终答案要加上二者之差:如果小于或等于,那么说 ...
- 7_5 困难的串(UVa129)<回溯法:避免无用判断>
“超级氪因素大赛”(译注:英国的一档电视心智竞答节目)的主办方雇你来对付那些足智多谋的参赛选手.在比赛的一个环节中,节目主持人将念出一长串的字母来考验选手的记忆能力.因为许多选手都是分析字串模式的高手 ...