- 博客(87)
- 收藏
- 关注
原创 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
222
原创 把一个链表的奇数和偶数分开
给你一个单链表,修改此单链表,使得前面是偶数,后面是奇数。偶数间的相对顺序不变,奇数间的相对顺序不变。返回修改后的单链表的头节点。例如: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
3750
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
147
原创 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
150
原创 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
120
原创 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
131
原创 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
91
原创 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
106
原创 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
107
原创 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
96
原创 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
171
原创 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
95
原创 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
101
原创 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
115
原创 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
110
原创 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
100
原创 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
95
原创 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
111
原创 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
85
原创 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
121
转载 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
89
原创 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
87
转载 145. Binary Tree Postorder Traversal
postorder遍历二叉树,参考http://blog.csdn.net/feliciafay/article/details/18341703/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; ...
2018-02-19 04:52:50
92
原创 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
100
原创 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
3173
原创 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
97
原创 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
91
原创 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
332
原创 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
102
原创 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
75
原创 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
169
原创 723. Candy Crush
This question is about implementing a basic elimination algorithm for Candy Crush.Given a 2D integer array board representing the grid of candy, different positive integers board[i][j]represent
2018-01-28 10:40:08
210
原创 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you
2018-01-28 10:30:19
87
原创 553. Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.However, you can add any number of parenthesis at any position to change
2018-01-26 09:30:13
69
原创 78. Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[
2018-01-26 02:24:37
205
原创 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3
2018-01-25 10:11:48
74
原创 442. Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it without ex
2018-01-25 10:02:10
76
原创 370. Range Addition
Assume you have an array of length n initialized with all 0's and are given k update operations.Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each elemen
2018-01-25 09:39:46
103
原创 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5
2018-01-25 09:21:01
95
原创 419. Battleships in a Board
Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:You receive a vali
2018-01-25 09:01:00
69
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人