- 博客(22)
- 资源 (1)
- 收藏
- 关注
原创 LintCode:跳跃游戏 II
LintCode:跳跃游戏 IIclass Solution: # @param A, a list of integers # @return an integer def jump(self, A): # write your code here if not A or len(A) == 1: return 0
2016-07-31 19:06:04 534
原创 LintCode:背包问题
LintCode:背包问题首先想到的是建个二维数组,动态规划,超时了。。。class Solution: # @param m: An integer m denotes the size of a backpack # @param A: Given n items with size A[i] # @return: The maximum size def bac
2016-07-31 18:34:52 817
原创 LintCode:将整数A转换为B
LintCode:将整数A转换为BJavaclass Solution { /** *@param a, b: Two integer *return: An integer */ public static int bitSwapRequired(int a, int b) { // write your code here
2016-07-31 16:03:26 269
原创 LintCode:跳跃游戏
LintCode:跳跃游戏贪心:class Solution: # @param A, a list of integers # @return a boolean def canJump(self, A): # write your code here if not A or len(A) == 1: return T
2016-07-31 00:37:18 223
原创 LintCode:堆化
LintCode:堆化参考:白话经典算法系列之七 堆与堆排序class Solution: # @param A: Given an integer array # @return: void def heapify(self, A): # write your code here n = len(A) if n == 0:
2016-07-30 17:04:33 302
原创 LintCode:二叉树的锯齿形层次遍历
LintCode:二叉树的锯齿形层次遍历"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""class Solution: """ @param roo
2016-07-30 11:55:27 688
原创 LintCode:删除排序数组中的重复数字 II
LintCode:删除排序数组中的重复数字 IIclass Solution: """ @param A: a list of integers @return an integer """ def removeDuplicates(self, A): # write your code here n = len(A)
2016-07-30 01:02:31 250
原创 LintCode:删除排序数组中的重复数字
LintCode:删除排序数组中的重复数字class Solution: """ @param A: a list of integers @return an integer """ def removeDuplicates(self, A): # write your code here n = len(A)
2016-07-30 00:59:01 233
原创 LintCode:O(1)时间检测2的幂次
LintCode:O(1)时间检测2的幂次class Solution { /* * @param n: An integer * @return: True or false */ public boolean checkPowerOf2(int n) { // write your code here if(n <=0
2016-07-27 22:20:15 484
原创 LintCode:旋转图像
LintCode:旋转图像public class Solution { /** * @param matrix: A list of lists of integers * @return: Void */ public void rotate(int[][] matrix) { // write your code here
2016-07-27 21:09:34 233
原创 LintCode:两数组的交
LintCode:两数组的交class Solution: # @param {int[]} nums1 an integer array # @param {int[]} nums2 an integer array # @return {int[]} an integer array def intersection(self, nums1, nums2):
2016-07-26 00:27:54 1427
原创 LintCode:全排列
LintCode:全排列参考:全排列算法及实现class Solution: """ @param nums: A list of Integers. @return: A list of permutations. """ def permute(self, nums): # write your code here if n
2016-07-26 00:10:38 616
原创 LintCode:实现 Trie
LintCode:实现 Trie参考地址:百度百科–字典树Java/** * Your Trie object will be instantiated and called as such: * Trie trie = new Trie(); * trie.insert("lintcode"); * trie.search("lint"); will return false * tri
2016-07-25 22:56:11 381
原创 LintCode:整数排序
LintCode:整数排序class Solution: # @param {int[]} A an integer array # @return nothing def sortIntegers2(self, A): # Write your code here i = 0 j = len(A) - 1 se
2016-07-25 01:11:05 567
原创 LintCode:逆波兰表达式求值
LintCode:逆波兰表达式求值被Python里的正负数相除坑了,在Python中,正数除负数是向下取整的,如6/(-132)=-1而在C/C++中是向零取整的,结果是0。class Solution: # @param {string[]} tokens The Reverse Polish Notation # @return {int} the value def e
2016-07-25 00:00:42 738
原创 Lintcode:分糖果
Lintcode:分糖果这里写链接内容class Solution: # @param {int[]} ratings Children's ratings # @return {int} the minimum candies you must give def candy(self, ratings): # Write your code here
2016-07-24 22:49:20 307
原创 LintCode:K组翻转链表
LintCode:K组翻转链表# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param head, a ListNode # @
2016-07-24 11:31:01 782
原创 LintCode:链表划分
LintCode:链表划分"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""class Solution: """ @param head: The f
2016-07-17 01:22:07 428
原创 LintCode:排序列表转换为二分查找树
LintCode:排序列表转换为二分查找树 先遍历得到所有的值,然后递归生成就可以了。"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = nextDefinition of TreeNo
2016-07-16 18:51:20 503
原创 LintCode:生成括号
LintCode:生成括号这里写链接内容class Solution: # @param {int} n n pairs # @return {string[]} All combinations of well-formed parentheses def generateParenthesis(self, n): # Write your code her
2016-07-16 17:09:54 233
原创 LintCode:栅栏染色
LintCode:栅栏染色 lintcode表述有问题,原文是“必须保证任意两个相邻的柱子颜色不同”,应该表述为“至多有两根相邻的柱子具有相同的颜色”,此题使用动态规划,状态转移方程为f(i)=f(i-1)*(k-1) + f(i-2)*(k-1),显然f(0) = k, f(1) = k*k方法一:class Solution: # @param {int} n non-negative
2016-07-16 15:16:38 1860
原创 LintCode:接雨水
LintCode:接雨水做法一:扫两遍。 对某个值A[i]来说,能trapped的最多的water取决于在i之前左边的最高值和在i右边的最高的值,然后取当中较小的一个。 所以可以根据上面的分析先从左到右扫一遍得到数组LeftMostHeight,然后再从右到左计算RightMostHeight,这样只扫了两遍可以得到答案,时间复杂度是O(n),空间复杂度是O(n)。class Solution:
2016-07-14 00:11:28 483
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人