刷题打卡第二天

刷题打卡第二天

#2简单, 3 中等 ,2困难

2022-11-18

牛客-面试高频榜单

2 简单 3 中等 1困难

61 两数之和

给出一个整型数组 numbers 和一个目标值 target,请在数组中找出两个加起来等于目标值的数的下标,返回的下标按升序排列。(注:返回的数组下标从1开始算起,保证target一定可以由数组里面2个数字相加得到)

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        if(numbers.length<2) return new int[2];
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0 ; i<numbers.length ;i++){
            if(map.containsKey(target-numbers[i])){
                if(i>map.get(target-numbers[i])){
                    return new int[]{map.get(target-numbers[i])+1,i+1};
                }else{
                    return new int[]{i+1,map.get(target-numbers[i])+1};
                }
                
            }else{
                map.put(numbers[i],i);
            }
        }
        return new int[2];
    }
}

33 合并两个排序的链表

不额外空间

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null) return list2;
        if(list2==null) return list1;
        
        ListNode temp = null;
        if(list1.val >list2.val){
           temp = list1;
           list1 = list2;
           list2 = temp;
        }
        ListNode res = list1;
        while(list1.next!=null && list2!=null){
            if(list1.next.val <=list2.val){
                list1 = list1.next;
            }
            else{
                temp = list1.next;
                list1.next = list2;
                list2 = list2.next;
                list1 = list1.next;
                list1.next = temp;
            }
        }
        if(list2!=null)
        {
            list1.next = list2;
        }
        return res;
    }
}

88 无序寻找第K大

有一个整数数组,请你根据快速排序的思路,找出数组中第 k 大的数。

给定一个整数数组 a ,同时给定它的大小n和要找的 k ,请返回第 k 大的数(包括重复的元素,不用去重),保证答案存在。

要求:时间复杂度 O(nlogn),空间复杂度 O(1)

import java.util.*;

public class Solution {
    public int findKth(int[] a, int n, int K) {
        // write code here
        return quickS(a,0,a.length-1,K);
    }
    public int quickS(int[] a,int left ,int right ,int k){
        //if(left==right) return a[left];
        int p = par(a,left,right);
            if(p==a.length-k){
                return a[p];
            }if(p>a.length-k){
                 return quickS(a,left,p-1,k);
            }else{
                return quickS(a,p+1 ,right,k);
            }
        
    }
    public int par(int[] a ,int left ,int right){
        int first = a[left];
        int l = left;
        while(left<right){
            while(left<right && a[right]>=first) right--;
            while(left<right && a[left]<=first) left++;
            swap(a,left,right);
        }
        swap(a,left,l);

        return left;
    }
    public void swap(int[] a ,int i ,int j){
        int t = a[i];
        a[i] = a[j] ;
        a[j] = t;

    }
}

50 链表中的节点每k个一组翻转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        ListNode tail = head;
        for(int i=0 ;i<k; i++){
            if(tail==null) return head;
            tail = tail.next;
        }
        ListNode pre  = null;
        ListNode cur = head;
        while(cur!=tail)
        {
            ListNode t = cur.next;
            cur.next = pre;
            pre = cur;
            cur = t;

        }
        head.next = reverseKGroup(tail,k);
        return pre;
    }
}

41 最长无重复子数组

给定一个长度为n的数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同。

子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组

import java.util.*;


public class Solution {
    /**
     * 
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int maxLength (int[] arr) {
        // write code here
        Queue<Integer> q = new LinkedList<>();
        int res = 0;
        for(int c : arr)
        {
            while(q.contains(c)){
                q.poll();
            }
            q.add(c);
            res = Math.max(res , q.size());
        }
        return res;
    }
}

51 合并k个已排序的链表

合并 k 个升序的链表并将结果作为一个升序的链表返回其头节点。

import java.util.*;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        return divi(lists,0,lists.size()-1);
    }
    public ListNode divi(ArrayList<ListNode> lists ,int left , int right ){
        if(left>right) return null;
        if(left==right) return lists.get(left);
        
        int mid = left+((right-left)>>1);

        return merge(divi(lists,left,mid),divi(lists,mid+1,right));
    }
    public ListNode merge(ListNode list1,ListNode list2){
        //一个已经为空了,直接返回另一个
        if(list1 == null)
            return list2;
        if(list2 == null)
            return list1;
        //加一个表头
        ListNode head = new ListNode(0);
        ListNode cur = head;
        //两个链表都要不为空
        while(list1 != null && list2 != null){
            //取较小值的节点
            if(list1.val <= list2.val){
                cur.next = list1;
                //只移动取值的指针
                list1 = list1.next;
            }else{
                cur.next = list2;
                //只移动取值的指针
                list2 = list2.next;
            }
            //指针后移
            cur = cur.next;
        }
        //哪个链表还有剩,直接连在后面
        if(list1 != null)
            cur.next = list1;
        else
            cur.next = list2;
        //返回值去掉表头
        return head.next;
    }
    
}

 }
    public ListNode merge(ListNode list1,ListNode list2){
        //一个已经为空了,直接返回另一个
        if(list1 == null)
            return list2;
        if(list2 == null)
            return list1;
        //加一个表头
        ListNode head = new ListNode(0);
        ListNode cur = head;
        //两个链表都要不为空
        while(list1 != null && list2 != null){
            //取较小值的节点
            if(list1.val <= list2.val){
                cur.next = list1;
                //只移动取值的指针
                list1 = list1.next;
            }else{
                cur.next = list2;
                //只移动取值的指针
                list2 = list2.next;
            }
            //指针后移
            cur = cur.next;
        }
        //哪个链表还有剩,直接连在后面
        if(list1 != null)
            cur.next = list1;
        else
            cur.next = list2;
        //返回值去掉表头
        return head.next;
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值