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

    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].

    题解:

      其实没什么难度,就是确定每一组的元素个数。

    Solution 1

     1 class Solution {
     2 public:
     3     vector<ListNode*> splitListToParts(ListNode* root, int k) {
     4         vector<ListNode*> res;
     5         int len = getLength(root);
     6         vector<int> lens(k, len / k);
     7         int remain = len - (len / k * k);
     8         while (remain > 0) {
     9             for (int i = 0; i < remain; ++i) {
    10                 ++lens[i];
    11             }
    12             remain -= k;
    13         }
    14         ListNode* cur = root;
    15         for (int i = 0; i < lens.size(); ++i) {
    16             if (lens[i] == 0) {
    17                 res.push_back({nullptr});
    18                 continue;
    19             }
    20             for (int j = 0; j < lens[i]; ++j) {
    21                 if (j == 0)
    22                     res.push_back(cur);
    23                 else {
    24                     cur = cur->next;
    25                 }
    26             }
    27             if (cur) {
    28                 ListNode* tmp = cur;
    29                 cur = cur->next;
    30                 tmp->next = nullptr;
    31             }
    32         }
    33         return res;
    34     }
    35     
    36     int getLength(ListNode* head) {
    37         int len = 0;
    38         while (head) {
    39             ++len;
    40             head = head->next;
    41         }
    42         return len;
    43     }
    44 };

    Solution 2

      精简优化版,无需开辟数组。转自 Grandyang

     1 class Solution {
     2 public:
     3     vector<ListNode*> splitListToParts(ListNode* root, int k) {
     4         vector<ListNode*> res(k);
     5         int len = 0;
     6         for (ListNode *t = root; t; t = t->next) 
     7             ++len;
     8         // avg为每组均分元素个数
     9         // ext为均分后剩余元素个数,剩余元素从第一组到最后一组依次添加
    10         int avg = len / k, ext = len % k; 
    11         for (int i = 0; i < k && root; ++i) {
    12             res[i] = root;
    13             // 每组元素个数为 均分个数 + 能否添加剩余元素
    14             // 能否添加剩余元素通过组序号和剩余元素数比较判断
    15             for (int j = 1; j < avg + (i < ext); ++j) {
    16                 root = root->next;
    17             }
    18             ListNode *t = root->next;
    19             root->next = NULL;
    20             root = t;
    21         }
    22         return res;
    23     }
    24 };
  • 相关阅读:
    好的软件架构设计(转)
    利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)
    Lucene.Net+盘古分词器(详细介绍)(转)
    如何让 height:100%; 起作用---父级元素必须设定高度
    前端问题汇总
    JS基础_强制类型转换-Number
    CSS实现垂直居中的常用方法
    vue项目上线步骤
    一篇文章全面了解运维监控知识体系
    vue+axios 前端实现登录拦截(路由拦截、http拦截)
  • 原文地址:https://www.cnblogs.com/Atanisi/p/8846377.html
Copyright © 2011-2022 走看看