随笔分类 -  leetcode

学习他人or自己完成
摘要:题意: 给定非排序数组,找出其排序后相邻元素的最大差值。 线性时间空间、元素数少于2时返回0、元素值非负且int范围内。 思路: 排序最快nlogn不符合要求; 参考网上,学习了桶排序的方法; 桶排序:按值分段处理; 设定桶大小和桶个数; 因为ans>=(MAX-MIN)/(len-1); 桶大小: 阅读全文
posted @ 2016-10-20 21:51 西小贝 阅读(119) 评论(0) 推荐(0)
摘要:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you nee 阅读全文
posted @ 2016-08-17 21:09 西小贝 阅读(450) 评论(0) 推荐(0)
摘要:There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every 阅读全文
posted @ 2016-08-16 21:26 西小贝 阅读(164) 评论(0) 推荐(0)
摘要:Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assum 阅读全文
posted @ 2016-08-16 20:51 西小贝 阅读(137) 评论(0) 推荐(0)
摘要:For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible roote 阅读全文
posted @ 2016-08-14 14:52 西小贝 阅读(185) 评论(0) 推荐(0)
摘要:121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitte 阅读全文
posted @ 2016-08-12 15:36 西小贝 阅读(145) 评论(0) 推荐(0)
摘要:Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2 阅读全文
posted @ 2016-08-11 10:03 西小贝 阅读(135) 评论(0) 推荐(0)
摘要:Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, 阅读全文
posted @ 2016-08-09 19:59 西小贝 阅读(132) 评论(0) 推荐(0)
摘要:Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 阅读全文
posted @ 2016-08-09 10:54 西小贝 阅读(196) 评论(0) 推荐(0)
摘要:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements 阅读全文
posted @ 2016-08-08 14:58 西小贝 阅读(112) 评论(0) 推荐(0)
摘要:Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. Th 阅读全文
posted @ 2016-08-08 10:34 西小贝 阅读(115) 评论(0) 推荐(0)
摘要:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowes 阅读全文
posted @ 2016-08-07 16:53 西小贝 阅读(125) 评论(0) 推荐(0)
摘要:题意:给出分子和分母,用字符串表示结果,循环的部分用“()”括起来。 思路: 模拟笔算, 最初根据1/99的情况,设想用path记录余数相同时的间隔步长,但只能用于处理(00..0n)的情况,其中n为[1,9]; 后来采取网上看到用map记录<余数,出现位置>的想法。 通过判断map中key是否重复 阅读全文
posted @ 2016-07-24 20:07 西小贝 阅读(221) 评论(0) 推荐(0)
摘要:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For 阅读全文
posted @ 2016-05-09 21:41 西小贝 阅读(185) 评论(0) 推荐(0)
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. note: 开始时,两指针都指向h 阅读全文
posted @ 2016-05-09 19:01 西小贝 阅读(160) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only n... 阅读全文
posted @ 2016-01-07 23:28 西小贝 阅读(127) 评论(0) 推荐(0)
摘要:Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note... 阅读全文
posted @ 2016-01-06 16:10 西小贝 阅读(144) 评论(0) 推荐(0)
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No... 阅读全文
posted @ 2016-01-06 16:09 西小贝 阅读(188) 评论(0) 推荐(0)
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.2... 阅读全文
posted @ 2016-01-06 16:09 西小贝 阅读(149) 评论(0) 推荐(0)
摘要:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message conta... 阅读全文
posted @ 2016-01-06 16:07 西小贝 阅读(201) 评论(0) 推荐(0)