LeetCode—66、88、118、119、121 Array(Easy)
66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
题目地址:https://leetcode.com/problems/plus-one/description/
题意:给定一个非负整数表示为非空数组,对该整数加1
思路:进位
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
carry, n = 1, len(digits)
ans = [0 for i in range(n)]
for i in range(n):
ans[i] = (carry + digits[n-i-1]) % 10
carry = (carry + digits[n-i-1]) / 10
if carry:
ans.append(carry)
return ans[::-1]
88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
题目地址:https://leetcode.com/problems/merge-sorted-array/description/
题意:给定两个已经排序好的数值,将nums2中的前n个插入nums1中,保持顺序
思路:
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
while m > 0 and n > 0:
if nums1[m-1] >= nums2[n-1]:
nums1[m+n-1] = nums1[m-1]
m -= 1
else:
nums1[m+n-1] = nums2[n-1]
n -= 1
if n > 0:
nums1[:n] = nums2[:n]
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
题目地址:https://leetcode.com/problems/pascals-triangle/description/
题意:给定行,返回杨辉三角
思路:
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
ans = [[1],[1,1]]
for i in range(1, numRows):
temp = [1] + [ans[-1][t]+ans[-1][t+1] for t in range(len(ans[-1])-1)] +[1]
ans.append(temp)
return ans[:numRows]
119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
题目地址:https://leetcode.com/problems/pascals-triangle-ii/description/
题意:返回杨辉三角的指定行
思路:和上题差不多
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
ans = [[1],[1,1]]
if rowIndex < 2:
return ans[rowIndex]
for i in range(1, rowIndex):
temp = [1] + [ans[-1][t]+ans[-1][t+1] for t in range(len(ans[-1])-1)] + [1]
ans.append(temp)
return temp
121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
题意:给定每一天的股价,求最大的利润
思路:先买入,在卖出,每次更新当前最小价格
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices:
return 0
ans, money, n = 0, prices[0], len(prices)
for i in range(n):
ans=max(ans,prices[i]-money)
money = min(prices[i],money)
return ans
LeetCode—66、88、118、119、121 Array(Easy)的更多相关文章
- (升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)
本课程主要讲解目前大数据领域最热门.最火爆.最有前景的技术——Spark.在本课程中,会从浅入深,基于大量案例实战,深度剖析和讲解Spark,并且会包含完全从企业真实复杂业务需求中抽取出的案例实战.课 ...
- 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法
下载远程(第三方服务器)文件.图片,保存到本地(服务器)的方法.保存抓取远程文件.图片 将一台服务器的文件.图片,保存(下载)到另外一台服务器进行保存的方法: 1 #region 图片下载 2 3 ...
- Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式
递归.二维数组顺时针旋转90°.正则表达式 1. 递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...
- 创建表空间、新增用户、给用户赋予DBA权限 、删除用户下的上有数据表
正文原创 一:查询数据库实例有多少用户: [oracle@localhost ~]$ sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 Product ...
- Hive 文件格式 & Hive操作(外部表、内部表、区、桶、视图、索引、join用法、内置操作符与函数、复合类型、用户自定义函数UDF、查询优化和权限控制)
本博文的主要内容如下: Hive文件存储格式 Hive 操作之表操作:创建外.内部表 Hive操作之表操作:表查询 Hive操作之表操作:数据加载 Hive操作之表操作:插入单表.插入多表 Hive语 ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- Python--高阶函数、函数嵌套、名称空间及变量作用域、闭包、装饰器
1.高阶函数(map/reduce/filter) 高阶函数是指函数的参数可以是函数 这篇总结几个常用的高阶函数:map/reduce/filter map函数.reduce函数.filter函数都是 ...
- JS 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数)
JS 获取(期号.当前日期.本周第一天.最后一天及当前月第一.最后天函数 /** 2 * 获取当前月期号 3 * 返回格式: YYYY-mm 4 * / 5 function getCurrentMo ...
- Python基础之函数:6、异常相关和生成器对象、yield用法、生成器表达式
目录 一.异常常见类型 1.类型错误 2.缩进错误 3.索引错误 4.语法错误 5.属性错误 6.key键错误 二.异常处理语法结构 1.基本语法结构 2.查看错误类型 3.针对不同类型所作措施 4. ...
随机推荐
- 4月24 php基础及函数的应用
PHP是一种被广泛认可应用,运行在服务端的脚本语言,PHP需要安转PHP应用程序服务器去解释执行,是用来协助Web服务器工作的编程语言,因此所写的任何代码只有在www文件下的才能实施预览.PHP是一种 ...
- MySql(九)索引
一.索引的介绍 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. 二 .索引的作用 约束和加速查找 三.常见的几 ...
- POJ - 题解sol[暂停更新]
初期:一.基本算法: (1)枚举. (poj1753,poj2965) poj1753 话说我用高斯消元过了这题... poj2965 巧了,用高斯消元01矩阵更快(l o l). (2)贪心(poj ...
- 通过JdbcTemplate编写数据访问(二十八)
数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式. 首先,为了连接数据库需要引入jdbc支持,在pom.xml中引入如下配置: 1 2 3 4 < ...
- Xmind settings lower
Xmind settings lower 1● setting 2● options 3● fast short keys 快捷键(Windows) 快捷键(Mac) 描述 Ctrl+N ...
- Utf-8+Bom编码导致的读取数据部分异常问题
项目中经常会有这种场景,在配置文件中配置对应关系,项目启动的时候从中读取数据存入map缓存起来,这样使用的时候就可以直接从map找到对应的映射关系. 然后遇到了这么一个诡异的问题,一个简单的映射关系如 ...
- lombok @Slf4j注解
背景知道有这么个东西,是因为项目中用到了@Slf4j注解. lombok库提供了一些注解来简化java代码 官网:http://projectlombok.org/ 查看lombok所有api:htt ...
- [x] 封装、继承,多态
那么多态的作用是什么呢,封装可以使得代码模块化,继承可以扩展已存在的代码,他们的目的都是为了代码重用. 而多态的目的则是为了接口重用.也就是说,不论传递过来的究竟是那个类的对象,函数都能够通过同一个接 ...
- 01背包问题之2(dp)
01背包问题之2 有n个物品,重量和价值分别为wi和vi,从这些物品中挑选出重量不超过W的物品,求所有挑选方案中物品价值总和的最大值 限制条件: 1 <= n <= 100; 1 < ...
- Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性
原文地址:https://moell.cn/article/24 不错的laravel网站 需求场景 修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性. Laravel版本 ...