offer

剑指offer-面试题30-包含min函数的栈-栈

你离开我真会死。 提交于 2019-12-05 15:37:05
/* 题目: 定义栈的数据结构,实现一个能够得到栈的最小元素的min函数。 */ /* 思路: 错误思路:每次保存当前最小的元素,如果当前最小元素弹出,最小元素是谁? 正确思路:两个栈,一个栈保存数据,另一个栈用于存储当前最小元素。 */ #include <iostream> #include<cstdlib> #include<stack> using namespace std; stack<int> m_data; stack<int> m_min; void push(int value) { m_data.push(value); int minVal = value; if(!m_min.empty()){ int temp = m_min.top(); if(temp < value){ minVal = temp; } } m_min.push(minVal); } void pop() { if(!m_data.empty()){ m_data.pop(); m_min.pop(); } } int top() { return m_data.top(); } int min() { return m_min.top(); } int main() { /* TreeNode *node7 = new TreeNode(7); TreeNode *node6 =

剑指offer-面试题26-树的子结构-二叉树

这一生的挚爱 提交于 2019-12-05 09:24:49
/* 题目: 输入两棵二叉树A和B,判断B是不是A的子树。 */ /* 思路: 1、注意浮点数大小的判断。 2、判断树A的某个节点是否和树B的根节点是否相同, 若相同,则判断以A该节点为根节点是否包含树B; 若不包含,判断A的左子树是否包含树B; 若不包含,判断A的右子树是否包含树B。 3、以A的某个节点为根,判断是否对应B的根节点, 判断A的左子树和B的左子树的相等性;判断A的右子树和B的右子树的相等性。 */ #include <iostream> #include<cstdlib> using namespace std; struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; bool equals(double num1, double num2){ if(num1-num2 > -0.0000001 && num1-num2 < 0.0000001){ return true; } return false; } bool doesHasSubtree(TreeNode* pNode1,TreeNode* pNode2){ if(pNode2 == nullptr)

剑指offer——二叉树的镜像

大兔子大兔子 提交于 2019-12-05 04:48:32
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 python ac代码: 递归: # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回镜像树的根节点 def Mirror(self, root): # write code here if root==None or (root.left==root.right==None): return root left=self.Mirror(root.left) right=self.Mirror(root.right) root.left=right root.right=left return root    来源: https://www.cnblogs.com/hit-joseph/p/11907197.html

剑指offer-面试题25-合并两个排序的链表-链表

时光怂恿深爱的人放手 提交于 2019-12-05 04:10:29
/* 题目: 输入两个递增排序的链表,合并这两个链表并使新的链表中的节点依然是递增排序。 返回新链表的头节点。 */ /* 思路: 1、返回的链表的头节点为两个链表中头节点数值更小的为链表1。 2、进行比较 3、判断链表2的节点是否为空,若不为空则全部加到链表1的尾部。 */ #include <iostream> #include<cstdlib> using namespace std; struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; ListNode* Merge(ListNode* pHead1, ListNode* pHead2){ if(pHead1 == nullptr) return pHead2; if(pHead2 == nullptr) return pHead1; ListNode* pNode = nullptr; ListNode* qNode = nullptr; ListNode* temp = nullptr; ListNode* head = nullptr; if(pHead1->val < pHead2->val){ pNode = pHead1; qNode = pHead2; head =

剑指offer-面试题23-链表中环的入口节点-双指针

℡╲_俬逩灬. 提交于 2019-12-05 03:10:54
/* 题目: 如果链表中包含环,如何找出环的入口? */ /* 思路: 双指针: 1、判断是否有环。 fast指针一次2步,slow指针一次1步,当fast与slow相遇时,说明有环。 2、判断环路中节点的个数。 当fast和slow相遇的节点在环上,一个指针固定, 另一个指针循环一周再次遇到该固定指针,遍历的个数即为节点个数。 3、找到入口节点。 fast指针先走k步, 接着,fast和slow指针同时遍历, 相遇节点即为入口节点。 */ #include<iostream> #include<string.h> #include<algorithm> #include<cmath> #include<stdio.h> using namespace std; struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; int LoopNodeLength(ListNode* pHead){ int length = 0; ListNode* slow = pHead; ListNode* fast = pHead->next; if(fast == nullptr) return length; while(fast->next != nullptr

剑指offer-面试题20-表示数值的字符串-字符串

混江龙づ霸主 提交于 2019-12-05 00:53:26
/* 题目: 判断字符串是否表示数值。 */ /* 思路: 字符串遵循模式A[.[B]][e|EC] ,[+|-].B[e|EC] A、C为可能带正负号的数字串 B为数字串 */ #include<iostream> #include<string.h> #include<algorithm> #include<cmath> #include<stdio.h> using namespace std; int index = 0; bool scanUnsignedInteger(char *str){ if(str[index] >= '0' && str[index] <= '9'){ while(str[index] >= '0' && str[index] <= '9'){ index++; } if(str[index] != '.' && str[index] != 'e' && str[index] != '\0' && str[index] != 'E'){ return false; } return true; } return false; } bool scanInteger(char* str){ if(str[index] == '+' || str[index] == '-'){ index++; } return

剑指offer-面试题18-删除链表的节点-链表

只愿长相守 提交于 2019-12-04 18:11:27
/* 题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。 */ /* 思路: 将要删除的节点的下一个节点的value和next复制过来,删除下一个节点。 考虑两种特殊情况: 节点为尾结点,则需要从头遍历。 节点既是尾结点也是头结点,需要将头节点指向的内容置为空。 */ struct ListNode{ int value; ListNode* next; }; void DeleteNode(ListNode** pListHead, ListNode* pToBeDeleted){ if(!pListHead || !pToBeDeleted){ throw("invalid parameters"); } //要删除的节点既是头结点,又是尾结点 if(*pListHead== pToBeDeleted && !pToBeDeleted->next){ *pListHead = nullptr; delete pToBeDeleted; pToBeDeleted = nullptr; }else if(!pToBeDeleted->next){//要删除的节点是尾结点if ListNode* pNode = *pListHead; while(!pNode->next->next){ pNode = pNode->next; } pNode-

剑指offer-面试题16-数值的整数次方-数字

纵饮孤独 提交于 2019-12-04 11:51:31
/* 题目: 实现函数double Power(double base,int exponent), 求base的exponent次方。 */ /* 思路: 本题需要考虑的情况较多: 1、0的0次方没有意义。 2、判断double值为0,需要使用精度。 3、考虑exponent为负数的情况。 可创新的点: 求x(n次方),可用x(n/2次方)*2(n/2次方) x为偶数 求x(n次方),可用x(n/2次方)*2(n/2次方)*x x为奇数 */ #include<iostream> #include<string.h> #include<algorithm> #include<cmath> using namespace std; #define MIN_VALUE 1e-8 bool g_InvalidInput = false; bool equal0(double num1){ if(abs(num1) < MIN_VALUE) return true; return false; } double PowerWithUnsignedExponent(double base,unsigned int exponent){ if(exponent == 0){ return 1; } if(exponent == 1){ return base; } //用位运算代替除法