LeetCode 今天搞两个(3,4)

问题一:

对于给定的n个位于同一二维平面上的点,求最多能有多少个点位于同一直线上
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
 
思路:利用斜率相等思想求解
  注意特殊情况: 
  1.存在重复的点
  2.斜率不存在的情况
  3.斜率为零的情况
import java.util.Map;
import java.util.HashMap;
/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    public int maxPoints(Point[] points) {
        if(points == null){
            return 0;
        }
        int len = points.length;
        if(len <= 2){
            return len;
        }
        Map<Double,Integer> map = new HashMap<Double,Integer>();
        int result = 0;
        for(int i=0;i < len;i++){
            map.clear();
            int max = 0;
            int repeat = 0;
            int vertical =0; 
            int horizontal = 0;
            double k = 0.0;
            for(int j =i + 1;j < len;j++){
                double xa = new Double(points[i].x) - new Double(points[j].x);
                double ya = new Double(points[i].y) - new Double(points[j].y);
                if(xa == 0.0 && ya == 0.0){
                    repeat++;
                    continue;
                }else if(xa == 0){
                    vertical++;
                    max = getMax(max,vertical);
                }else if(ya == 0.0){
                    horizontal++;
                    max = getMax(max,horizontal);
                }else{
                    k = ya/xa;
                    if(map.containsKey(k)){
                        map.put(k,map.get(k)+1);
                    }else{
                        map.put(k,1);
                    }
                    max = getMax(max,map.get(k));
                }                    
            }
            result = getMax(result,max+repeat+1);
        }
        return result;
    }
    public int getMax(int a,int b){
        return a > b ? a : b;
    }
}

问题二:

  问题描述:

在O(n log n)的时间内使用常数级空间复杂度对链表进行排序。
Sort a linked list in O(n log n) time using constant space complexity.
 
考虑复杂度为O(n log n)排序算法
(1)堆排序   时间复杂度虽然满足要求,但是在实现过程中存在大量数据交换以及多链表的索引,对于链表这种数据结构,插入和删除节点时间复杂度为O(1)
        但是索引复杂度就会提高,不适合用堆排序来实现链表的排序;
(2)快速排序  时间复杂度满足要求,空间复杂度为O(log n) ,在实现过程中也存在大量的节点交换,不适合用作链表的排序;
(3)归并排序  时间复杂度满足要求,在针对数组排序,空间复杂度为O(log(n)) 但是针对链表而言,可以不用额外的开辟空间,可以达到常数空间复杂度
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode start = head;
        ListNode mid = getMid(head);
        ListNode end = mid.next;
        mid.next = null;
        return merge(sortList(start),sortList(end));
        
    }
    public ListNode merge(ListNode node1, ListNode node2){
        if(node1 == null) {
            return node2;
        }
        if(node2 == null){
            return node1;
        }
        ListNode min = new ListNode(-1);
        ListNode cur = min;
        while(node1!=null && node2!=null){
            if(node1.val < node2.val){
                cur.next = node1;
                node1 = node1.next;
            }else{
                cur.next = node2;
                node2 = node2.next;
            }
            cur = cur.next;
        }
        if(node1 != null){
            cur.next = node1;
        }
        if(node2 != null ){
            cur.next = node2;
        }
        return min.next;
    }
// 采用快慢指针获取链表的中间节点
    public ListNode getMid(ListNode node){
        if(node == null || node.next == null){
            return node;
        }
        ListNode slow = node;
        ListNode fast = node;
        while(fast.next != null && fast.next.next!= null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
原文地址:https://www.cnblogs.com/lc1475373861/p/12013496.html