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)的更多相关文章

  1. (升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)

    本课程主要讲解目前大数据领域最热门.最火爆.最有前景的技术——Spark.在本课程中,会从浅入深,基于大量案例实战,深度剖析和讲解Spark,并且会包含完全从企业真实复杂业务需求中抽取出的案例实战.课 ...

  2. 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法

    下载远程(第三方服务器)文件.图片,保存到本地(服务器)的方法.保存抓取远程文件.图片   将一台服务器的文件.图片,保存(下载)到另外一台服务器进行保存的方法: 1 #region 图片下载 2 3 ...

  3. Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式

    递归.二维数组顺时针旋转90°.正则表达式 1.   递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...

  4. 创建表空间、新增用户、给用户赋予DBA权限 、删除用户下的上有数据表

    正文原创 一:查询数据库实例有多少用户: [oracle@localhost ~]$ sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 Product ...

  5. Hive 文件格式 & Hive操作(外部表、内部表、区、桶、视图、索引、join用法、内置操作符与函数、复合类型、用户自定义函数UDF、查询优化和权限控制)

    本博文的主要内容如下: Hive文件存储格式 Hive 操作之表操作:创建外.内部表 Hive操作之表操作:表查询 Hive操作之表操作:数据加载 Hive操作之表操作:插入单表.插入多表 Hive语 ...

  6. 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)

    并发编程概述   前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...

  7. Python--高阶函数、函数嵌套、名称空间及变量作用域、闭包、装饰器

    1.高阶函数(map/reduce/filter) 高阶函数是指函数的参数可以是函数 这篇总结几个常用的高阶函数:map/reduce/filter map函数.reduce函数.filter函数都是 ...

  8. JS 获取(期号、当前日期、本周第一天、最后一天及当前月第一、最后天函数)    

    JS 获取(期号.当前日期.本周第一天.最后一天及当前月第一.最后天函数 /** 2 * 获取当前月期号 3 * 返回格式: YYYY-mm 4 * / 5 function getCurrentMo ...

  9. Python基础之函数:6、异常相关和生成器对象、yield用法、生成器表达式

    目录 一.异常常见类型 1.类型错误 2.缩进错误 3.索引错误 4.语法错误 5.属性错误 6.key键错误 二.异常处理语法结构 1.基本语法结构 2.查看错误类型 3.针对不同类型所作措施 4. ...

随机推荐

  1. 6月16 ThinkPHP连接数据库及Model数据模型层--------查询及数据添加

    连接数据库配置及Model数据模型层 convertion.php config.php 1.在config.php做数据库连接配置 2.修改配置 /* 数据库设置 */ 'DB_TYPE' => ...

  2. 【PowerDesigner】【6】Table视图同时显示Code和Name

    效果图: —————————————————————— 步骤: 文字版: 1,顶部工具栏Tools→Display Preference 2,Columns→List columns右侧按钮 3,勾选 ...

  3. BigDecimal 类型数据比较大小

    public static void main( String[] args ) { BigDecimal a=new BigDecimal(-1); if(a.compareTo(BigDecima ...

  4. win php安装 oracle11 g

    1.下载plsql和oracle11g plsql安装比较简单,就是普通的安装.oracle11 g不用安装, 下面我讲解一下win 64位的系统配置oracle: (1).首先我使用的是warpse ...

  5. 自用chrome+油猴脚本,使用迅雷下载百度云大文件,一键离线下载

    油猴是有名的火狐浏览器插件(Greasemonkey),当然也有Chrome版本(tampermonkey),甚至IE.Safari.Opera都有……虽然这些插件是由不同的开发者开发出来的,界面也可 ...

  6. zzw原创_oracle循环中的异常捕捉_捕捉异常后并继续循环

    例子如下 set serveroutput on; declare   V_SQL VARCHAR2(255);   errorCode number; --异常编码     errorMsg var ...

  7. windows7安装教程(vmware)

    这步是正确安装windows的关键,如果不设置那么安装时将不能识别出磁盘,造成安装不成功. 选择No进行自定义修饰,主要是保证C盘大小合适,其他盘可在安装完成之后再调整. 后续安装步骤全自动,完全不用 ...

  8. ubuntu ssh前后台切换命令相关

    后台运行:命令+& 例如 sleep 60 & jobs -l 显示job的pid和状态 ps 显示用户进程 将第一个job切换回前台:fg 1 放到后台:bg 1 cltr + z ...

  9. ubuntu中更新.netcore到2.1版本

    如果需要安装新版本到dotnetcore,需要先卸载旧版本(https://github.com/dotnet/core/blob/master/release-notes/download-arch ...

  10. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...