
leetcode
文章平均质量分 68
Pincushion
这个作者很懒,什么都没留下…
展开
-
Hash Table
class HashItem{ int key, val;public: HashItem(int k, int k): key(k), val(v){} const int& getKey(){ return key; } const int& getVal(){ return val; }};...原创 2018-03-26 17:05:47 · 253 阅读 · 0 评论 -
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Input: k = 3, n ...原创 2018-02-20 01:22:46 · 120 阅读 · 0 评论 -
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.N...原创 2018-02-20 01:19:44 · 137 阅读 · 0 评论 -
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C...原创 2018-02-20 01:14:32 · 108 阅读 · 0 评论 -
387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: You may assume...原创 2018-02-19 08:26:28 · 143 阅读 · 0 评论 -
451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters.Example 1:Input:"tree"Output:"eert"Explanation:'e' appears twice while 'r' and 't' both appear once.So 'e' must...转载 2018-02-19 05:53:35 · 112 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
preorder 遍历二叉树/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}...原创 2018-02-19 05:00:26 · 105 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
postorder遍历二叉树,参考http://blog.youkuaiyun.com/feliciafay/article/details/18341703/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...转载 2018-02-19 04:52:50 · 115 阅读 · 0 评论 -
541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them...原创 2018-02-19 03:39:29 · 115 阅读 · 0 评论 -
htons
判断是否为小端表示,如果是就转为大端,如果不是就直接返回。判断大小端, 存储int i为1,如果取到最后一个字节的地址,输出结果还是1,那么就是小端表示,不然就是大端表示: int i = 1; char *p = (char *)&i; if(*p == 1) printf("Little Endian"); el...原创 2018-02-19 03:04:25 · 3235 阅读 · 0 评论 -
8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases....原创 2018-02-18 11:45:38 · 113 阅读 · 0 评论 -
285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.Note: If the given node has no in-order successor in the tree, return null.inorder 遍历 BST 用stack模拟,先把左节...原创 2018-02-18 10:37:03 · 109 阅读 · 0 评论 -
Is BST?
判断一个树是不是BST因为BST是对于每一个点,左子树的所有节点都小于当前点,右子树的所有节点都大于当前点。所以其实每个子树代表一段值,可以用当前点是否超出了[min, max]范围判断是否是二叉树。 bool isBST(TreeNode* root, int min, int max) { if (root == NULL) return 1; if (root...原创 2018-02-18 10:09:56 · 373 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5比较简单的题...原创 2018-02-20 01:26:16 · 126 阅读 · 0 评论 -
237. Delete Node in a Linked List
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 ...原创 2018-02-20 01:27:49 · 126 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
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.For example:Given binary tree [3,9,20,null...原创 2018-02-20 01:30:26 · 142 阅读 · 0 评论 -
把一个链表的奇数和偶数分开
给你一个单链表,修改此单链表,使得前面是偶数,后面是奇数。偶数间的相对顺序不变,奇数间的相对顺序不变。返回修改后的单链表的头节点。例如:Input: 17->15->8->12->10->5->4->1->7->6->NULLOutput: 8->12->10->4->6->17->15原创 2018-03-23 23:12:13 · 3825 阅读 · 2 评论 -
82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2-&...原创 2018-03-23 22:44:42 · 174 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.和26题很像,只...原创 2018-03-23 22:41:44 · 185 阅读 · 0 评论 -
81. Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose an array sorted in ascending order is rotated at some pivot...原创 2018-03-23 11:16:20 · 143 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the a...原创 2018-03-23 11:11:13 · 156 阅读 · 0 评论 -
80. Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elements ...原创 2018-03-23 11:05:45 · 119 阅读 · 0 评论 -
215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note: Y...原创 2018-02-20 10:02:07 · 133 阅读 · 0 评论 -
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the ...原创 2018-02-20 06:13:00 · 132 阅读 · 0 评论 -
50. Pow(x, n)
Implement pow(x, n).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100注意corner case很多,1 如果x == 1.0 或者 n == 0 应该返回12如果x= -1.0,n为偶返回1.0, n为奇数返回-1.0,必须先判断,不然n=INT...原创 2018-02-20 05:49:38 · 122 阅读 · 0 评论 -
733. Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate (sr, sc) representing the starting pixel (row and column...原创 2018-02-20 02:34:43 · 195 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between tw...原创 2018-02-20 02:05:09 · 123 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
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 lowest common ancestor is defined between two nodes v an...原创 2018-02-20 01:39:25 · 125 阅读 · 0 评论 -
298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path.The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connection...原创 2018-02-18 05:54:04 · 124 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the ...原创 2018-02-18 02:04:43 · 95 阅读 · 0 评论 -
369. Plus One Linked List
Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself原创 2018-01-28 11:08:53 · 194 阅读 · 0 评论 -
220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute diffe原创 2017-12-26 16:33:39 · 139 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b",原创 2018-01-11 10:35:22 · 92 阅读 · 0 评论 -
415. Add Strings
这道题的类型和2.add two numbers 是一样的,只不过2的里面是2个list相加,这个是两个string相加。Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of bot原创 2017-12-26 11:22:41 · 100 阅读 · 0 评论 -
190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 001110010原创 2017-12-26 10:54:10 · 89 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of t原创 2017-12-26 10:14:26 · 103 阅读 · 0 评论 -
269. Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted le原创 2017-12-30 22:09:14 · 353 阅读 · 0 评论 -
202. Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2017-12-22 22:52:38 · 112 阅读 · 0 评论 -
2. Add Two Numbers
不是很难的一道题,就是list操作,但是细节总是写不对。1 循环时是先while(l1 != NULL && l2 != NULL)然后在加剩下的list,但是发现这个时候也要考虑进位,所以把长list的后半段留下来并不能直接加在返回list的后面,所以还是用while(l1 != NULL || l2 != NULL)把两个list都遍历完,再处理剩下的情况,2 但是使用sum来记录总和原创 2017-12-22 22:22:58 · 88 阅读 · 0 评论 -
219. Contains Duplicate II
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 absolute difference between i and j is a原创 2017-12-22 20:57:02 · 118 阅读 · 0 评论