
leetcode
文章平均质量分 69
littleorange6
这个作者很懒,什么都没留下…
展开
-
关于divide and conquer的两道Leetcode
贴上两道感觉很像的leetcode题目,第一道并不是自己独立写出来的,但是第二道因为对第一道的总结而一遍AC,开心~!返回一个集合时,要比较注意arraylist们都是在哪里声明的,在哪里添加的。Unique Binary Search Trees IIQuestion:Given n, generate all structurally unique BST's (b原创 2015-09-05 11:20:46 · 476 阅读 · 0 评论 -
leetcode中的二分法总结
[leetcode 69] Sqrt(x)题目计算sqrt(x)的开方,结果为向下取整。思路思路1:用牛顿迭代法 牛顿迭代法是用来解f(x)=0f(x)=0的方程的解。牛顿的迭代法的原理图如下,摘自wiki百科: 可以看出,这条切线满足的方程是: y=f′(xn)(x−xn)+f(xn)y=f'(x_{n})\,(x-x_{n})+f(x_{n}),这个方程和xx轴的交点写成迭代的形式xn原创 2017-07-13 11:09:22 · 543 阅读 · 0 评论 -
leetcode中的DP题目总结
[leetcode 413]Arithmetic Slicesdp解释dp[i]dp[i] 代表以 ii 结尾的arithmetic slice的个数。因此有下列关系 dp[i]=dp[i−1]+1,ifA[i]−A[i−1]=A[i−1]−A[i−2]dp[i] = dp[i-1] + 1, if A[i] - A[i-1] = A[i-1] - A[i-2] dp[i]=0,ifA[i]−A原创 2017-04-24 13:30:24 · 3356 阅读 · 1 评论 -
127.Word Ladder
题目:从开始单词到结束单词,借助字典是否可达,如果可达路径长度是多少?一个例子:beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log"]As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog"原创 2016-12-22 20:55:00 · 344 阅读 · 0 评论 -
理解回溯法
46.permutation题目:返回所有的不同全排列思路:刚开始的思路是普通的递归生成全排列。比如:[1,2,3,4]的全排列就是1+[2,3,4]的全排列,2+[1,3,4]的全排列3+[1,2,4]的全排列4+[1,2,3]的全排列我们用helper(int[] nums, int pos)来返回从pos开始的所有全排列。把后面的数字一个一个放在pos上(只放不同的),然原创 2016-09-26 19:50:34 · 331 阅读 · 0 评论 -
9. Palindrome Numbers
题目:判断一个数字是不是回文数思路:看这个数是不是等于这个数倒过来,这种方法会出现,一个数正过来是不溢出的,倒过来就溢出了我的方法是:用一个base,把数字一点一点拨出来,123321-》2332-》33-》0,这样没有溢出的问题,但是很慢,写起来也不方便推荐:把数字倒出来一半,123321 -》 123和123 或者12321-》12 123然后比较这两半的大小!也没有溢原创 2016-09-20 20:33:36 · 263 阅读 · 0 评论 -
395. Longest Substring with At Least K Repeating Characters
题目给定一个字符串,给定一个数字k,找这个字符串里的最大子串的长度,子串满足的条件是:里面的每一个字母都出现了至少k次。例如:Input:s = "ababbc", k = 2Output:5The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.原创 2016-09-06 21:06:13 · 803 阅读 · 0 评论 -
394. Decode String
题目:解码字符串,解码规则是k[str]被解码为k个str连起来的字符串思路原创 2016-09-06 20:21:19 · 343 阅读 · 0 评论 -
bst
/** * Definition for singly-linked list*/class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}/** * Definition for a binary tree node.*/class TreeNode {原创 2016-09-15 16:02:14 · 235 阅读 · 0 评论 -
367. Valid Perfect Square
题目判断一个数是不是一个完全平方数,不可以用内置函数例如:Input: 16Returns: True思路:BinarySearch一些小trick:mid = left + (right - left)/2 来替换 mid = (left + right)/2 可能出现的溢出判断mid*mid == num 时同样可能发生溢出,换成(num/mid原创 2016-09-10 10:35:23 · 253 阅读 · 0 评论 -
331. 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 node, we re原创 2016-02-02 22:20:30 · 1397 阅读 · 0 评论 -
141+142. Linked List Cycle I+II
问题:返回一个单链表中环的起点,如果没有环返回nullGiven a linked list, return the node where the cycle begins. If there is no cycle, return null.思路:用两个指针,一个一次走两步,一个一次走一步,如果这个链表有环,这两个指针一定会相遇,而且相遇时走的步数就是环的长度。然后我原创 2016-01-31 22:45:02 · 284 阅读 · 0 评论 -
303+304. Range Sum Query && Range Sum Query 2D
题目:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined原创 2016-01-31 21:16:21 · 365 阅读 · 0 评论 -
330. Patching Array
题目:给一个数组,给一个范围n,最少放进去多少个数字,可以让数组中的数字的子集的和覆盖满整个范围Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive ca原创 2016-01-28 23:26:19 · 2459 阅读 · 4 评论 -
297. Serialize and Deserialize Binary Tree
问题:用自己的方法把二叉树转化为字符串,在用自己的方法把这个字符串转为二叉树Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or tran原创 2016-01-28 20:40:53 · 291 阅读 · 0 评论 -
329. Longest Increasing Path in a Matrix
问题:找到一个二维数组中最长的升序列的长度,序列只可以上下左右走。Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. Yo原创 2016-01-27 15:25:59 · 562 阅读 · 0 评论 -
Ugly Number的三道题
问题原创 2016-01-26 22:00:18 · 435 阅读 · 0 评论 -
328. Odd Even Linked List
问题:单链表位于奇数位置的统一放在前面,位于偶数位置的统一放在后面Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in原创 2016-01-26 15:08:36 · 408 阅读 · 0 评论 -
日常总结算法竞赛常用函数
生成子集-非递归 vector<vector<int>> subsets(vector<int> &nums) { // write your code here sort(nums.begin(), nums.end()); int size = nums.size(); int N = 1 ...原创 2018-12-29 23:17:17 · 685 阅读 · 0 评论