剑指offer
issac199854
种子不是一朝一夕就能发芽结出果实 而你手中的苹果耽搁了时间就不再好吃了
展开
-
替换空格
class Solution {public: void replaceSpace(char *str,int length) { if((NULL == str )||(length<=0)) { return; } int originalleng...原创 2018-12-09 14:03:57 · 128 阅读 · 0 评论 -
反转链表
ListNode* ReverseList(ListNode* pHead){ ListNode* newList = NULL; ListNode* cur = pHead; ListNode* pre = NULL; while (cur != NULL) { ListNode* next = cur-&gt;pNext; if (next == NULL) { ...原创 2019-02-27 00:17:47 · 129 阅读 · 0 评论 -
链表中倒数第K个结点
这里主要使考虑代码的强壮性。ListNode* ListLastK(ListNode* pHead, int K){ if (NULL == pHead || K == 0) return NULL; ListNode* pFast = pHead; for (unsigned int i = 0; i < K - 1; ++1) { if (pFast->pNe...原创 2019-02-27 00:06:09 · 136 阅读 · 0 评论 -
调整数组使奇数位于偶数前面
bool Even(int n){ return (n & 1) == 0;}void Reorder(int* array, int length, bool(*func)(int))//函数指针可以实现其他条件{ if (array == NULL || length == 0) return; int* begin = array; int* end = arr...原创 2019-02-26 23:50:53 · 127 阅读 · 0 评论 -
O(1)时间删除单链表节点
void DeleteNode(ListNode** pHead, ListNode* pDelete){ if (!pHead || !pDelete) return; if (pDelete-&gt;pNext != NULL) { ListNode* next = pDelete-&gt;pNext; pDelete-&gt;nValue = next-&gt;nVal...原创 2019-02-26 23:28:29 · 322 阅读 · 0 评论 -
顺时针打印矩阵
void PrintCircle(int(*array)[4], int row, int col, int start){ int endCol = col - 1 - start; int endRow = row - 1 - start; for (int i = start; i &lt;= endCol; ++i)//从左-&gt;右 { printf("%d ", arr...原创 2019-03-01 00:27:37 · 175 阅读 · 0 评论 -
输入一个数组,判断该数组是不是二叉搜索树的后序遍历序列
bool IsBSTreePostOrder(int* array,int size){ if(NULL == array || size&lt;0) return 0; int root = array[size-1];//序列的最后一个元素即为二叉搜索树的根节点 int i = 0; for(;i&lt;size-1;++i) { if(array[i]&gt;root)...原创 2019-02-28 17:53:47 · 450 阅读 · 0 评论 -
合并有序链表
#include &lt;stdio.h&gt;#include &lt;malloc.h&gt;typedef int DataType;typedef struct ListNode{ int _value; struct ListNode* _next;}ListNode;ListNode* Merge(ListNode* pHead1,ListNode* pHead...原创 2019-02-28 01:34:39 · 124 阅读 · 0 评论 -
二叉树的镜像
#include &lt;stdio.h&gt;typedef struct BTNode{ int _value; struct BTNode* _left; struct BTNode* _right;}BTNode;template&lt;class T&gt;void Swap(T&amp; x,T&amp; y){ T temp = x; x = y; ...原创 2019-02-28 01:33:45 · 280 阅读 · 0 评论 -
树的子结构
#include &lt;stdio.h&gt;typedef struct BTNode { int _value; struct BTNode* _left; struct BTNode* _right;}BTNode;bool DoesTree1HaveTree2(BTNode* root1,BTNode* root2){ if(root2 == NULL) ...原创 2019-02-28 01:33:01 · 158 阅读 · 0 评论 -
数值的整数次方
double PowerWithUnsignedExponent(double base, int exponent){ if (exponent == 0) return 1; if (exponent == 1) return base; double result = PowerWithUnsignedExponent(base, exponent &gt;&gt; 1);...原创 2018-12-09 23:49:54 · 140 阅读 · 0 评论 -
二进制中1的个数(三种方法比较)
背景:给一个数,求这个数以二进制形式中1的个数//只能求正数二进制中1的个数int _1numof1(int n){ int count = 0; while (n) { if (n & 1) count++; n = n >> 1; } return count;}//可以求正数和负数二进制中1的个数,再32位机器下,需要循环32次。in...原创 2018-12-09 20:46:08 · 563 阅读 · 0 评论 -
斐波那契数列
//第一种解法:递归实现,可以理解为从后往前,但是当求N较大时,效率非常低。int fibN1(int n){ if (n &lt;= 0) return 0; if (n == 1) { return 1; } return fibN1(n - 1) + fibN1(n - 2);}//第二种解法:可以理解为从前往后。int fibN2(int n){ in...原创 2018-12-09 20:11:33 · 165 阅读 · 0 评论 -
旋转数组的最小数字
C语言int rotateArray(int *str, int length){ if (NULL == str || length &amp;lt;= 0) return; int left = 0; int right = length - 1; int mid = left; while (str[left] &amp;gt;= str[right]) { if (right -...原创 2018-12-09 19:18:47 · 145 阅读 · 0 评论 -
O(N)排序公司员工年龄
void sortages(int ages[], int length){ if ((ages == NULL) || length &lt;= 0) return; int oldage = 99; int timeofage[100] = {0};//用来记录1-99岁每个年龄的人数。 for (int i = 0; i &lt; length; i++) { int...原创 2018-12-09 17:41:16 · 216 阅读 · 0 评论 -
两个栈实现一个队列
1.stack1用来插入元素2.若删除元素,则将stack1中的元素倒入stack2中,再取stack2栈顶元素3.若stack1中有元素,stack2中也有元素,那么这时候删除应该怎么办?其实仔细想一下很简单,stack2中的元素本来就比stack1中的元素入队列早一些,所以此时直接弹出stack2中的元素。class Solution{public: void push(in...原创 2018-12-09 16:50:37 · 144 阅读 · 0 评论 -
重建二叉树
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti...原创 2018-12-09 16:25:13 · 187 阅读 · 0 评论 -
从尾到头打印单链表
一、使用begin()与end()/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {* }* };*/class Solution {public...原创 2018-12-09 14:56:02 · 273 阅读 · 0 评论 -
复杂链表的复制
#include <stdlib.h>#include <stdio.h>typedef struct RandomListNode{ int _val; struct RandomListNode* _next; struct RandomListNode* _random;}RandomListNode;class solution{ p...原创 2019-03-03 11:56:14 · 158 阅读 · 0 评论