博客
关于我
LeetCode——双指针的算法问题
阅读量:596 次
发布时间: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/

你可能感兴趣的文章
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>