16. 3Sum Closest

这道题让我们求最接近给定值的三数之和,是在之前那道3Sum 三数之和的基础上又增加了些许难度,那么这道题让我们返回这个最接近于给定值的值,即我们要保证当前三数和跟给定值之间的差的绝对值最小,所以我们需要定义一个变量result用来记录当前最小三个数的和,然后我们还是要先将数组排个序,然后开始遍历数组,思路跟那道三数之和很相似,都是先确定一个数,然后用两个指针left和right来滑动寻找另外两个数,每确定两个数,我们求出此三数之和sum,然后算和给定值的差的绝对值abs(sum-target),然后和abs(result-target)比较并更新结果result即可,代码如下:

  1. public class Solution {
  2. public int threeSumClosest(int[] num, int target) {
  3. int result = num[0] + num[1] + num[num.length - 1];//初始化
  4. Arrays.sort(num);
  5. //选择一个数
  6. for (int i = 0; i < num.length - 2; i++) {
  7. int start = i + 1, end = num.length - 1;//双指针指向最小数和最大数
  8. while (start < end) {
  9. int sum = num[i] + num[start] + num[end];
  10. if (sum > target) {
  11. //大了,后一个指针前移
  12. end--;
  13. } else {
  14. //小了,前一个指针后移
  15. start++;
  16. }
  17. if (Math.abs(sum - target) < Math.abs(result - target)) {
  18. result = sum;
  19. }
  20. }
  21. }
  22. return result;
  23. }
  24. }

15. 3Sum

这道题让我们求三数之和,返回符合条件的集合

和上面一样,除了需要跳过相同的数字避免集合出现重复

  1. public List<List<Integer>> threeSum(int[] num) {
  2. Arrays.sort(num);
  3. List<List<Integer>> res = new LinkedList<>();
  4. for (int i = 0; i < num.length-2; i++) {
  5. if (i == 0 || (i > 0 && num[i] != num[i-1])) {
  6. int lo = i+1, hi = num.length-1, sum = 0 - num[i];
  7. while (lo < hi) {
  8. if (num[lo] + num[hi] == sum) {
  9. res.add(Arrays.asList(num[i], num[lo], num[hi]));
  10. while (lo < hi && num[lo] == num[lo+1]) lo++;//skip
  11. while (lo < hi && num[hi] == num[hi-1]) hi--;//skip
  12. lo++; hi--;
  13. } else if (num[lo] + num[hi] < sum) lo++;
  14. else hi--;
  15. }
  16. }
  17. }
  18. return res;
  19. }

Arrays.asList的用法

使用工具类Arrays.asList()把数组转换成集合时,不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportOperationException异常
说明:asList的返回对象是一个Arrays内部类,并没有实现集合的修改方法。Arrays.asList体现的是适配器模式,只是转换接口,后台的数据仍是数组。
String[] str = new String[]{"1","2"};
List list = Arrays.asList(str);
第一种情况:list.add("x");//运行时异常
第二种情况:str[0] = "unv";//那么list.get(0)也随着修改。

61. Rotate List

给定一个链表,以及一个整数k,返回链表右旋k个元素后的结果。

要使得链表右旋k个元素,也就是说明链表后(k)个元素将成为新链表的前半部分,原链表的前(len-k)个元素将成为新链表的后半部分;

此时原链表的head前恰好有k 个元素,即完成了右旋k个位置。

  1. public ListNode rotateRight(ListNode head, int n) {
  2. if (head==null||head.next==null) return head;
  3. ListNode dummy=new ListNode(0);
  4. dummy.next=head;
  5. ListNode fast=dummy,slow=dummy;
  6.  
  7. int i;
  8. for (i=0;fast.next!=null;i++)//Get the total length
  9. fast=fast.next;
  10.  
  11. for (int j=i-n%i;j>0;j--) //Get the i-n%i th node
  12. slow=slow.next;
  13.  
  14. fast.next=dummy.next; //Do the rotation
  15. dummy.next=slow.next;
  16. slow.next=null;
  17.  
  18. return dummy.next;
  19. }

82. Remove Duplicates from Sorted List II

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if(head==null) return null;
  3. ListNode FakeHead=new ListNode(0);
  4. FakeHead.next=head;
  5. ListNode pre=FakeHead;
  6. ListNode cur=head;
  7. while(cur!=null){
  8. while(cur.next!=null&&cur.val==cur.next.val){
  9. cur=cur.next;
  10. }
  11. if(pre.next==cur){
  12. pre=pre.next;//没有遇到跳过的,移动pre
  13. }
  14. else{
  15. pre.next=cur.next;//有重复的,跳过了一部分,不移动pre
  16. }
  17. cur=cur.next;
  18. }
  19. return FakeHead.next;
  20. }

leetcode two pointer的更多相关文章

  1. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  3. LeetCode: Populating Next Right Pointer in Each Node

    LeetCode: Populating Next Right Pointer in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

  4. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  5. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  6. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  7. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  8. LeetCode OJ--Copy List with Random Pointer **

    https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 灵活的指针链表应用. 每个节点有两个指针next,random,对本链表 ...

  9. Java for LeetCode 138 Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

随机推荐

  1. Android虚拟、实体键盘不能同时使用?

    /****************************************************************************** * Android虚拟.实体键盘不能同时 ...

  2. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

  3. Codeforces 954H Path Counting 【DP计数】*

    Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...

  4. Atocder ARC082 F-Sandglass 【思维题】*

    Atocder ARC082 F-Sandglass Problem Statement We have a sandglass consisting of two bulbs, bulb A and ...

  5. sql中的一些函数(长期更新。。)

    前言 在最近看别人的sql的时候,看到一些函数,比如left(),right()等等,好奇是什么意思,查询之后觉得还是挺有用的,特此记录下来.博客会在遇到新的函数的时候定期更新. 正文 1. left ...

  6. HDFS原理分析之HA机制:avatarnode原理

    一.问题描述 由于namenode 是HDFS的大脑,而这个大脑又是单点,如果大脑出现故障,则整个分布式存储系统就瘫痪了.HA(High Available)机制就是用来解决这样一个问题的.碰到这么个 ...

  7. [MEF]第02篇 MEF的导入导出契约

    一.演示概述此演示介绍了如何为Export指定导出的协议名和类型,以及如何为Import指定导入的协议名和类型,只有确保导出和导入的协议名和类型相匹配了,才能注入成功,否则注入就会失败.相关下载(屏幕 ...

  8. Toxiproxy 网络情况模式代理

    1. 介绍 Toxiproxy is a framework for simulating network conditions. It's made specifically to work in ...

  9. Oracle 之 Cloning $oracle_home (克隆安装oracle软件)

    用途:Cloning an Oracle Home , 可以免去多台机器重复安装oracle软件 1.停止相关进程[root@node1 bin]# ./crsctl stop cluster -al ...

  10. java nio和bio

    理解同步/异步,阻塞/非阻塞:https://juejin.im/entry/598da7d16fb9a03c42431ed3 2:http://qindongliang.iteye.com/blog ...