博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode——双指针的算法问题
阅读量:589 次
发布时间:2019-03-11

本文共 3027 字,大约阅读时间需要 10 分钟。

两个数组的交集II

package 计算机程序算法分类.双指针问题;import java.util.*;/** * @Classname 两个数组的交集 * @Description TODO * @Date 2021/4/12 21:18 * @Created by xjl */public class 两个数组的交集 {    /**     * @description TODO  采用的是的双指针来实现 这个题目是知识保留一份的结果     * @param: nums1     * @param: nums2     * @date: 2021/4/13 14:05     * @return: int[]     * @author: xjl     */    public int[] intersect(int[] nums1, int[] nums2) {        // 先对两个数组进行排序        Arrays.sort(nums1);//时间复杂度为O(nlog(n))        Arrays.sort(nums2);        int i = 0;        int j = 0;        //存放的结果        HashSet
set = new HashSet<>(); while (i < nums1.length && j < nums2.length) { if (nums1[i] < nums2[j]) { i++; } else if (nums1[i] > nums2[j]) { j++; } else { set.add(nums1[i]); i++; j++; } } ArrayList
list = new ArrayList<>(set); return list.stream().mapToInt(Integer::intValue).toArray(); } /** * @description TODO 这个题目是知识保留一份的结果 * @param: nums1 * @param: nums2 * @date: 2021/4/13 14:41 * @return: int[] * @author: xjl */ public int[] intersection(int[] nums1, int[] nums2) { ArrayList
result = new ArrayList(); ArrayList
list = new ArrayList<>(); for (int i : nums1) { list.add(i); } for (int i : nums2) { if (list.contains(i)&&!result.contains(i)) { result.add(i); } } return result.stream().mapToInt(Integer::intValue).toArray(); } /** * @description TODO 采用的是的双指针来实现 保留所有的重复的结果 * @param: nums1 * @param: nums2 * @date: 2021/4/13 14:05 * @return: int[] * @author: xjl */ public int[] intersect_2(int[] nums1, int[] nums2) { // 先对两个数组进行排序 Arrays.sort(nums1);//时间复杂度为O(nlog(n)) Arrays.sort(nums2); int i = 0; int j = 0; //存放的结果 List
list = new ArrayList<>(); while (i < nums1.length && j < nums2.length) { if (nums1[i] < nums2[j]) { i++; } else if (nums1[i] > nums2[j]) { j++; } else { list.add(nums1[i]); i++; j++; } } return list.stream().mapToInt(Integer::intValue).toArray(); } public int[] intersection2(int[] nums1, int[] nums2) { Set
set = new HashSet<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i = 0, j = 0; while (i < nums1.length && j < nums2.length) { if (nums1[i] == nums2[j]) { set.add(nums1[i]); i++; j++; } else if (nums1[i] < nums2[j]) { i++; } else if (nums1[i] > nums2[j]) { j++; } } int[] res = new int[set.size()]; int index = 0; for (int num : set) { res[index++] = num; } return res; }}

双指针解替换后的最长重复字符双指针验证回文串

 

 

使用快慢指针把有序链表转换二叉搜索树快慢指针解决环形链表

 

 

双指针解旋转链表

 

双指针判断的环的入口位置

 

双指针求无重复字符的最长子串双指针求接雨水问题

 

双指针求盛最多水的容器

 

链表的无序变有序(不能使用额外的空间)

 

双指针做链表的相加 归并算法 数组的合并

 

 

转载地址:http://ccltz.baihongyu.com/

你可能感兴趣的文章