楼主: 开水不开
跳转到指定楼层
上一主题 下一主题
收起左侧

转码野生老农刷题打卡

🔗
 楼主| 开水不开 2023-3-30 10:16:14 | 只看该作者
全局:
2023-03-10
剑指 Offer 03. 数组中重复的数字
这个都没一次过。。。疯了
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-3-30 11:18:47 | 只看该作者
全局:
2023-03-30
852. Peak Index in a Mountain Array
binary search经典题目
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-3-31 17:14:43 | 只看该作者
全局:
2023-03-31
215. Kth Largest Element in an Array
这题太好了,考了小顶堆,堆的实现
快排或者归并排序。
最牛逼的还是看了下题解,大神太牛了,用的是快排的思想,快速定位position。服了,直接贴代码
  1. class Solution {
  2.     private final static Random random = new Random();
  3.     public int findKthLargest(int[] nums, int k) {
  4.         int len = nums.length;

  5.         //1st len - 1;
  6.         //2nd len - 2;
  7.         //the kth largest number target index after sort is len - k
  8.         int target = len - k;
  9.         
  10.         //binery search for a random number's positions
  11.         //searching range shrink every search
  12.         //why random? cuz inverse order array will lead the algrithm time cost become o(N2);
  13.         int left = 0;
  14.         int right = len - 1;
  15.         
  16.         //there must a position equals to target
  17.         while(true){
  18.             //find a random numbers position
  19.             int pivodPosition = partition(nums, left, right);
  20.             if(pivodPosition == target){
  21.                 return nums[pivodPosition];
  22.             }else if(pivodPosition < target){
  23.                 left = pivodPosition + 1;
  24.             }else if(pivodPosition > target){
  25.                 right = pivodPosition - 1;
  26.             }

  27.         }


  28.     }

  29.     private int partition(int[] nums, int left, int right){
  30.         int randomIndex = left + random.nextInt(right - left + 1);
  31.         //random find a pivot , temprory put it on the first ele in range
  32.         swap(nums, left, randomIndex);
  33.         int pivot = nums[left];
  34.         int le = left + 1;
  35.         int ge = right;

  36.         while(true){
  37.             //exclusive the lesser ele
  38.             while(le <= ge && nums[le] < pivot){
  39.                 le++;
  40.             }
  41.             //exclusive greater ele
  42.             while(le <= ge && nums[ge] > pivot){
  43.                 ge--;
  44.             }
  45.             //found, ge is the position, cus le move first, so le will stop at the first ele larger than pivot's position
  46.             if(le >= ge){
  47.                 break;
  48.             }
  49.             //if stuck, swich two pins and continue
  50.             swap(nums,le, ge);
  51.             le++;
  52.             ge--;
  53.             

  54.         }

  55.         //put the pivot in right position
  56.         swap(nums,left, ge);
  57.         return ge;

  58.     }

  59.     private void swap(int[] nums, int a , int b){
  60.         int tmp = nums[a];
  61.         nums[a] = nums[b];
  62.         nums[b] = tmp;
  63.     }

  64. }
复制代码
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-3 15:30:19 | 只看该作者
全局:
2023-04-03
148. Sort List
想到归并,但是这题怎么也想不出怎么O(1)空间复杂度。题解太巧妙了。一段段的排序。。直接贴代码吧
  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode() {}
  7. *     ListNode(int val) { this.val = val; }
  8. *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12.     public ListNode sortList(ListNode head) {
  13.         if (head == null) {
  14.             return head;
  15.         }
  16.         //计算长度
  17.         int length = 0;
  18.         ListNode node = head;
  19.         while (node != null) {
  20.             length++;
  21.             node = node.next;
  22.         }
  23.         //哨兵
  24.         ListNode dummyHead = new ListNode(0, head);
  25.         //每轮循环排序subLength长度的链表
  26.         for (int subLength = 1; subLength < length; subLength <<= 1) {
  27.             ListNode prev = dummyHead, curr = dummyHead.next;
  28.             while (curr != null) {
  29.                 //拆分前半段长度为sublength的链表
  30.                 ListNode head1 = curr;
  31.                 for (int i = 1; i < subLength && curr.next != null; i++) {
  32.                     curr = curr.next;
  33.                 }
  34.                 //记录后半段的头,拆分后半段sublength链表
  35.                 ListNode head2 = curr.next;
  36.                 curr.next = null;//切断第一段和第二段
  37.                 curr = head2;
  38.                 for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {
  39.                     curr = curr.next;
  40.                 }
  41.                 //记录下一次循环排序的链表头
  42.                 ListNode next = null;
  43.                 if (curr != null) {
  44.                     next = curr.next;
  45.                     curr.next = null;//切断第二段链表尾部
  46.                 }
  47.                 //合并第一段和第二段
  48.                 ListNode merged = merge(head1, head2);
  49.                 //连接已经排序好的sublength * 2链表
  50.                 prev.next = merged;
  51.                 //将prev移动到sublength的末尾
  52.                 while (prev.next != null) {
  53.                     prev = prev.next;
  54.                 }
  55.                 //curr移动到未排序部分的链表头
  56.                 curr = next;
  57.             }
  58.         }
  59.         return dummyHead.next;
  60.     }

  61.     public ListNode merge(ListNode head1, ListNode head2) {
  62.         ListNode dummyHead = new ListNode(0);
  63.         ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
  64.         while (temp1 != null && temp2 != null) {
  65.             if (temp1.val <= temp2.val) {
  66.                 temp.next = temp1;
  67.                 temp1 = temp1.next;
  68.             } else {
  69.                 temp.next = temp2;
  70.                 temp2 = temp2.next;
  71.             }
  72.             temp = temp.next;
  73.         }
  74.         if (temp1 != null) {
  75.             temp.next = temp1;
  76.         } else if (temp2 != null) {
  77.             temp.next = temp2;
  78.         }
  79.         return dummyHead.next;
  80.     }
  81. }
复制代码
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-4 23:04:00 | 只看该作者
全局:
2023-04-04
剑指 Offer 59 - I. 滑动窗口的最大值
首先想到双端队列,就是忘了怎么写出来
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-6 13:50:30 | 只看该作者
全局:
2023-04-06
55. Jump Game
挨着跳,能跳的范围大于length-1了就说明ok了。
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-7 11:46:30 | 只看该作者
全局:
2023-04-07
剑指 Offer 58 - II. 左旋转字符串
过过过
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-10 10:53:15 | 只看该作者
全局:
2023-04-10
658. Find K Closest Elements
方法1:先二分找到最接近x的,然后双指针向两边找。
方法2:直接用compartor排序,离得近的放前面
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-11 16:13:18 | 只看该作者
全局:
2023-04-11
面试题 08.01. Three Steps Problem LCCI
动态玫瑰花
回复

使用道具 举报

🔗
 楼主| 开水不开 2023-4-12 14:23:02 | 只看该作者
全局:
2023-04-12
153. Find Minimum in Rotated Sorted Array
二分,pivot就是nums[0],
记得要排除nums[0]是最小的情况
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表