摘要:
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space.class Solution {public: int firstMissingPositive(int A[], int n) { // Start typing your C/C++ solutio 阅读全文
posted @ 2013-09-16 21:02
懒猫欣
阅读(130)
评论(0)
推荐(0)
摘要:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Note:Bonus points if you could solve it both recursively and itera... 阅读全文
posted @ 2013-09-16 18:40
懒猫欣
阅读(223)
评论(0)
推荐(0)
摘要:
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ... 阅读全文
posted @ 2013-09-16 18:15
懒猫欣
阅读(174)
评论(0)
推荐(0)
摘要:
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./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x... 阅读全文
posted @ 2013-09-16 18:12
懒猫欣
阅读(171)
评论(0)
推荐(0)
摘要:
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. Howe 阅读全文
posted @ 2013-09-16 14:54
懒猫欣
阅读(177)
评论(0)
推荐(0)
摘要:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line: "PAHNAPLSIIGYIR"Write the code that will take a string 阅读全文
posted @ 2013-09-16 14:27
懒猫欣
阅读(173)
评论(0)
推荐(0)
摘要:
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).class Solution {public: int FindKthElm(int A[],int bA,int eA,int B[],int bB,int eB,int k){ if(bA>eA){ return B[bB+k-... 阅读全文
posted @ 2013-09-16 12:11
懒猫欣
阅读(183)
评论(0)
推荐(0)
摘要:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: ... 阅读全文
posted @ 2013-09-16 02:27
懒猫欣
阅读(137)
评论(0)
推荐(0)
摘要:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tree * struc... 阅读全文
posted @ 2013-09-16 02:24
懒猫欣
阅读(160)
评论(0)
推荐(0)
浙公网安备 33010602011771号