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