LeetCode086 Partition List

来源:互联网 发布:mac不能玩游戏的原因 编辑:程序博客网 时间:2024/06/07 13:54

详细见:leetcode.com/problems/maximal-rectangle


Java Solution: github

package leetcode;import tools.ListNode辅助.*;/* * Given a linked list and a value x, partition it such that all nodes less *  than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2->4->3->5. */public class P086_PartitionList {public static void main(String[] args) {ListNode head = tools.ListNode辅助.A_一维生成器(new int[] {1, 4, 3, 2, 5, 2});//head = tools.ListNode辅助.A_一维生成器(new int[] {6, 4, 4, 2, 5, 2});//head = null;//head = tools.ListNode辅助.A_一维生成器(new int[] {6});//head = tools.ListNode辅助.A_一维生成器(new int[] {1, 1, 1, 1, 1});//head = tools.ListNode辅助.A_一维生成器(new int[] {1, 2, 3});//head = tools.ListNode辅助.A_一维生成器(new int[] {3, 1, 2});ListNode ans = new Solution().partition(head, 3);tools.ListNode辅助.B_打印链表(ans);}/* * 昨天一天没有弄清楚,其实一个循环完成一个功能才是上上策。 * AC * 1 ms */static class Solution {    public ListNode partition(ListNode head, int x) {    ListNode first_big = head, first_small = head;    int first_big_index = 0, first_small_index = 0;    while (first_big != null) {    if (first_big.val >= x) {    break;    } else {    first_big = first_big.next;    first_big_index ++;    }    }    while (first_small != null) {    if (first_small.val < x) {    break;    } else {    first_small = first_small.next;    first_small_index ++;    }    }    if (first_big == null || first_small == null) {    return head;    }    ListNode ans = null, last_small = null, cur = null, cur_pre = null;    if (first_big_index > first_small_index) {    ans = first_small;    last_small = first_small;    while (last_small.next.val < x) {    last_small = last_small.next;    }    cur = first_big.next;    cur_pre = first_big;    while (cur != null) {    if (cur.val < x) {    break;    } else {    cur_pre = cur;    cur = cur.next;    }    }    if (cur == null) {    return ans;    }    } else if (first_big_index < first_small_index) {    ans = first_small;    last_small = first_small;    ListNode temp = last_small.next;    while (temp != null) {    if (temp.val >= x) {    break;    } else {    last_small = temp;    temp = temp.next;    }    }    ListNode last_big = first_big;    while(last_big.next != first_small) {     last_big = last_big.next;    }    last_big.next = last_small.next;    last_small.next = first_big;    cur = last_big.next;    cur_pre = last_big;    } else {    return head;    }    while (cur != null) {    if (cur.val >= x) {    cur_pre = cur;    cur = cur.next;    } else {    ListNode save_cur = cur.next;    cur.next = first_big;    last_small.next = cur;    last_small = cur;    cur_pre.next = save_cur;    cur = save_cur;    }    }    return ans;    }}}


C Solution: github

/*    url: leetcode.com/problems/partition-list    AC 3ms 19.74%*/#include <stdio.h>#include <stdlib.h>typedef struct ListNode * pln;typedef struct ListNode sln;struct ListNode {    int val;    struct ListNode *next;};void solve(pln* sh, pln* st, pln* t) {    if (*sh == NULL) {        *sh = *st = *t;    } else {        (*st)->next = *t;        *st = *t;    }}pln partition(pln head, int x) {    pln sh = NULL, bh = NULL;    pln st = NULL, bt = NULL;    pln t = head;    while (t != NULL) {        if (t->val < x) {            solve(&sh, &st, &t);        } else {            solve(&bh, &bt, &t);        }        t = t->next;    }    if (sh == NULL) return bh;    if (bh == NULL) return sh;    st->next = bh;    bt->next = NULL;    return sh;}pln ln_construct(int* a, int n) {    pln* l = (pln*) malloc(sizeof(pln) * n);    int i = 0;    pln ans = NULL;    for (i = n-1; i > -1; i --) {        l[i] = (pln) malloc(sizeof(sln));        l[i]->next = i == n-1 ? NULL: l[i+1];        l[i]->val = a[i];    }    if (n-1 > -1)        l[n-1]->next = NULL;    ans = l[0];    free(l);    return ans;}void ln_print(pln l) {    while (l != NULL) {        printf("%d ", l->val);        l = l->next;    }    printf("\r\n");}void ln_free(pln l) {    pln i = l, j = NULL;    while(i != NULL) {        j = i->next;        free(i);        i = j;    }}int main() {    int n[] ={1,4,3,2,5,2};    pln l = ln_construct(n, 6);    pln a = partition(l, 3);    ln_print(a);    ln_free(l);}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/maximal-rectangle    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月21日    @details:    Solution: 52ms 54.78%'''class ListNode(object):    def __init__(self, x):        self.val = x        self.next = None    def __str__(self, *args, **kwargs):        return str(self.val)def construct(l):    ln = 0 if l == None else len(l)    if ln == 0: return None    lns = [None] * ln    for i in range(ln-1, -1, -1):        lns[i] = ListNode(l[i])        if i != ln-1:             lns[i].next = lns[i+1]    return lns[0]def print_ListNode(l):    print("================")    while l != None:        print(l.val)        l = l.next    print("================")class Solution(object):    def add(self, s, t, i, j):        if s[i] == None:            s[i] = s[j] = t        else:            s[j].next = t            s[j] = t                def partition(self, h, x):        """        :type h: ListNode        :type x: int        :rtype: ListNode        """        s, t = [None] * 4, h        while t != None:            g = t.next            t.next = None            if t.val < x:                self.add(s, t, 0, 1)            else:                self.add(s, t, 2, 3)            t = g        if s[0] == None: return h        s[1].next = s[2]        return s[0]if __name__ == "__main__":    m = [            [1, 4, 3, 2, 5, 2],            [1],            [4, 3, 5],            [5, 2],                    ]    for n in m:        h = construct(n)        print_ListNode(Solution().partition(h, 3))


0 0
原创粉丝点击