基础
董道不渝
余将董道而不豫兮
展开
-
链表反转
#include#includestruct node{ int num; struct node * next;};node * reverse(node * x){ node * y = x, * t, * r = NULL; while(y != NULL) { t = y->next; y->next = r; r =原创 2014-12-19 11:02:46 · 342 阅读 · 0 评论 -
剑指offer:字符串的排列
题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba(结果请按字母顺序输出)。输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。class Solution {public: string tmp; vector res;原创 2015-12-02 21:50:00 · 416 阅读 · 0 评论 -
剑指offer:最小的K个数
题目描述:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。PS:一开始想到map是按key值升序排列,就新建了一个map,value存放数字出现的次数,没想到内存超出范围。class Solution {public: vector GetLeastNumbers_Solution(vector原创 2015-12-03 13:51:23 · 378 阅读 · 0 评论 -
剑指offer:二叉搜索树与双向链表
题目描述输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NUL原创 2015-12-01 17:06:09 · 383 阅读 · 0 评论 -
字节对齐(struct 和 union)
在没有#pragma pack宏的情况下,字节对齐应遵循以下三个基本原则:1、数据成员对齐规则:结构体(struct)或联合(union)的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储的起始位置要从该成员大小的整数倍开始。2、结构体作为成员:如果一个结构里有某些结构体成员,则结构体成员要从其内部最大元素大小的整数倍地址开始存储。3、收尾工作:结构原创 2015-11-29 12:44:36 · 833 阅读 · 0 评论 -
剑指offer:二叉树中和为某一值的路径
题目描述:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点。/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(N原创 2015-11-18 22:51:16 · 326 阅读 · 0 评论 -
剑指offer:把数组排成最小的数
题目描述输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。class Solution {public: string s, mn = "x"; string PrintMinNumber(vector numbers) { if (numbe原创 2015-12-08 19:17:51 · 373 阅读 · 0 评论 -
剑指offer之空格替换
#include #include #include #include #include using namespace std;class Solution{public: string replaceSpace(string str) { string ans; for(int i = 0, j = 0; i < str.si原创 2015-06-24 21:05:14 · 422 阅读 · 0 评论 -
360:最后一个字符
正在挑战一个CrackMe的你,把需要填写的前面几位密码都正确猜出了,可是这最后一位密码,好像藏得有点深。CrackMe的作者还挑衅般的在里面藏了个.tar.gz文件,解压缩出来,里面写道你要的最后一个字符就在下面这个字符串里。这个字符是下面整个字符串中第一个只出现一次的字符。 (比如,串是abaccdeff,那么正确字符就是b了)然而下面给出来的字符串好像太长太长了,单靠人力原创 2015-08-12 23:11:20 · 749 阅读 · 0 评论 -
leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.class Solution {public: int trailingZeroes(int n) { int c原创 2015-07-15 22:46:07 · 328 阅读 · 0 评论 -
leetcode:Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a原创 2015-07-15 21:37:05 · 431 阅读 · 0 评论 -
maximum-depth-of-binary-tree
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 tre原创 2015-07-02 16:39:20 · 377 阅读 · 0 评论 -
single-number
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 you implement it without using e原创 2015-07-02 10:41:41 · 324 阅读 · 0 评论 -
二叉树的镜像
#include #include #include #include #include using namespace std;string s;struct node{ char data; struct node * left; struct node * right;};node * creatBTree(){ if(s.siz原创 2015-06-30 15:01:54 · 350 阅读 · 0 评论 -
剑指offer之重建二叉树
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-06-26 09:41:40 · 420 阅读 · 0 评论 -
双向循环链表
题目描述:用户输入一个数,使26个字母排列顺序发生变化,例如输入3:,输出结果:DEFGHIJKLMNOPQRSTUVWXYZABC输入-3,输出结果:XYZABCDEFGHIJKLMNOPQRSTUVW当然,这是一道很简单的题目,用双向循环链表来写确实复杂了,主要是为了练习基本功。#include#includestruct node{ cha原创 2015-03-19 15:19:39 · 586 阅读 · 0 评论 -
快速找链表中间元素
思路:定义两个指针,一个速度是另一个的2倍,当快的指针到链表末尾,慢指针所指元素即中间元素。#include#includestruct node{ int num; struct node * next;};int main(void){ int i; node * a = (node *)malloc(sizeof node); no原创 2015-03-15 14:30:40 · 545 阅读 · 0 评论 -
表插入排序
[email protected]:snippets/560966.git原创 2014-12-24 11:35:46 · 577 阅读 · 0 评论 -
剑指offer:连续子数组的最大和
题目描述:例如{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。 class Solution {public: int FindGreatestSumOfSubArray(vector array) { if (array.size() == 0) return 0; vector v; int i; fo原创 2015-12-03 20:48:51 · 365 阅读 · 0 评论