zoukankan      html  css  js  c++  java
  • 双指针思想的题型总结

    将刷题过程中陆续碰到的双指针题型进行一下汇总总结

    题目1:不重复打印排序数组中相加和为给定值的所有二元组

    题目描述

    给定排序数组arr和整数k,不重复打印arr中所有相加和为k的不降序二元组
    例如, arr = [-8, -4, -3, 0, 1, 2, 4, 5, 8, 9], k = 10,打印结果为:
    1, 9
    2, 8
    [要求]
    时间复杂度为O(n)O(n),空间复杂度为O(1)O(1)

    输入描述:

    第一行有两个整数n, k
    接下来一行有n个整数表示数组内的元素

    输出描述:

    输出若干行,每行两个整数表示答案
    按二元组从小到大的顺序输出(二元组大小比较方式为每个依次比较二元组内每个数)

      import java.util.*;
      public class Main{
          public static void main(String[] args){
              Scanner in=new Scanner(System.in);
              int n=in.nextInt();
              int k=in.nextInt();
              int[]a=new int[n];
              for(int i=0;i<n;i++){
                      a[i]=in.nextInt();
              }
              printDouble(a,k);
      
          }
          public static void printDouble(int []a,int k){
              if(a.length<2||a==null) return ;
              int low=0,high=a.length-1;
              while(low<high){
                  if(a[low]+a[high]<k){
                      low++;
                  }else if(a[low]+a[high]>k){
                      high--;
                  }else{
                      if(low==0||a[low-1]!=a[low]){
                          System.out.println(a[low]+ " "+a[high]);
                      }
                      low++;
                      high--;
                  }
              }
          }
      }
      
      
      
      
      

      题目2:

      
      
      
      
    • 相关阅读:
      docker 常用命令
      linux 查看服务器序列号
      centos 7 lsof 安装使用
      Jenkins +svn +maven +tomcat+ ansible 自动化批量部署
      nginx 部署前期一定要关闭selinux
      yum 执行不了, 解决方法
      IIS发布网站
      使用TreeView 使用多选功能
      C#类和接口
      关于C#垃圾回收
    • 原文地址:https://www.cnblogs.com/pathjh/p/12323859.html
    Copyright © 2011-2022 走看看