两指针
永远的EMT
每天时刻保持超越自我的意识
展开
-
【LeetCode】11. Container With Most Water
题解:注意理解题意是求最大水容器,所以两条边只能选小的那条作为深度,本题算法是用两指针,要求最大面积只须两指针同时遍历即可完成O(n)复杂度int maxArea(vector& height) { int n=height.size(); int ans=0; int l=0,r=n-1; while(l<r) { ans=max(an原创 2017-08-02 23:12:36 · 216 阅读 · 0 评论 -
【LeetCode】904. Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i].You start at any tree of your choice, then repeatedly perform the following steps:Add one piece of fruit from this tree to your b...原创 2019-07-01 00:53:42 · 210 阅读 · 0 评论 -
【LeetCode】Longest Substring with At Most Two Distinct Characters
给定一个字符串 s ,找出至多包含两个不同字符的最长子串 t 。示例 1:输入: "eceba"输出: 3解释: t 是 "ece",长度为3。示例 2:输入: "ccaabbb"输出: 5解释: t 是 "aabbb",长度为5。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-substrin...原创 2019-07-01 00:47:50 · 258 阅读 · 0 评论 -
【LeetCode】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"Note:I...原创 2019-07-01 00:26:19 · 158 阅读 · 0 评论 -
【LeetCode】713. Subarray Product Less Than K
Your are given an array of positive integers nums.Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.Example 1:Input: nums ...原创 2019-07-01 00:17:05 · 183 阅读 · 0 评论 -
【LeetCode】1040. 移动石子直到连续 II
在一个长度无限的数轴上,第 i 颗石子的位置为stones[i]。如果一颗石子的位置最小/最大,那么该石子被称作端点石子。每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子。值得注意的是,如果石子像stones = [1,2,5]这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子...原创 2019-06-30 22:30:39 · 740 阅读 · 0 评论 -
【LeetCode】Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.Example:Input:[ ["1","0","1","0","0"], ["1","0","1&quo原创 2018-11-18 23:35:15 · 180 阅读 · 0 评论 -
【LeetCode】Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume tha...原创 2018-10-28 00:56:09 · 179 阅读 · 0 评论 -
【LeetCode】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 you原创 2017-09-11 19:26:31 · 301 阅读 · 0 评论 -
【LeetCode】234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?题解:判断一个单链表是不是回文串,可以将后半部分进行反转。注意这里找中间位置可以用两指针的方法,设置快慢指针,快指针是慢指针两倍速度从而快指针到结尾时可以保证慢原创 2017-09-11 16:25:23 · 316 阅读 · 0 评论 -
【LeetCode】532. K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers原创 2017-09-11 13:55:42 · 434 阅读 · 0 评论 -
【LeetCode】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 additio原创 2017-09-10 21:25:05 · 226 阅读 · 0 评论 -
【LeetCode】345. Reverse Vowels of a String
题解:翻转元音字母,只需两指针,注意大小写,这里可以用string的方法find_first_of来查找元音位置class Solution {public: string reverseVowels(string s) { int i = 0, j = s.size() - 1; while (i < j) { i = s.f原创 2017-09-10 19:20:04 · 281 阅读 · 0 评论 -
【LeetCode】27. Remove Element
题解:利用两指针,一个用来更新数组一个用来遍历数组int removeElement(vector& nums, int val) { int i=0; for(int j=0;j<nums.size();j++) { if(nums[j]!=val){ nums[i]=nums[j]; i++;原创 2017-08-14 15:38:45 · 277 阅读 · 0 评论 -
【LeetCode】125. Valid Palindrome
题解:用两指针即可bool isPalindrome(string s) { int len=s.size(); int lt=0,rt=len-1; while(lt<rt) { while(isalnum(s[lt])==false&<<rt)lt++; while(isalnum(s[rt])==false&<<rt)rt--;原创 2017-08-05 18:22:17 · 279 阅读 · 0 评论 -
【LeetCode】238. Product of Array Except Self
题解:主要用两指针的思想,左指针用来记录左边数的乘积右指针用来记录右边数的乘积并且同时计算左右两边的乘积vector productExceptSelf(vector& nums) { int n=nums.size(); int l=1,r=1; vector res(n,1); for(int i=0;i<n;i++) { res[i原创 2017-08-03 22:15:22 · 232 阅读 · 0 评论 -
【LeetCode】26. Remove Duplicates from Sorted Array
题解:本题除了用hash还可以用两指针可以节省空间复杂度。用一个指针i遍历set型数组,另一个j遍历整个数组,每次不同时i++,并让j所在数放到i下一个数上,这样可以更新i指针所指的数。 int removeDuplicates(vector& nums) { int i=0; if(nums.size()==0) return 0; for(int j=1;j<num原创 2017-08-02 23:57:28 · 242 阅读 · 0 评论 -
【LeetCode】Container With Most Water
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two...原创 2019-07-01 01:14:32 · 186 阅读 · 0 评论