File tree Expand file tree Collapse file tree 5 files changed +197
-0
lines changed
Expand file tree Collapse file tree 5 files changed +197
-0
lines changed Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=160 lang=python
3+ #
4+ # [160] 相交链表
5+ #
6+ # Definition for singly-linked list.
7+ # class ListNode(object):
8+ # def __init__(self, x):
9+ # self.val = x
10+ # self.next = None
11+
12+ class Solution (object ):
13+ def getIntersectionNode (self , headA , headB ):
14+ """
15+ :type head1, head1: ListNode
16+ :rtype: ListNode
17+ """
18+
19+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=24 lang=python
3+ #
4+ # [24] 两两交换链表中的节点
5+ #
6+ # https://leetcode-cn.com/problems/swap-nodes-in-pairs/description/
7+ #
8+ # algorithms
9+ # Medium (62.13%)
10+ # Likes: 283
11+ # Dislikes: 0
12+ # Total Accepted: 39.4K
13+ # Total Submissions: 63.5K
14+ # Testcase Example: '[1,2,3,4]'
15+ #
16+ # 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
17+ #
18+ # 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
19+ #
20+ #
21+ #
22+ # 示例:
23+ #
24+ # 给定 1->2->3->4, 你应该返回 2->1->4->3.
25+ #
26+ #
27+ #
28+ # Definition for singly-linked list.
29+ # class ListNode(object):
30+ # def __init__(self, x):
31+ # self.val = x
32+ # self.next = None
33+
34+ class Solution (object ):
35+ def swapPairs (self , head ):
36+ """
37+ :type head: ListNode
38+ :rtype: ListNode
39+ """
40+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=46 lang=python
3+ #
4+ # [46] 全排列
5+ #
6+ # https://leetcode-cn.com/problems/permutations/description/
7+ #
8+ # algorithms
9+ # Medium (71.57%)
10+ # Likes: 376
11+ # Dislikes: 0
12+ # Total Accepted: 46.1K
13+ # Total Submissions: 64.2K
14+ # Testcase Example: '[1,2,3]'
15+ #
16+ # 给定一个没有重复数字的序列,返回其所有可能的全排列。
17+ #
18+ # 示例:
19+ #
20+ # 输入: [1,2,3]
21+ # 输出:
22+ # [
23+ # [1,2,3],
24+ # [1,3,2],
25+ # [2,1,3],
26+ # [2,3,1],
27+ # [3,1,2],
28+ # [3,2,1]
29+ # ]
30+ #
31+ #
32+ class Solution (object ):
33+ def permute (self , nums ):
34+ """
35+ :type nums: List[int]
36+ :rtype: List[List[int]]
37+ """
38+
39+ def backtrack (nums ,path ):
40+ if not nums :
41+ res .append (path )
42+ for i in xrange (len (nums )):
43+ backtrack (nums [:i ]+ nums [i + 1 :],path + [nums [i ]])
44+ res = []
45+ backtrack (nums ,[])
46+ return res
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=47 lang=python
3+ #
4+ # [47] 全排列 II
5+ #
6+ # https://leetcode-cn.com/problems/permutations-ii/description/
7+ #
8+ # algorithms
9+ # Medium (53.97%)
10+ # Likes: 159
11+ # Dislikes: 0
12+ # Total Accepted: 22.6K
13+ # Total Submissions: 41.6K
14+ # Testcase Example: '[1,1,2]'
15+ #
16+ # 给定一个可包含重复数字的序列,返回所有不重复的全排列。
17+ #
18+ # 示例:
19+ #
20+ # 输入: [1,1,2]
21+ # 输出:
22+ # [
23+ # [1,1,2],
24+ # [1,2,1],
25+ # [2,1,1]
26+ # ]
27+ #
28+ #
29+ class Solution (object ):
30+ def permuteUnique (self , nums ):
31+ """
32+ :type nums: List[int]
33+ :rtype: List[List[int]]
34+ """
35+ def backtrack (nums ,path ):
36+ if not nums :
37+ res .append (path )
38+ return
39+ for i in xrange (len (nums )):
40+ if i >= 1 and nums [i ]== nums [i - 1 ]:
41+ continue
42+ backtrack (nums [:i ]+ nums [i + 1 :],path + [nums [i ]])
43+ res = []
44+ nums .sort ()
45+ backtrack (nums ,[])
46+ return res
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode.cn id=94 lang=python
3+ #
4+ # [94] 二叉树的中序遍历
5+ #
6+ # https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/
7+ #
8+ # algorithms
9+ # Medium (67.86%)
10+ # Likes: 277
11+ # Dislikes: 0
12+ # Total Accepted: 59.4K
13+ # Total Submissions: 87.5K
14+ # Testcase Example: '[1,null,2,3]'
15+ #
16+ # 给定一个二叉树,返回它的中序 遍历。
17+ #
18+ # 示例:
19+ #
20+ # 输入: [1,null,2,3]
21+ # 1
22+ # \
23+ # 2
24+ # /
25+ # 3
26+ #
27+ # 输出: [1,3,2]
28+ #
29+ # 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
30+ #
31+ #
32+ # Definition for a binary tree node.
33+ # class TreeNode(object):
34+ # def __init__(self, x):
35+ # self.val = x
36+ # self.left = None
37+ # self.right = None
38+
39+ class Solution (object ):
40+ def inorderTraversal (self , root ):
41+ """
42+ :type root: TreeNode
43+ :rtype: List[int]
44+ """
45+
46+
You can’t perform that action at this time.
0 commit comments