zoukankan      html  css  js  c++  java
  • LeetCode 725. Split Linked List in Parts

    原题链接在这里:https://leetcode.com/problems/split-linked-list-in-parts/submissions/

    题目:

    Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".

    The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.

    The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.

    Return a List of ListNode's representing the linked list parts that are formed.

    Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]

    Example 1:

    Input: 
    root = [1, 2, 3], k = 5
    Output: [[1],[2],[3],[],[]]
    Explanation:
    The input and each element of the output are ListNodes, not arrays.
    For example, the input root has root.val = 1, root.next.val = 2, 
    oot.next.next.val = 3, and root.next.next.next = null.
    The first element output[0] has output[0].val = 1, output[0].next = null.
    The last element output[4] is null, but it's string representation as a ListNode is [].

    Example 2:

    Input: 
    root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
    Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
    Explanation:
    The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.

    Note:

    • The length of root will be in the range [0, 1000].
    • Each value of a node in the input will be an integer in the range [0, 999].
    • k will be an integer in the range [1, 50].

    题解:

    算出每组有多少个item. 以及多余的个数. 指针跳每组的步数, 如果有多余个数etra, 指针再多跳一步. 在指针后面断开写入res中. extra--.

    Time Complexity: O(n+k). 也有可能k很大, 每组只有一个item.

    Space: O(1).

    AC Java:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 class Solution {
    10     public ListNode[] splitListToParts(ListNode root, int k) {
    11         ListNode [] res = new ListNode[k];
    12         
    13         int len = getLength(root);
    14         ListNode cur = root;
    15         int group = len/k;
    16         int extra = len%k;
    17         
    18         for(int index = 0; index<k && cur!=null; index++){
    19             res[index] = cur;
    20             
    21             for(int i = 0; i<group-1+(extra>0 ? 1 : 0); i++){
    22                 cur = cur.next;
    23             }
    24             
    25             ListNode mark = cur.next;
    26             cur.next = null;
    27             cur = mark;
    28             extra--;
    29         }
    30         
    31         return res;
    32     }
    33     
    34     private int getLength(ListNode head){
    35         int res = 0;
    36         while(head != null){
    37             res++;
    38             head = head.next;
    39         }
    40         
    41         return res;
    42     }
    43 }
  • 相关阅读:
    [bzoj 3048] [Usaco2013 Jan]Cow Lineup
    [bzoj 3192] [JLOI2013]删除物品
    搬迁至新博客的原因
    洛谷 P3317 [SDOI2014]重建(矩阵树定理+数学推导) [bzoj3534]
    [bzoj1002]: [FJOI2007]轮状病毒(矩阵树定理)
    [bzoj1006]: [HNOI2008]神奇的国度(最大势算法)
    高精度板子
    洛谷 P3211 [HNOI2011]XOR和路径(推dp+高斯消元)
    字符串--manacher算法(回文串匹配)
    洛谷 P2633 Count on a tree[bzoj2588](倍增lca+主席树)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/10965555.html
Copyright © 2011-2022 走看看