随笔分类 - 两个指针
摘要:题目: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, g...
阅读全文
摘要: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...
阅读全文
摘要:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After ...
阅读全文
摘要:思路:因为nums1的空间>= m+n,故可以用三个指针, k = m+n-1, i = m - 1, j = n - 1,操作都在数组nums1上进行,最后再把nums2中剩余的元素拷贝到nums1中即可。Java代码如下: public void merge(int[] nums1, in...
阅读全文
摘要:思路:数组元素删除操作较麻烦,这里采用的是交换元素法,利用两个指针分别指数组的开头下标和尾部下标,把要删除的元素放在数组的最后,最后返回前面指针的下标。Java代码: public int removeElement(int[] nums, int val) { int l = ...
阅读全文
摘要:思路:最大面积等于两条垂直线中较小的一条乘以与x轴相交的长度,可以使用Two Pointers,一个从数组头开始(记为i),一个从数组尾部开始(记为j),当i result) result = tmp; if (height[i] < heigh...
阅读全文
浙公网安备 33010602011771号