题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
# -*- coding:utf-8 -*-
# class RandomListNode:
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None
class Solution:
# 返回 RandomListNode
def Clone(self, pHead):
# write code here
if not pHead:
return pHead
head = RandomListNode(pHead.label)
head.next = pHead.next
head.random = pHead.random
head.next = self.Clone(pHead.next)
return head
题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
中序遍历:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Convert(self, pRootOfTree):
# write code here
root = pRootOfTree
if not root:
return None
tmp = []
isfirst = True
while tmp or root:
if root:
tmp.append(root)
root = root.left
else:
root = tmp.pop()
if isfirst:
Root = root
isfirst = False
pre = root
else:
pre.right = root
root.left = pre
pre = root
root = root.right
return Root
题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
# -*- coding:utf-8 -*-
class Solution:
def Permutation(self, ss):
# write code here
res = []
if not ss:
return res
if len(ss)==1:
return [ss]
for i in range(len(ss)):
for tmp in self.Permutation(ss[:i]+ss[i+1:]):
res.append(ss[i] + tmp)
return sorted(set(res))