leetcode Permutation Sequence
摘要:题目描述: Example 1: Example 2: 即:对于一个整数n,共有n!个排列,给出数字k,返回第k个全排列 观察题目可得如下规律: 对于n,其中每个字母 开头的全排列共有n-1!个,如数字3,以 1开头的共有2!个。 因此:m= k / (n-1)! 可以确定出第一个数字, 将该数字加
阅读全文
leetcode 之 word search
摘要:题目描述 79. Word Search •Total Accepted: 85823 •Total Submissions: 357336 •Difficulty: Medium Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from let...
阅读全文
leetcode 之 Palindrome Partitioning
摘要:即, 给定一个字符串,计算所有的回文子串. 思路: 采用深度dfs的思想,对应aab, 网上搜到一张 图: 依次遍历字符串, step [0, len), 判断s[step:len] 是否问回文子串, 如果是, step +1 继续判断,代码如下:
阅读全文
leetcode 之 Excel Sheet Column Number
摘要:意思是将一个字符串转成相应的数字,其转换规则是 excel表格计数那样, A->1 AA->27 稍稍分析得知, 其实是一个26进制的问题, 容易得出: AA = 26^1*nums(A) + 26^0*nums(A) ABC= 26^2*nums(A)+26^1*nums(B)+.... 代码如下
阅读全文
leetcode 之 Count Numbers with Unique Digits
摘要:1. 回溯: 输入n的话, 不考虑10^n, 则总共有n位数. 使用 cur[n] 代表当前的数字. 使用一个flag[10] 代表0~9位数字, 每次放入一个数字,则flag对应的数字置为1, 代码如下: 上述回溯法,在时间上超时了T_T 2.动态规划: 根据提示4, f(k) = 9 * 9 *
阅读全文
leetcode 之Jump Game
摘要:题目描述: 即, 给定一组非负数, 每一个数字代表可以向后走的最大步数, 如A= [2, 3, 1, 1, 4], A[0] = 2, 代表从A[0]可以访问到(A[1] = 3, A[2] =1), 同理,类推。 如果从第一个数开始, 按上述方法, 可以访问到最后一个数, 则该数组返回True,
阅读全文
leetcode 之 Symmetric Tree
摘要:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution
阅读全文
leetcode 之 House Robber
摘要:题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopp
阅读全文
leetcode 之 Minimum Path Sum
摘要:题目描述: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its
阅读全文
leetcode 之 Longest Increasing Subsequence
摘要:题目描述: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The lon
阅读全文
leetcode 之Maximum Product Subarray
摘要:题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-
阅读全文
leetcode 之 Verify Preorder Serialization of a Binary Tree
摘要:题目描述: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null
阅读全文
leetcode 之 Combinations
摘要:描述: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: 运行结果: [[
阅读全文
leetcode 之 Kth Smallest Element in a BST
摘要:题目描述: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤
阅读全文
leetcode 之 Different Ways to Add Parentheses
摘要:题目描述: Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operato
阅读全文
leetcode 之Binary Tree Postorder Traversal
摘要:题目描述: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [3,2,1]. 即 给定一颗二叉树。 使
阅读全文
leetcode 之 Permutation
摘要:描述: 其实我没有看到这个题 给定一个数n, 求其全排列。 如3 则输出 [1, 2, 3][1, 3, 2][2, 1, 3][2, 3, 1][3, 1, 2][3, 2, 1] 使用回溯法求解。 使用n维数组visit 来标记一个数是否已经加入集合。回溯树如下: 深度优先搜索,当搜索深度为n时
阅读全文
leetcode 之 Next Permutation
摘要:题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not
阅读全文
leetcode之Unique Binary Search Trees
摘要:题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 uniq
阅读全文
leetcode 之 Insertion Sort List
摘要:题目描述: 难度: 中等 Sort a linked list using insertion sort. 中文描述: 使用插入排序对一个链表进行排序. 解析: 插入排序就不多说了,主要看插入排序对于单链表来说怎么做. 在单链表中,由于无法拿到链表的前一个元素,所有每次遍历必须从head 开始.找到
阅读全文