Two Points问题--之LeetCode 11题

---恢复内容开始---

Container with most  water--LeetCode11题

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

题目的大致意思就是给定义个数组,然后根据相应的索引值垂直x轴划线,然后从任意选择两条线段,根据木桶原理进行蓄水,找到蓄水最多的两条线段。我首先采用暴力遍历的方式进行计算,这样的话时间复杂度是O(N^2),通过LeetCode测试时,时间超出限制。

然后看LeetCode上面的优质解答,代码如下:

这样就延伸出一系列的Two Points(双指针)问题,以下题目全部来自微信公众号:光影键盘侠。

Move Zeros---LeetCode第道题

给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序

注意:

1.必须在原数组上操作
2.最小化操作数

解题思路(双指针分别是指向数组的和指向0的指针):

代码:

Remove Duplicates from Sorted Array---LeetCode第道题

给一个整数数组,去除重复的元素。

你应该做这些事

1.在原数组上操作
2.将去除重复之后的元素放在数组的开头
3.返回去除重复元素之后的元素个数

注意是排好序的数组,例如输入 [0 0 1 2 3 3]输出4

解题思路:两个指针一个进行遍历,一个记录重复的长度,相同的话长度加1,不同的话左移--代码如下:

Implement strStr()--LeetCode第28题

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:(注意:i的范围很容易出错,注意是haystack.length()-needle.length()+1而不是简单的haystack.length())两个指针分别从两个字符串进行遍历,判断haystack有没有needle

---恢复内容结束---

Container with most  water--LeetCode11题

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

题目的大致意思就是给定义个数组,然后根据相应的索引值垂直x轴划线,然后从任意选择两条线段,根据木桶原理进行蓄水,找到蓄水最多的两条线段。我首先采用暴力遍历的方式进行计算,这样的话时间复杂度是O(N^2),通过LeetCode测试时,时间超出限制。

然后看LeetCode上面的优质解答,代码如下:

这样就延伸出一系列的Two Points(双指针)问题,以下题目全部来自微信公众号:光影键盘侠。

Move Zeros---LeetCode第道题

给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序

注意:

1.必须在原数组上操作
2.最小化操作数

解题思路(双指针分别是指向数组的和指向0的指针):

代码:

Remove Duplicates from Sorted Array---LeetCode第道题

给一个整数数组,去除重复的元素。

你应该做这些事

1.在原数组上操作
2.将去除重复之后的元素放在数组的开头
3.返回去除重复元素之后的元素个数

注意是排好序的数组,例如输入 [0 0 1 2 3 3]输出4

解题思路:两个指针一个进行遍历,一个记录重复的长度,相同的话长度加1,不同的话左移--代码如下:

Implement strStr()--LeetCode第28题

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:(注意:i的范围很容易出错,注意是haystack.length()-needle.length()+1而不是简单的haystack.length())两个指针分别从两个字符串进行遍历,判断haystack有没有needle

时间: 2024-08-16 03:21:00

Two Points问题--之LeetCode 11题的相关文章

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

Leetcode 算法题--ReverseWordsInString

翻转字符串,想到什么写什么...我的做法是先trim掉空格,然后从字符串尾部开始扫描,遇到空格则认为一个单词结束,然后copy这个单词.需要注意的地方在于当扫描到最后一个单词的第一个字母时(譬如the sky is blue的t字母),注意单词长度的自增逻辑. 网上还有人的做法是反转整个字符串,然后逐个翻转单词. 1 package edu.hust.sse.Problems; 2 3 //Given s = "the sky is blue", 4 //return "bl

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi