-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
43 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,7 @@ | ||
# 题意 | ||
# 题目描述: | ||
|
||
题目描述 | ||
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。 | ||
|
||
输入一个字符串,按字典序打印出该字符串中字符的所有排列 | ||
|
||
例如输入字符串abc, | ||
|
||
则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 | ||
|
||
结果请按字母顺序输出。 | ||
|
||
注意 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母 | ||
|
||
样例输入 | ||
|
||
1 2 3 2 2 2 5 4 2 | ||
|
||
样例输出 | ||
|
||
2 | ||
|
||
# 分析 | ||
|
||
- 排序以后统计个数 | ||
- 基于Partition函数的$O(n)$的查找第K大的数 | ||
- 阵地攻守(特点-它出现的次数比其他所有的数组出现的次数之和还要多)pick | ||
- 使用map来建立数字到出现次数的映射 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,8 @@ | ||
# 题目描述 | ||
# 题意 | ||
|
||
请实现两个函数,分别用来序列化和反序列化二叉树。这里没有规定序列化的方式 | ||
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 | ||
|
||
# 分析 | ||
# 层次遍历后结果之按字输出 | ||
|
||
## 遍历二叉树 | ||
其实我们可以借鉴层次遍历时候的思路, 参见剑指Offer--060-把二叉树打印成多行, 我们把层次遍历的结果保存在vector< vector >中每一层保存在一个vector中, 那么我们输出的时候就可以进行调整, 按照之字形输出即可 | ||
|
||
其实这道题约定的序列化没有固定的格式, 只要你序列化后的结果, 再反序列化后与原树相同即可, | ||
|
||
因此我们可以随意指定自己的格式, | ||
|
||
比如空节点用$表示,或则#表示, | ||
|
||
然后遍历采用先序, 中序, 后序或者层次都可以, | ||
|
||
我们的示例程序中采用空结点用#表示, 结点与结点用逗号,分隔 | ||
|
||
选择了合适的遍历算法, 那么剩下的问题就是字符串序列和整数权值的相互转换问题 | ||
序列化的关键, 其实就是将树的权值(整数)转换为字符串序列, 可以采用ostringstream, sprintf和itoa | ||
|
||
反序列化的关键, 则正好相反, 将字符串转换为整数, 可以使用istringstream, sscanf和atoi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
# 题意 | ||
# 题目描述 | ||
|
||
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 | ||
请实现两个函数,分别用来序列化和反序列化二叉树。这里没有规定序列化的方式 | ||
|
||
# 层次遍历后结果之按字输出 | ||
# 分析 | ||
|
||
其实我们可以借鉴层次遍历时候的思路, 参见剑指Offer--060-把二叉树打印成多行, 我们把层次遍历的结果保存在vector< vector >中每一层保存在一个vector中, 那么我们输出的时候就可以进行调整, 按照之字形输出即可 | ||
## 遍历二叉树 | ||
|
||
其实这道题约定的序列化没有固定的格式, 只要你序列化后的结果, 再反序列化后与原树相同即可, | ||
|
||
因此我们可以随意指定自己的格式, | ||
|
||
比如空节点用$表示,或则#表示, | ||
|
||
然后遍历采用先序, 中序, 后序或者层次都可以, | ||
|
||
我们的示例程序中采用空结点用#表示, 结点与结点用逗号,分隔 | ||
|
||
选择了合适的遍历算法, 那么剩下的问题就是字符串序列和整数权值的相互转换问题 | ||
序列化的关键, 其实就是将树的权值(整数)转换为字符串序列, 可以采用ostringstream, sprintf和itoa | ||
|
||
反序列化的关键, 则正好相反, 将字符串转换为整数, 可以使用istringstream, sscanf和atoi |