LC33. 搜索旋转排序数组
对二分查找的题目一个变种,其实原理是相同的我们都是从有序当中进行二分搜索,那么如何进入有序区间就是根据nums[0]和nums[mid]进行比较判断进入左侧还是右侧区间。
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if(n == 0) return -1;
if(n == 1) return nums[0] == target ? 0 : -1;
int left = 0,right = n - 1;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target) return mid;
//通过中位数判断在并且决定在左侧升序还是右侧升序
if(nums[0] <= nums[mid]){
//target可以在升序区间进行操作
//根据左侧的升序区间中判断target是否在其中然后缩小区间
if(nums[0] <= target && target < nums[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}else{
if(nums[mid] < target && target <= nums[n - 1]){
left = mid + 1;
}else{
right = mid - 1;
}
}
}
return -1;
}
}
LC23. 合并K个升序链表
注:在解决这个题之前可以回看合并两个有序链表的题型(递归以及迭代(使用一个dummy以及一个指针进行遍历合并两个有序链表))
两种解法:
① 递归+归并排序(烧脑),其实原理就是将两个有序的链表抽离出一个方法,并且归并的思想本身就是将两个有序的数组进行一个合并,最后分区间左边和右边得出来的已经排好序的链表,最终进行一个排序即可。
②优先级队列,在java当中采用小顶堆priorityqueue,那么每次pop出来的顶元素永远都是当前顶堆当中最小的那么我们只需要将这些堆顶元素进行一个串联就行
//递归 + 归并思路
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists == null || lists.length == 0) return null;
return merge(lists,0,lists.length - 1);
}
public ListNode merge(ListNode[] lists,int left,int right){
if(left == right) return lists[left];
int mid = left + (right - left) / 2;
ListNode l1 = merge(lists,left,mid);
ListNode l2 = merge(lists,mid + 1 ,right);
return mergeTwoList(l1,l2);
}
public ListNode mergeTwoList(ListNode l1,ListNode l2){
if(l1 == null){
return l2;
}else if(l2 == null){
return l1;
}else if(l1.val < l2.val){
l1.next = mergeTwoList(l1.next,l2);
return l1;
}else{
l2.next = mergeTwoList(l2.next,l1);
return l2;
}
}
}
//优先级队列小顶堆思路
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
//有lambda表达式<>不能省
Queue<ListNode> queue = new PriorityQueue<>((v1,v2)->v1.val - v2.val);
//使用一个虚拟头节点,返回整个链表
ListNode dummy = new ListNode(-1);
//使用一个指针去串联链表
ListNode prev = dummy;
//维护一个小顶堆
for(ListNode node : lists){
if(node != null)
queue.offer(node);
}
while(!queue.isEmpty()){
ListNode minnode = queue.poll();
prev.next = minnode;
prev = minnode;
//如果当前链表仍然有下一个值那么取下一个值放到小顶堆当中
if(minnode.next != null) queue.offer(minnode.next);
}
return dummy.next;
}
}
LC54. 螺旋矩阵
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList();
if(matrix == null || matrix.length == 0)
return res;
int l = 0,t = 0,r = matrix[0].length - 1,b = matrix.length - 1;
int numtarget = matrix.length * matrix[0].length;
while(numtarget >= 1){
for(int i = l;i <= r && numtarget >= 1;i++){
res.add(matrix[t][i]);
numtarget--;
}
t++;
for(int i = t;i <= b && numtarget >= 1;i++){
res.add(matrix[i][r]);
numtarget--;
}
r--;
for(int i = r;i >= l && numtarget >= 1;i--){
res.add(matrix[b][i]);
numtarget--;
}
b--;
for(int i = b;i >= t && numtarget >=1;i--){
res.add(matrix[i][l]);
numtarget--;
}
l++;
}
return res;
}
}