219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

题目地址:https://leetcode.com/problems/contains-duplicate-ii/description/

题意:给定一个数组和一个整数k,判断是否存在两个相等数的下标之差绝对值为k

思路:hash

class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
d = {}
for i, n in enumerate(nums):
if n in d:
if abs(i - d[n]) <= k:
return True
d[n] = i
return False

268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Example 1

Input: [3,0,1]
Output: 2

Example 2

Input: [9,6,4,2,3,5,7,0,1]
Output: 8
题目地址:https://leetcode.com/problems/missing-number/description/
题意:找0~n中缺少的数
思路:首项为0公差为1的等差数列和,减去数组的和
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
return n * (n+1) / 2 - sum(nums)

283. Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

题目地址:https://leetcode.com/problems/move-zeroes/description/

题意:将数组的0移到数组的后面,其他元素的相对顺序不变

思路:遍历,为0则删除加到末尾

class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
for n in nums:
if n == 0:
nums.remove(n)
nums.append(n)

414. Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.

Example 2:

Input: [1, 2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]
Output: 1
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
题目地址:https://leetcode.com/problems/third-maximum-number/description/
题意:返回数组中第三大的数
思路:使用set使数组元素唯一,排序输出
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sorted(set(nums))[-3] if len(set(nums))>2 else max(nums)

448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
题目地址:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
题意:给定长度n的数组,找出[1,n]不在数组中的元素
思路:
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in xrange(len(nums)):
index = abs(nums[i]) - 1
nums[index] = - abs(nums[index])
return [i + 1 for i in range(len(nums)) if nums[i] > 0]
												

LeetCode--219、268、283、414、448 Array(Easy)的更多相关文章

  1. 【LEETCODE】48、数组分类,简单级别,题目:189,217,219,268,283,414

    package y2019.Algorithm.array; import java.util.Arrays; import java.util.Stack; /** * @ClassName Rot ...

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

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

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

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

  4. 算法进阶面试题04——平衡二叉搜索树、AVL/红黑/SB树、删除和调整平衡的方法、输出大楼轮廓、累加和等于num的最长数组、滴滴Xor

    接着第三课的内容和讲了第四课的部分内容 1.介绍二叉搜索树 在二叉树上,何为一个节点的后继节点? 何为搜索二叉树? 如何实现搜索二叉树的查找?插入?删除? 二叉树的概念上衍生出的. 任何一个节点,左比 ...

  5. CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset

    一.盒子模型(Box Model) 盒子模型也有人称为框模型,HTML中的多数元素都会在浏览器中生成一个矩形的区域,每个区域包含四个组成部分,从外向内依次是:外边距(Margin).边框(Border ...

  6. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。

    一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...

  7. 教你一招:解决win10/win8.1系统在安装、卸载软件时出现2502、2503错误代码的问题

    经常遇到win10/win8.1系统在安装.卸载软件时出现2502.2503错误代码的问题. 解决办法: 1.打开任务管理器后,切换到“详细信息”选项卡,找到explore.exe这个进程,然后结束进 ...

  8. Map集合及与Collection的区别、HashMap和HashTable的区别、Collections、

    特点:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map集合和Collection集合的区别 Map集合:成对出现 (情侣)                       ...

  9. 兼容8事件绑定与解绑addEventListener、removeEventListener和ie的attachEvent、detachEvent

    兼容8事件绑定与解绑addEventListener.removeEventListener和ie的attachEvent.detachEvent   ;(function(){ // 事件绑定 bi ...

随机推荐

  1. 漏洞复现——tomcat远程代码执行漏洞

    漏洞描述: 当存在该漏洞的Tomcat 运行在 Windows 主机上,且启用了 HTTP PUT请求方法,攻击者可通过构造的攻击请求向服务器上传包含任意代码的 JSP 文件,造成任意代码执行 影响范 ...

  2. Python-Selenium中chromeDriver限制图片和Javascript加载

    我们有的时候使用Selenium会希望能够限制图片和Javascript执行,从而提高网页加载速度. options=webdriver.ChromeOptions() prefs={      'p ...

  3. java XML(可扩展标记语言)

    XML 是EXtensible Markup Language的缩写,它是一种类似于HTML的标记语言,称为可扩展标记语言,传输数据而不是显示数据,可以自定义标签,具有自我描述性是一种通用的数据交换格 ...

  4. nginx开启fileinfo扩展

    //实现网址 https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80838424 (1) (2).make && make ...

  5. 按钮切换显示不同的内容(js控制)

    今天项目发现了一个jsp页面按钮切换,下面展示模块的不同显示问题,看到同事修改完之后的效果,js控制感觉特写好,所以想写把这个好的方法js记录下来,以便以后的参考. 一:先上图,了解大概的样子,如下图 ...

  6. KM算法详解[转]

    KM算法详解 原帖链接:http://www.cnblogs.com/zpfbuaa/p/7218607.html#_label0 阅读目录 二分图博客推荐 匈牙利算法步骤 匈牙利算法博客推荐 KM算 ...

  7. CAD插入块后坐标不匹配

    有两张图,将一张图复制(CTRL+V),再另一张图中粘贴到原坐标(pasteorig),两张图可以很好匹配,但将一张图以外部参照的方式插入另一张图却发现图形无法匹配.因为没有看到图纸,所以我也没法准确 ...

  8. python(6)之文件

    一.读写文件 以追加文件内容(a).读(r).写(w),读写(r+)的形式打开文件: f = open('yesterday','a',encoding='utf-8')#文件句柄 #输出一行文件内容 ...

  9. php常见问题-foreach和引用造成的问题。

    结论:  foreach($arr as &$v) 类似这样的引用循环, 脚本语言需要注意,再次使用 $v时,他还指向原来的引用.会产生问题. unset($v)可以解除引用. 所以循环引用过 ...

  10. ajax常见的面试问题

    1:什么是ajax?ajax作用是什么? 异步的javascript和xml AJAX 是一种用于创建快速动态网页的技术. ajax用来与后台交互 2:原生js ajax请求有几个步骤?分别是什么 / ...