
Leetcode
文章平均质量分 55
AndrewGhost
IT小虫成长记 Andrew_Ghost@yeah.net
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
80. Remove Duplicates from Sorted Array II
#include#includeusing namespace std; int removeDuplicates(vector& nums) { int size=nums.size(); if(size==0) return 0; int temp=nums[0]; int count=0; vector::iterator it = nums.begin();原创 2016-03-15 21:53:13 · 254 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1]Note: Recursive solu原创 2016-03-24 10:41:46 · 269 阅读 · 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.原创 2016-04-06 16:28:32 · 316 阅读 · 0 评论 -
46. Permutations
题目:1~n的全排列思想: 用数组记录操作后的序列,输出结果时只需要输出该数组即可;交换第1个元素与第i(1=当剩余序列中只有一个元素时,得到一种排列结果,输出该结果.#include#include#includeusing namespace std;vector> intVV;void FullArray(vector&nums,int k,int m)原创 2016-03-16 14:00:43 · 342 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
Suppose a sorted array 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 array retur原创 2016-04-06 17:58:21 · 292 阅读 · 0 评论 -
179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2016-03-25 11:58:46 · 303 阅读 · 0 评论 -
240. Search a 2D Matrix II
此题考查杨氏矩阵的搜索,这里采用的是Step-wise线性搜索解法,其余的方法可参考http://blog.youkuaiyun.com/pi9nc/article/details/9082997以下为完整的程序:#include#includeusing namespace std;bool searchMatrix(vector>& matrix, int target) { int ro原创 2016-03-15 10:51:38 · 354 阅读 · 0 评论 -
84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width o原创 2016-03-25 21:03:52 · 334 阅读 · 0 评论 -
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2016-03-26 14:52:24 · 302 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15原创 2016-03-28 00:10:43 · 279 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".又是一道水题,无非从尾加到头,对长的串特殊处理一下。class Solution {public: string addBinary(stri原创 2016-03-28 12:24:32 · 343 阅读 · 0 评论 -
75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2016-03-28 12:45:48 · 264 阅读 · 0 评论 -
Leetcode 202. Happy Number
202. Happy Number QuestionEditorial Solution My SubmissionsTotal Accepted: 84378Total Submissions: 222866Difficulty: EasyWrite an algorithm to determine if a nu原创 2016-08-20 14:29:13 · 293 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example: Given binary tree [3,9,20,null,null,15,7],原创 2016-08-21 16:46:48 · 318 阅读 · 0 评论 -
59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ],原创 2016-08-22 22:32:33 · 344 阅读 · 0 评论 -
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-09-15 23:51:54 · 290 阅读 · 0 评论 -
76. Minimum Window Substring
class Solution {public: string minWindow(string s, string t) { map tmap,temp; string result; int begin,end; begin=end=0; int count=t.length(); int min=s.length()+1; int finalS原创 2016-03-12 23:58:17 · 311 阅读 · 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 the nodes.You should try to do it in原创 2016-03-23 17:46:56 · 257 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
这道题很简单,思路大概是找出最长的那个链表,然后长链表从头开始除掉比短链表长的那一部分,当然不是删掉,移动一下指针就行了,这样下来两个链表就对齐了。之后,一对一的比较,若相同就是交汇处,到遍历完时,没有找到交叉点,那就是空了。/** * Definition for singly-linked list. * struct ListNode { * int val; *原创 2016-03-18 11:36:57 · 307 阅读 · 0 评论 -
337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour原创 2016-03-30 22:10:27 · 391 阅读 · 0 评论 -
66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.水题。class Solutio原创 2016-03-31 17:20:16 · 280 阅读 · 0 评论 -
89. Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2016-03-31 18:00:31 · 318 阅读 · 0 评论 -
97. Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", ret原创 2016-03-20 16:22:34 · 226 阅读 · 0 评论 -
136. Single Number
史上最水的题。。。当然如果用了sort... Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could原创 2016-03-20 17:02:05 · 205 阅读 · 0 评论 -
141. Linked List Cycle
bool hasCycle(ListNode *head) { ListNode *p,*q; p=head; q=head; while(q!=NULL&&q->next!=NULL){ p=p->next; q=q->next->next; if(q-p==0) return true; }原创 2016-03-09 14:32:02 · 216 阅读 · 0 评论 -
61. Rotate List
ListNode* rotateRight(ListNode* head, int k) { if(head==NULL) return NULL; ListNode* p,*q; int i=0,num=0; p=head; q=head; while(p!=NULL) { num++;原创 2016-03-09 15:05:00 · 286 阅读 · 0 评论 -
231. Power of Two
bool isPowerOfTwo(int n) { if(n<=0) return false; while(n>1) { int r=n%2; if(r!=0) return false; else{ n/=2; } } return true; }原创 2016-03-09 15:10:56 · 237 阅读 · 0 评论 -
19. Remove Nth Node From End of List
#include#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };ListNode* createList(int a[],int n){ ListNode* head原创 2016-03-09 15:11:58 · 226 阅读 · 0 评论 -
92. Reverse Linked List II
#include#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };ListNode* createList(int a[],int n){ ListNode* head=NU原创 2016-03-09 15:12:43 · 246 阅读 · 0 评论 -
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant spac原创 2016-03-21 14:42:38 · 246 阅读 · 0 评论 -
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solution {public:原创 2016-09-16 09:22:42 · 292 阅读 · 0 评论