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".
最新文章
- HHKB MAC 配置指南 操作指南 快捷键
- C中的volatile用法
- HDU 1231:最大连续子序列 解题报告
- 《ASP.NET MVC4 WEB编程》学习笔记------Entity Framework的Database First、Model First和Code Only三种开发模式
- C# GUID
- 显示标题栏中标题左侧的小图icon
- webpack3.x基本配置与总结
- 多线程之倒计时器CountDownLatch和循环栅栏CyclicBarrier
- Java基础1:深入理解Java面向对象三大特性
- The question that comes to mind
- bzoj 4540 [HNOI 2016] 序列 - 莫队算法 - Sparse-Table - 单调栈
- 关于mysql中使用聚合函数结果集为空,仍显示size为1,所有元素为Null问题的解决办法
- 基本select 语句总结
- uva-11044-水题
- jdk(1.8)命令行工具(二)
- QT 主窗口和子窗口相互切换示例
- 2017.10.23 Arduino Atmel EFM32低功耗监测
- sudo 运行 npm或node时,出现找不到命令
- [18/11/29] 继承(extends)和方法的重写(override,不是重载)
- [电子书] 《Android编程兵书》PDF