`
文章列表
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. class Solution(object): def majorityElement(self, nums): &quo ...
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 class Solution(object): def titleToNum ...
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ numList = {'I':1, 'V':5, 'X ...
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. class Solution(object): def isAnagram(self, s, t): """ ...
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val ...
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] ...
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4after calling your function. # Definition ...
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You must do this in-place without making a copy of ...
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1   # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self ...
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.   # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.l ...
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it without any loop/recursion in O(1) runtime?   class Solu ...
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. Both of you are very clever and have op ...
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3.   class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int &qu ...
Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str L = list( ...
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
Global site tag (gtag.js) - Google Analytics