466. 链表节点计数
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: the first node of linked list.
@return: An integer
"""
def countNodes(self, head):
# write your code here
count = 1
if head == None:
return 0
while head.next != None:
head = head.next
count += 1
return count
484. 交换数组两个元素
class Solution:
"""
@param A: An integer array
@param index1: the first index
@param index2: the second index
@return: nothing
"""
def swapIntegers(self, A, index1, index2):
# write your code here
temp = A[index1]
A[index1] = A[index2]
A[index2] = temp
632. 二叉树的最大节点
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param: root: the root of tree
@return: the max node
"""
def maxNode(self, root):
# write your code here
if root == None:
return None
max = root
if root.left != None:
leftMax = self.maxNode(root.left)
if leftMax.val > max.val:
max = leftMax
if root.right != None:
rightMax = self.maxNode(root.right)
if rightMax.val > max.val:
max = rightMax
return max
763. Hex Conversion
class Solution:
"""
@param n: a decimal number
@param k: a Integer represent base-k
@return: a base-k number
"""
def hexConversion(self, n, k):
# write your code here
if n == 0:
return "0"
ret_num = ''
while n > 0:
t = n % k
c = ''
if t <= 9: #0 ~ 48
c = ord('0') + t
else: #A ~ 65
c = ord('A') + (t - 10)
print(c)
ret_num = chr(c) + ret_num
n = int(n / k)