![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
双指针问题
JackZhangNJU
未来的路还很长
展开
-
leetcode 462. Minimum Moves to Equal Array Elements II 换一个角度思考问题 + 寻找中位数
Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by ...原创 2017-12-13 09:40:58 · 263 阅读 · 0 评论 -
leetcode 611. Valid Triangle Number 构成三角形数量 + 双指针
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: ...原创 2017-12-21 09:14:58 · 339 阅读 · 0 评论 -
leetcode 410. Split Array Largest Sum 最小化最大数 + 一个很棒的二分搜索BinarySearch的做法 + 真心很棒
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m sub原创 2017-12-08 14:31:03 · 344 阅读 · 0 评论 -
leetcode 218. The Skyline Problem 优先级队列PriorityQueue + mulitset模拟Heap
A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as原创 2017-09-22 10:29:16 · 555 阅读 · 0 评论 -
leetcode 209. Minimum Size Subarray Sum 最短子数组的和 + 十分典型的移动窗口做法
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the arr原创 2017-09-21 10:23:38 · 448 阅读 · 0 评论 -
leetcode 141. Linked List Cycle 链表循环的判定 + 快慢指针寻找环
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?本题就是判断链表是否存在循环,这个方法很棒,说实话我自己是想不到的。代码如下:/*class ListNode { int val; Li原创 2017-09-16 13:39:39 · 618 阅读 · 0 评论 -
leetcode 142. Linked List Cycle II 快慢指针寻找环 + 快慢指针寻找环入口
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?和上一道题不一样原创 2017-09-17 12:53:02 · 414 阅读 · 0 评论 -
leetcode 466. Count The Repetitions 重复字符串的判断
Define S = [s,n] as the string S which consists of n connected strings s. For example, [“abc”, 3] =”abcabcabc”.On the other hand, we define that string s1 can be obtained from string s2 if we can re...原创 2017-12-15 15:26:56 · 905 阅读 · 0 评论 -
leetcode 128. Longest Consecutive Sequence 最长连续序列 + HashSet查找的方法
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, ...原创 2017-09-16 12:35:11 · 319 阅读 · 0 评论 -
leetcode 374. Guess Number Higher or Lower 猜数游戏 + 二分查找
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number is higher or原创 2017-10-08 10:31:30 · 347 阅读 · 0 评论 -
leetcode 92. Reverse Linked List II 反转链表 + 头插入反转链表(或者使用栈)
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note: Given m, n satisfy the follo原创 2017-09-12 12:41:16 · 553 阅读 · 0 评论 -
leetcode 328. Odd Even Linked List 奇偶序列的调整 + 暴力做法真棒
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in p原创 2017-10-02 13:29:58 · 306 阅读 · 0 评论 -
leetcode 632. Smallest Range k个数组至少一个元素最小区间+典型移动窗口做法
You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k lists.We define the range [a,b] is smaller than range [c,d] if b-...原创 2017-12-20 14:32:59 · 1823 阅读 · 0 评论 -
leetcode 633. Sum of Square Numbers 二分查找+勾股定理
Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Inp...原创 2017-12-19 20:48:36 · 536 阅读 · 0 评论 -
leetcode 483. Smallest Good Base 最小基数使为1 + 二分查找
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1.Now given a string representing n, you should return the smallest good base of n in string format. Example 1: I...原创 2017-12-15 21:32:19 · 356 阅读 · 0 评论 -
leetcode 480. Sliding Window Median 滑动窗口中位数 + multiset排序
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples: [2,3,4] , the median i原创 2017-12-15 20:43:36 · 581 阅读 · 0 评论 -
leetcode 697. Degree of an Array 等于数组的阶的最短的数组 + 遍历即可
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.Your task is to find the smallest possible length of a ...原创 2017-12-23 18:16:10 · 260 阅读 · 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?建议和leetcode 680. Valid Palindrome II 去除一个字符的回文字符串判断 + 双指针 一起学习反转链表判断即可。代码如下:...原创 2017-09-25 14:38:10 · 306 阅读 · 0 评论 -
leetcode 680. Valid Palindrome II 去除一个字符的回文字符串判断 + 双指针
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Example 1: Input: “aba” Output: True Example 2: Input: “abca” Output: True Explanat...原创 2017-12-23 15:06:25 · 426 阅读 · 0 评论 -
leetcode 668. Kth Smallest Number in Multiplication Table 有序矩阵搜索 + 右上角二分搜索
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?Given the height m and the length n of a m * n Multiplicati...原创 2017-12-22 12:04:33 · 469 阅读 · 0 评论 -
leetcode 667. Beautiful Arrangement II K个间隔的数据分组 + 双指针遍历
Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, … , a...原创 2017-12-22 11:05:42 · 313 阅读 · 0 评论 -
leetcode 658. Find K Closest Elements 寻找绝对距离最近K个元素+ 双指针遍历
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always prefe...原创 2017-12-21 18:01:40 · 1200 阅读 · 1 评论 -
leetcode 424. Longest Repeating Character Replacement 典型移动窗口解决问题
Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repe...原创 2017-12-11 14:28:48 · 447 阅读 · 0 评论 -
leetcode 646. Maximum Length of Pair Chain 最长连续区间 + 排序 + 贪心算法
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain ...原创 2017-12-21 12:58:43 · 683 阅读 · 0 评论 -
leetcode 645. Set Mismatch 寻找缺少元素
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of o...原创 2017-12-19 21:40:10 · 283 阅读 · 0 评论 -
leetcode 457. Circular Array Loop 数组环+数组中寻找环的存在+双指针+循环判断
You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it’s negative (-n), move backward n steps. Assume the first e...原创 2017-12-11 19:02:44 · 1458 阅读 · 0 评论 -
leetcode 443. String Compression 压缩字符串+暴力遍历
Given an array of characters, compress it in-place.The length after compression must always be smaller than or equal to the original array.Every element of the array should be a character (not int...原创 2017-12-09 15:35:24 · 596 阅读 · 0 评论 -
leetcode 85. Maximal Rectangle 最大子矩阵 + DP + 转换为最大直方图
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0原创 2017-09-12 10:32:23 · 830 阅读 · 0 评论 -
leetcode 11. Container With Most Water 双指针 + 最短木桶理论
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 lin原创 2017-08-30 15:05:54 · 593 阅读 · 0 评论 -
leetcode 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.这道题很简单,就是取出链表的重复元素,遍历一次即可。代码如下:原创 2017-09-12 11:04:03 · 187 阅读 · 0 评论 -
leetcode 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2原创 2017-09-11 22:06:16 · 257 阅读 · 0 评论 -
leetcode 206. Reverse Linked List 反转字符串
Reverse a singly linked list.反转链表,我这里是采用头插法来实现反转链表。代码如下:/*class ListNode{ int val; ListNode next; ListNode(int x) { val = x; }}*/public class Solution{ public ListNode revers原创 2017-09-21 09:33:20 · 219 阅读 · 0 评论 -
leetcode 205. Isomorphic Strings 同构字符串判断 + HashMap
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara原创 2017-09-21 09:24:29 · 278 阅读 · 0 评论 -
leetcode 160. Intersection of Two Linked Lists 链表公共节点
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ...原创 2017-09-19 09:13:17 · 511 阅读 · 0 评论 -
leetcode 150. Evaluate Reverse Polish Notation 逆波兰表达式的计算
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: [“2”, “1”, “+”, “3”, ““]原创 2017-09-18 09:39:23 · 464 阅读 · 0 评论 -
leetcode 148. Sort List 链表归并排序
Sort a linked list in O(n log n) time using constant space complexity.本题就是考察的是链表的归并排序。代码如下:/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}*/public class So原创 2017-09-18 09:26:52 · 540 阅读 · 0 评论 -
leetcode 143. Reorder List 双指针
Given a singly linked list L: L0?L1?…?Ln-1?Ln, reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}原创 2017-09-17 13:34:24 · 364 阅读 · 0 评论 -
leetcode 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nod原创 2017-08-31 19:33:42 · 253 阅读 · 0 评论 -
leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no原创 2017-08-31 17:44:53 · 253 阅读 · 0 评论 -
leetcode 109. Convert Sorted List to Binary Search Tree 链表构造平衡二叉搜索树 + DFS
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.根据一个排序好的链表构造二叉平衡树,这个问题的重点是找到中间元素,使用双指针即可。建议和 leetcode 108. Convert Sorted Array to Binary Se原创 2017-09-14 11:13:48 · 431 阅读 · 0 评论