1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

  1. Given nums = [2, 7, 11, 15], target = 9,
  2. Because nums[0] + nums[1] = 2 + 7 = 9,
  3. return [0, 1].

题目地址:https://leetcode.com/problems/two-sum/description/

题意:给定一个整形数组和一个目标,返回数组中相加等于目标的两个数的下标

思路:创建一个hash表,枚举每一个数n,把数值作为key,下标作为values,遍历时找target-n

python

  1. class Solution(object):
  2. def twoSum(self, nums, target):
  3. """
  4. :type nums: List[int]
  5. :type target: int
  6. :rtype: List[int]
  7. """
  8. d ={}
  9. for i, n in enumerate(nums):
  10. m = target - n
  11. if m in d:
  12. return[d[m],i]
  13. else:
  14. d[n] = i

 

26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example:

  1. Given nums = [1,1,2],
  2. Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
  3. It doesn't matter what you leave beyond the new length.
  1. 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
  1. 题意:给定一个已经排序好的数组,用O(1)的空间在原数组去掉重复的数,返回新的长度
  1. 思路:利用双指针,一个指向当前不重复的长度len,一个指向遍历看不重复的写到len
  1. python
  1. class Solution(object):
  2. def removeDuplicates(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. if nums == []:
  8. return 0
  9. index = 0
  10. for i in range(1,len(nums)):
  11. if nums[index] != nums[i]:
  12. index += 1
  13. nums[index] = nums[i]
  14. return index+1
  1.  

27. Remove Element

Given an array and a value, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

  1. Given nums = [3,2,2,3], val = 3,
  2. Your function should return length = 2, with the first two elements of nums being 2.
  1. 题目地址:https://leetcode.com/problems/remove-element/description/
  1. 题意:给定一个数组和目标,在原位将数组中的目标值删除,返回新的长度
  1. 思路:双指针
  1. python
  1. class Solution(object):
  2. def removeElement(self, nums, val):
  3. """
  4. :type nums: List[int]
  5. :type val: int
  6. :rtype: int
  7. """
  8. j = 0
  9. for i in range(len(nums)):
  10. if nums[i] != val:
  11. nums[j] = nums[i]
  12. j += 1
  13. return j
  1.  

35. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

  1. Input: [1,3,5,6], 5
  2. Output: 2

Example 2:

  1. Input: [1,3,5,6], 2
  2. Output: 1

Example 3:

  1. Input: [1,3,5,6], 7
  2. Output: 4

Example 1:

  1. Input: [1,3,5,6], 0
  2. Output: 0
  1. 题目地址:https://leetcode.com/problems/search-insert-position/description/
  1. 题意:给定一个数组和target,如果在数组里找到target,返回target的下标,如果找不到返回插入的位置
  1. 思路:二分法
  1. python
  1. class Solution(object):
  2. def searchInsert(self, nums, target):
  3. """
  4. :type nums: List[int]
  5. :type target: int
  6. :rtype: int
  7. """
  8. start, end = 0, len(nums)
  9. while start < end:
  10. mid = (start+end) / 2
  11. if nums[mid] < target:
  12. start = mid+1
  13. else:
  14. end = mid
  15. return start
  1.  
  1. 53. Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

题目地址:https://leetcode.com/problems/maximum-subarray/description/

题意:找出连续子数组的最大和

思路:动态规划问题

  1. python
  1. class Solution(object):
  2. def maxSubArray(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. ans, sum = nums[0], 0
  8. for x in nums:
  9. sum += x
  10. if sum > ans:
  11. ans = sum
  12. if sum < 0:
  13. sum = 0
  14. return ans

LeetCode--1、26、27、35、53 Array(Easy)的更多相关文章

  1. 2022年整理最详细的java面试题、掌握这一套八股文、面试基础不成问题[吐血整理、纯手撸]

    这里是参考B站上的大佬做的面试题笔记.大家也可以去看视频讲解!!! 文章目录 1.面向对象 2.JDK.JRE.JVM区别和联系 3.==和equals 4.final 5.String .Strin ...

  2. ZT ---- 给孩子的信(孩子写给爸爸妈妈的信在24、25、26楼)

    胡同口 > 情感 > 婚后空间 > 给孩子的信(孩子写给爸爸妈妈的信在24.25.26楼) 给孩子的信(孩子写给爸爸妈妈的信在24.25.26楼)分享: 腾讯微博 新浪微博 QQ空间 ...

  3. 35、cobbler自动化安装操作系统

    35.1.cobbler介绍: Cobbler是独立的,不需要先安装Kickstart然后再安装Cobbler: Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速 ...

  4. 在论坛中出现的比较难的sql问题:27(字符串拆分、字符串合并、非连续数字的间隔范围、随机返回字符串)

    原文:在论坛中出现的比较难的sql问题:27(字符串拆分.字符串合并.非连续数字的间隔范围.随机返回字符串) 在论坛中看到一个帖子,帖子中有一些sql方面的面试题,我觉得这些面试题很有代表性. 原帖的 ...

  5. EC读书笔记系列之16:条款35、36、37、38、39、40

    条款35 考虑virtual函数以外的其他选择 记住: ★virtual函数的替代方案包括NVI手法及Strategy模式的多种形式.NVI手法自身是一个特殊形式的Template Method模式 ...

  6. 35、mysql数据库(ddl)

    35.1.数据库之库操作: 1.创建数据库(在磁盘上创建一个对应的文件夹): create database [if not exists] db_name [character set xxx]; ...

  7. P151、面试题27:二叉搜索树与双向链表

    题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.(本质是中序遍历)二叉树结点的定义如下:struct BinaryTreeNod ...

  8. .Net程序员学用Oracle系列(26):PLSQL 之类型、变量和结构

    1.类型 1.1.属性类型 1.2.记录类型 2.变量 2.1.变量类型 2.2.变量定义 2.3.变量赋值 3.结构 3.1.顺序结构 3.2.选择结构 3.3.循环结构 4.总结 1.类型 在&l ...

  9. 35、concurrent.futures模块与协程

    concurrent.futures  —Launching parallel tasks    concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...

随机推荐

  1. Selenium-WebDriver驱动对照表

    Chrome 对于chrome浏览器,有时候会有闪退的情况,也许是版本冲突的问题,我们要对照着这个表来对照查看是不是webdriver和chrome版本不对 chromedriver版本 支持的Chr ...

  2. MongoDB存储

    mongo DB #定义数据库,文件为config.py mongo_url='loclhost'//数据库地址 mongo_DB='DB_name'//数据库名称 mongo_TABEL='tabe ...

  3. 6月3 Smarty基础读取配置

    Smarty百科 Smarty是一个php模板引擎.更准确的说,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法.可以描述为应用程序员和美工扮演了不同的角色,因为在大多数情况下 ,他们不可能是同 ...

  4. 关于react16.4——上下文Context

    首先我们来聊一聊(上下文)Context. 上下文(Context) 提供了一种通过组件树传递数据的方法,无需在每个级别手动传递 props 属性. 在典型的 React 应用程序中,数据通过 pro ...

  5. meta标签常用设置

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  6. 理解JS中的this的指向

    原文地址:https://www.cnblogs.com/pssp/p/5216085.html#1 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到 ...

  7. eclipse初学者的使用

    eclipse的使用 字体设置:      设置环境字符: 设置背景颜色 寻找窗口: java JDK的配置: 配置自动提示: 适合初学者适应环境开发.

  8. Linux svn的搭建和使用

    搭建: http://www.cnblogs.com/aLittleBitCool/archive/2011/07/09/2101602.html 使用: http://www.cnblogs.com ...

  9. Qt动态布局

    QVBoxLayout *m_pvLayout = NULL: QWidget *m_pWidgetPlay = NULL: m_pvLayout = new QVBoxLayout(this); m ...

  10. post和get的使用场景和区别

    使用场景: 区别: ①传送方式不同:get通过地址栏传输,post通过报文传输. ②get产生一个TCP数据包,post产生两个数据包,对于get方式的请求,浏览器会把http header和data ...