
c++
文章平均质量分 59
孤独的根号13
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
删除vector中重复元素
vector v={1,2,4,3,5.2};则调用以下语句即可:sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());vector.pop_back()可以弹出最后加入的元素原创 2016-04-27 11:14:32 · 4117 阅读 · 1 评论 -
改重排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 · 280 阅读 · 0 评论 -
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 · 390 阅读 · 0 评论 -
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 · 247 阅读 · 0 评论 -
将序列按照想要的排序,去重
将一段序列按照字典序排序,去重,按照长度大小排序,相等长度按照字典序排序。c++ primer中的一段程序#include #include using namespace std;void elimDups(vector &words){ //按字典序排序words,以便查找重复单词 sort(words.begin(), words.end()); //un原创 2016-05-26 19:52:53 · 919 阅读 · 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-05-26 21:41:00 · 267 阅读 · 0 评论 -
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 · 283 阅读 · 0 评论 -
建立二叉树
建立二叉树如对{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 · 602 阅读 · 0 评论 -
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 · 345 阅读 · 0 评论 -
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 · 264 阅读 · 0 评论 -
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 · 268 阅读 · 0 评论 -
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 · 281 阅读 · 0 评论 -
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 · 375 阅读 · 0 评论 -
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 · 291 阅读 · 0 评论 -
二叉搜索树的性质
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 1. 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 2. 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 3. 它的左、右子树也分别为二叉排序树。因此,若对它进行中序遍历,则是一颗递增的排好序的序列! 这一点原创 2016-05-28 20:56:23 · 2657 阅读 · 0 评论 -
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 · 430 阅读 · 0 评论 -
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 · 291 阅读 · 0 评论 -
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 · 320 阅读 · 0 评论 -
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 · 283 阅读 · 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 node原创 2016-05-29 21:03:27 · 308 阅读 · 0 评论 -
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 · 357 阅读 · 0 评论 -
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 · 697 阅读 · 0 评论 -
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 · 279 阅读 · 0 评论 -
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 · 251 阅读 · 0 评论 -
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 · 379 阅读 · 0 评论 -
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 · 1133 阅读 · 0 评论 -
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 · 379 阅读 · 0 评论 -
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 · 3019 阅读 · 0 评论 -
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 · 645 阅读 · 0 评论 -
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 · 343 阅读 · 0 评论 -
string中split实现
对string中split的方法的实现其中pattern是需要划分的标识,是string类型vector split(string str, string pattern){ vector result; string::size_type pos; str+=pattern; int size = str.size();原创 2016-04-23 14:14:35 · 925 阅读 · 0 评论 -
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 · 676 阅读 · 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] ha原创 2016-05-08 15:50:01 · 308 阅读 · 0 评论 -
subarray、subsequence的区别
subarray是截取数组中连续的一段子数组。subsequence是序列中不连续的一段子序列。[1,2,3,4,5,6]subarray=[3,4,5];subsequence=[2,4,5];原创 2016-05-08 15:53:02 · 3083 阅读 · 0 评论 -
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 · 232 阅读 · 0 评论 -
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 · 314 阅读 · 0 评论 -
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 · 393 阅读 · 0 评论 -
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 · 666 阅读 · 0 评论 -
357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,22,33,44,原创 2016-06-23 23:11:50 · 232 阅读 · 0 评论 -
336. Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.Example 1:Give原创 2016-06-07 20:54:46 · 253 阅读 · 0 评论