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. ...
随机推荐
- python:extend (扩展) 与 append (追加) 之间的天与地
>>> li = ['a', 'b', 'c'] >>> li.extend(['d', 'e', 'f']) >>> li ['a', 'b', ...
- 函数使用五:MIR7 发票预制 BAPI_INCOMINGINVOICE_PARK
引自:http://blog.csdn.net/champaignwolf/article/details/51422329 FUNCTION zincominginvoice_park. *&quo ...
- 在Shell中使用alias
以前在Linux跳机上登录其他服务器(ssh IP),为了方便,把很多服务器的IP和业务名称touch到了用户主目录下,这样方便了好多,每次ls -l 出来下就可以了,然后复制,粘贴就方便了.如下图. ...
- Oracle 常用sql整理
1. 查看当前正在只用的undo段 select s.sid, s.serial#, s.username, r.name, t.STATUS, t.START_TIME, t.USED_UBLK, ...
- jqgrid取所有行的值,jqgrid取行对应列(name)的值,jqgrid取多行值对应列转json的方法
1.jqgrid取所有行的值(#gridTable指对应table的ID) var obj = $("#gridTable").jqGrid("getRowData&qu ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- nodejs 解析excel文件
app.js: var FileUpload = require('express-fileupload') app.use(FileUpload()); service.js: npm instal ...
- 牛客网 PAT 算法历年真题 1008 : 锤子剪刀布 (20)
锤子剪刀布 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 大家应该都会玩“锤子剪刀布”的游戏:现给出 ...
- secFox setting
secFox setting 1● load file 2● install 3● use
- spring boot 入门 使用spring.profiles.active来分区配置
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...