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. ...
随机推荐
- Windows下dump文件生成与分析
一. 生成Dump文件方式 1.1任务管理器 在程序崩溃后,先不关闭程序,在任务管理器中找到该程序对应的进程.右键—>创建转储文件. 此时会在默认的目录下创建出一个dump文件. 可以看出 ...
- BIT-区间修改单点查询
正好刷题时碰到了这个之前遇到过的问题,类似的还有区间修改区间查询,以后学了会补上. 我们知道BIT只支持单点修改区间查询,如果现在问题变成每次让一个区间的数同时变化,然后询问的是某一个点得值的时候该怎 ...
- Node.js的环境搭建
Node.js 可以理解为服务端的JavaScript.是基于Chrome JavaScript 运行时的一个平台. 是一个事件驱动I/O,基于Google V8 引擎,执行起来速度特别快,性能非常好 ...
- 【LeetCode】Anagram
Anagram 指由颠倒字母顺序而构成的单词. e.g. 给出 ["eat", "tea", "tan", "ate", ...
- QString 的用法
C++语言提供了两种字符串的实现:C风格的字符串,以'\0‘结尾;std::string,即标准模版库中的类.Qt则提供了自己的字符串实现:QString,QString不用担心内存分配以及关于'\0 ...
- Scanner类完成用户键盘录入
l Scanner类 Scanner类是引用数据类型的一种,我们可以使用该类来完成用户键盘录入,获取到录入的数据. Scanner使用步骤: 导包:import java.util.Scanner; ...
- 微服务之SpringCloud基础
SpringCloud微服务基础 微服务架构--SpringCloud网站架构模式 单点应用/分布式系统面向于服务架构(SOA) /微服务架构web项目三层架构1.控制层2.业务逻辑层3.数据访问层传 ...
- MySQL批量修改字符集
统一将字符字符集变成utf8_general_ci,已测试. DROP PROCEDURE IF EXISTS `chanageCharSet`; CREATE PROCEDURE `chanageC ...
- py 正在爬取第%d页的美眉图
#coding=utf-8import reimport requestsfrom bs4 import BeautifulSoupnums = 1num = 1while True: url = & ...
- learning ddr pagesize calculate
example: if DDR is 512MB*16*8 COLBITS = 10, A0-A9 be used for cloumn address. ORG = 16 , each bank ...