当前位置: 首页 > news >正文

今日算法题

import java.util.*;public class test_04_15 {//合并两个有序数组public void merge(int[] nums1, int m, int[] nums2, int n) {int nm = nums1.length-1;int i=m-1;int j=n-1;while (i>=0&&j>=0) {if (nums1[i] > nums2[j]) {nums1[nm--] = nums1[i--];} else {nums1[nm--] = nums2[j--];}}while (i>=0){nums1[nm--] = nums1[i--];}while (j>=0){nums1[nm--] = nums2[j--];}}//无重复字符的最长子串//abcabcbbpublic static int lengthOfLongestSubstring(String s) {if (s.length()==0){return 0;}int start=0;int end=0;int maxRes = 1;HashMap<Character,Integer> hashMap = new HashMap<>();while (end<s.length()){//不存在重复if (hashMap.containsKey(s.charAt(end))&&hashMap.get(s.charAt(end))>=start){//存在重复的start=hashMap.get(s.charAt(end))+1;}else{maxRes=Math.max(maxRes,end-start+1);hashMap.put(s.charAt(end),end);end++;}}return maxRes;}//判断子序列public boolean isSubsequence(String s, String t) {int i=0;int j=0;while (j<t.length()) {if (s.charAt(i) == t.charAt(j)) {i++;}j++;}return i==s.length();}//删除有序数组中的重复项//1 1 2 3public static int removeDuplicates(int[] nums){int i=0;int j=0;while (j<nums.length-1){if (nums[j]==nums[j+1]){j++;}else{nums[i]=nums[j];i++;j++;}}nums[i++]=nums[j];return i;}public static int maxArea(int[] height) {int start = 0;int end = height.length-1;int cap = 0;while (start<end){cap = Math.max(cap,Math.min(height[start],height[end])*(end-start));if (height[start]<height[end]){start++;}else{end--;}}return cap;}//三数之和public static List<List<Integer>> threeSum(int[] nums) {Arrays.sort(nums);System.out.println(Arrays.toString(nums));List<List<Integer>> res = new ArrayList<>();for (int i=0;i<nums.length;i++){//获取第一个数int temp1 = -nums[i];int j=0;int k=nums.length-1;while (j<k){if (j==i){j++;continue;}if (k==i){k--;continue;}if (nums[j]+nums[k]<temp1){j++;}else if (nums[j]+nums[k]>temp1){k--;}else{List<Integer> list = Arrays.asList(nums[i], nums[j], nums[k]);Collections.sort(list);if (!res.contains(list)){res.add(list);}j++;k--;}}}return res;}//[-1,0,1,2,-1,-4]//[-4,-1,-1,0,1,5]public static List<List<Integer>> threeSum2(int[] nums) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(nums);System.out.println(Arrays.toString(nums));for (int i=0;i<nums.length-2;i++){//跳过重复数字if (i>0&&nums[i]==nums[i-1]){continue;}int j=i+1;int k=nums.length-1;while (j<k){if (nums[j]+nums[k]==-nums[i]){res.add(Arrays.asList(nums[i],nums[j],nums[k]));//去除重复的while (j<k&&nums[j]==nums[j+1]){j++;}while (j<k&&nums[k]==nums[k-1]){k--;}j++;k--;}else if (nums[j]+nums[k]>-nums[i]){k--;}else{j++;}}}return res;}//反转链表public ListNode reverseList(ListNode head) {ListNode pre = null;ListNode p =head;ListNode pp=p.next;while (pp!=null){p.next=pre;pre=p;p=pp;pp=pp.next;}p.next=pre;return p;}// 1 2public ListNode reverseList2(ListNode head) {if (head.next==null){return head;}ListNode newHead = reverseList2(head.next);head.next.next=head;head.next=null;return newHead;}//环形链表public boolean hasCycle(ListNode head) {ListNode slow = head;ListNode quick = head;while (quick!=null&&quick.next!=null){slow=slow.next;quick=quick.next.next;if (slow==quick){return true;}}return false;}//反转链表IIpublic static ListNode reverseBetween(ListNode head, int left, int right) {ListNode rightNode = head;ListNode leftNode = head;ListNode leftNodePre = null;while (left-->1){leftNodePre=leftNode;leftNode=leftNode.next;}while (right-->1){rightNode=rightNode.next;}ListNode copyLeftNode = leftNode;// 1 2 3 4 5ListNode pre = null;ListNode p=leftNode;ListNode pp=p.next;while (p!=rightNode){p.next=pre;pre=p;p=pp;pp=pp.next;}p.next=pre;copyLeftNode.next=pp;if (leftNodePre==null){return p;}else{leftNodePre.next=p;return head;}}//k个一组反转链表public static ListNode reverseKGroup(ListNode head, int k) {int count = 0;ListNode p = head;while (p!=null){count++;p=p.next;}int left=1;for (int i=0;i<count/k;i++){head = reverseBetween(head, left, left + k - 1);left=left+k;}return head;}//删除链表中的节点public void deleteNode(ListNode node) {node.val=node.next.val;node.next=node.next.next;}public ListNode deleteNode2(ListNode head,ListNode node) {ListNode dumpy = new ListNode(-1);dumpy.next=head;ListNode nodePre = dumpy;while (nodePre.next!=node){nodePre=nodePre.next;}nodePre.next=node.next;return dumpy.next;}//重排链表//1 2//5 4 3public static void reorderList(ListNode head) {//找出中间节点ListNode slow = head;ListNode quick = head;ListNode slowPre = null;while (quick!=null&&quick.next!=null){slowPre = slow;slow= slow.next;quick=quick.next.next;}if (slowPre==null){return;}else{slowPre.next=null;}//将后面链表进行反转ListNode pre = null;ListNode p = slow;ListNode pp=slow.next;while (pp!=null){p.next=pre;pre = p;p=pp;pp=pp.next;}p.next=pre;//开始进行合并//1 2//4 3ListNode l1 = head;ListNode l2 = p;ListNode l2Pre;ListNode.printList(l1);ListNode.printList(l2);while (l1!=null){l2Pre=l2;l2=l2.next;l2Pre.next=l1.next;l1.next=l2Pre;l1=l1.next.next;}if (l2!=null){p=head;while (p.next!=null){p=p.next;}p.next=l2;}}//删除链表倒数第n个节点public ListNode removeNthFromEnd(ListNode head, int n) {ListNode pre = null;ListNode p = head;ListNode temp = head;while (n-->0){temp=temp.next;}while (temp!=null){temp=temp.next;pre=p;p=p.next;}if (pre==null){return head.next;}else{pre.next=pre.next.next;}return head;}//旋转链表public ListNode rotateRight(ListNode head, int k) {if (head==null||head.next==null){return head;}int count = 1;ListNode newHead = new ListNode(head.val);ListNode q=newHead;ListNode p = head.next;ListNode pre = head;while (p!=null){q.next=new ListNode(p.val);q=q.next;pre=p;p=p.next;count++;}pre.next=newHead;int num = count-(k%count);p=head;while (num-->0){p=p.next;}ListNode right = p;while (--count>0){right=right.next;}right.next=null;return p;}public static void main(String[] args) {ListNode head1 = new ListNode(1);ListNode head2 = new ListNode(2);ListNode head3 = new ListNode(3);head1.next=head2;head2.next=head3;reorderList(head1);ListNode.printList(head1);}
}

相关文章:

  • 安装 MySQL8.0.17
  • Selenium2+Python自动化:利用JS解决click失效问题
  • GitHub开源项目esp32小智AI语音代码详解
  • 【C语言基础】C++ 中的 `vector` 及其 C 语言实现详解
  • 力扣 双指针算法(一)
  • 每日一题-力扣-2537. 统计好子数组的数目 0416
  • Java高频面试之并发编程-03
  • Qt QML实现Windows桌面颜色提取器
  • JVM:类加载子系统
  • android rtsp 拉流h264 h265,解码nv12转码nv21耗时卡顿问题及ffmpeg优化
  • 基于多模态深度学习的亚急性脊髓联合变性全流程预测与个性化管理技术方案
  • JVM:对象的实例化、直接内存
  • cfd笔记【1】简介
  • vue js 上传文件 form data
  • 一个含有 n 个顶点的连通且无环的简单无向图,在其邻接矩阵存储结构中共有几个零元素?
  • Hadoop集群部署教程-END
  • 【25软考网工笔记】第二章 数据通信基础(4)数据编码
  • Flask快速入门
  • Flask(1): 在windows系统上部署项目1
  • android11通过白名单卸载安装应用
  • 关于文案的网站/google框架一键安装
  • wordpress reference/南宁网站seo外包
  • 网站建设项目规划书案例/免费发广告网站
  • django 开放api 做网站/免费seo视频教学
  • 网站建设的基本技术/合肥seo网络优化公司
  • 杭州响应式网站制作/网页设计代码大全