- 博客(39)
- 收藏
- 关注
原创 建立二叉树
建立二叉树如对{1,2,2,3,3,null, null, 4,4}输入{1,2,2,3,3,0,0,4,4}TreeNode* create(vectorvec,int num){ TreeNode* root = new TreeNode(vec[0]); queue q; q.push(root); int i = 1; while(i <
2016-05-24 21:17:29 535
原创 int 与 string 相互转换
int转化为string1、使用itoa(int to string) 1 //char *itoa( int value, char *string,int radix); 2 // 原型说明: 3 // value:欲转换的数据。 4 // string:目标字符串的地址。 5 // radix:转换后的进制数,可以是10进制、16进制等。 6 // 返回
2016-05-05 16:14:38 1726
原创 187. Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri
2016-05-31 20:44:25 360
原创 260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given
2016-05-31 19:17:40 285
原创 205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot
2016-05-30 23:17:41 345
原创 290. Word Pattern(技巧:记录当前位置来判断两个字符串是否符合)
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
2016-05-30 23:15:55 1060
原创 202. Happy Number
1.我的答案这里我假设输入最大值为 2147483647, 则10位的数字经过计算后也不过810,因此我开辟811的数组来存。class Solution {public: int isH(int n, vector& num){ int sum = 0; while(n){ int yu = n % 10; sum += yu*y
2016-05-30 21:52:40 223
原创 36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille
2016-05-30 21:06:40 249
原创 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 node
2016-05-29 21:03:27 276
原创 二叉搜索树的性质
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 1. 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 2. 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 3. 它的左、右子树也分别为二叉排序树。因此,若对它进行中序遍历,则是一颗递增的排好序的序列! 这一点
2016-05-28 20:56:23 2183
原创 106. Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2016-05-28 18:22:28 269
原创 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.1.我的首先想法(递归建树) 但是超时了/** * Definition for a bin
2016-05-28 16:19:19 242
原创 103. Binary Tree Zigzag Level Order Traversal(技巧:应用层次遍历+记录每层个数)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary
2016-05-27 22:20:22 353
原创 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \
2016-05-27 22:01:15 241
原创 98. Validate Binary Search Tree(提交了好几次,终于.....)
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th
2016-05-27 14:08:46 313
原创 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2016-05-27 11:46:35 255
原创 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-05-26 21:41:00 243
原创 将序列按照想要的排序,去重
将一段序列按照字典序排序,去重,按照长度大小排序,相等长度按照字典序排序。c++ primer中的一段程序#include #include using namespace std;void elimDups(vector &words){ //按字典序排序words,以便查找重复单词 sort(words.begin(), words.end()); //un
2016-05-26 19:52:53 875
原创 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely fille
2016-05-25 21:15:12 226
原创 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the
2016-05-24 22:05:46 356
原创 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the larges
2016-05-16 22:24:40 251
原创 343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, ret
2016-05-16 21:44:39 288
原创 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =
2016-05-16 20:25:27 263
原创 改重排sort
对字符串序列,进行先按字节长度大小排序,相同长度的按照字典序排序。对stable_sort传入更改的排序机制。二元谓词//// main.cpp// practise//// Created by zjl on 16/5/9.// Copyright © 2016年 zjl. All rights reserved.//#include #include
2016-05-11 20:03:38 247
原创 91. Decode Ways 数字转字母的不同编码方式
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu
2016-05-08 22:25:31 591
原创 300. Longest Increasing Subsequence 最长子序列
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther
2016-05-08 16:31:57 211
原创 subarray、subsequence的区别
subarray是截取数组中连续的一段子数组。subsequence是序列中不连续的一段子序列。[1,2,3,4,5,6]subarray=[3,4,5];subsequence=[2,4,5];
2016-05-08 15:53:02 2626
原创 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] ha
2016-05-08 15:50:01 280
原创 64. Minimum Path Sum 路径最小总和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at
2016-05-08 14:17:11 625
原创 63. Unique Paths II 找唯一途径2(中间有路障)
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the
2016-05-07 23:36:03 308
原创 62. Unique Paths 唯一路径的条数
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the
2016-05-07 22:35:15 609
原创 338. Counting Bits 数字的二进制中1的个数
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
2016-05-07 21:31:47 2948
原创 Path Sum 路径和(注:同时包含得到各个路径的模板:两种不同表达形式的代码)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum
2016-05-06 11:40:15 655
原创 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.解答:1.我的答案(递归)/** * Definition
2016-05-06 11:06:19 333
原创 Binary Tree Paths 二叉树的路径(含创建树的代码)
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]1.我的答
2016-05-05 16:17:38 390
原创 Symmetric Tree 对称树
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 3But the f
2016-05-05 11:33:15 344
原创 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,#,#,15,7},
2016-05-05 10:29:15 253
原创 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order
2016-05-03 22:50:06 337
原创 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","
2016-05-03 11:44:02 279
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人