python_lintcode_96. The linked list is divided into _389. To judge whether Sudoku is legal

Source: Internet
Author: User
96. Link List Division Topic

Given a single linked list and a value x, dividing a list makes all nodes less than x equal to the node greater than X.

You should keep the original relative order of the two parts of the linked table node.

Examples

The given list 1->4->3->2->5->2->null, and x=3

returns 1->2->2->4->3->5->null
train of ThoughtSimple idea: is to create a new two list of linked lists to store less than X and greater than X, and then merge the two list. Code
' "'
Definition of ListNode
class ListNode (object):

    def __init__ (self, Val, next=none):
        self.val = val<  C4/>self.next = Next
"" "


class Solution:" "
    @param: head:the The linked list
    @param: x: An Integer
    @return: A listnode
    "" "
    def partition (self, head, x):
        # Write your code here
        if None:return None
        #新建两个链表存放小于x和大于x
        l1 = head
        l2= listnode (0)
        y = L2
        L3 = ListNode (0)
        z = L3 While
        L1:
            if l1.val<x:
                y.next = L1
                y = y.next
            else:
                z.next = L1
                z = z.next
            l 1 = l1.next
        #将大于x的尾部添加None
        z.next = None
        #将两条链表进行合并
        y.next = l3.next
        #返回该链表
        return L2.next
389. Judge whether Sudoku is legal Topic

http://www.lintcode.com/zh-cn/problem/valid-sudoku/

Please determine if a Sudoku is valid.

The Sudoku may only be populated with partial numbers, in which the missing digits are used. Said.

Attention matters

A valid Sudoku (only partially populated) is not necessarily solvable. We just need to make the padding space valid. thinking at first thought is to determine whether can be solvable Sudoku, see the following points of attention. Only need to judge the ranks and the small square can be legitimate (no duplicate number of cases) code

Class Solution: "" "@param: Board:the Board @return: Whether the Sudoku is valid" "Def Isvalidsudo KU (self, Board): # Write your code here RES = set () res2 =set () #1判断行列有效 to I in
                    Range (9): for J in Range (9): #行 if board[i][j]!= '. ' and Board[i][j] in res: Return False Else:res.add (board[i][j]) #列 I F board[j][i]!= '. ' and Board[j][i] in Res2:return False else:res  2.add (Board[j][i]) res2.clear () res.clear () #2判断每一块有效 #对于九大块 for I
                    Range (0,9,3): for J in Range (0,9,3): #对于九大块内的一小块 to A in range (I,I+3): For b in Range (J,J+3): #判断是否有效 if board[a][b]!= '. ' and boar
        D[A][B] in Res:                    Return False Else:res.add (Board[a][b])
 #小块判断完即可清空res res.clear () return True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.