剑指Offer
Yoc Lu
Do it yourself!
展开
-
剑指Offer - 用两个栈实现队列
//思路://栈1用来作入队列//栈2用来出队列,当栈2为空时,栈1全部出栈到栈2,栈2再出栈(即出队列)class Solution{public: void push(int node){ stack1.push(node); } int pop(){ int a; if(stack2.empty()){ ...原创 2019-03-29 14:08:27 · 148 阅读 · 0 评论 -
剑指Offer - 从尾到头打印链表
/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {* }* };*/class Solution{public: vector<in...原创 2019-03-29 08:33:28 · 142 阅读 · 0 评论 -
剑指Offer - 替换空格
//思路://1.从前往后插入移动的次数多,不建议//2.从后往前插入class Solution {public: void replaceSpace(char *str,int length) { if(str==NULL||length<0)return; int i=0; int oldnumber=0;//记录...原创 2019-03-28 21:35:35 · 186 阅读 · 0 评论 -
C++ 常量区 静态区 堆区 栈区
C++ 常量区 静态区 堆区 栈区参考博客C++中类的变量可以通过static、const、static const来修饰,不同的修饰在不同的情况下表示不同的含义。程序运行期间的内存分区:1.代码区:存放CPU指令码。2.常量区:存放只读常量,该区只读,不可写。3.静态区:存放静态变量,该区在程序编译完成后就决定了其大小,程序运行期间该区的大小不会变,该区可读写。4.动态区:分为堆区...原创 2019-03-28 20:53:04 · 3288 阅读 · 0 评论 -
剑指Offer - 二维数组中的查找
class Solution {public: bool Find(int target, vector<vector<int> > array) { int row=array.size(); int column=array[0].size(); bool flag=false; ...原创 2019-03-28 20:23:52 · 144 阅读 · 0 评论 -
掷骰子
掷骰子题目本题主要考察字符串操作,注意到,每一次翻转的时候,有两个方向的是不会变的,其他的四个方向也只是机械地进行交换。C++代码如下:#include <stdio.h>#include <cstring>void trans(char str[],char c){ if(c=='L'){ char temp=str[1]; ...原创 2019-03-26 21:31:04 · 848 阅读 · 0 评论 -
当出差遇上大雾
当出差遇上大雾题目本题考察深度优先搜索DFS,C++代码如下:#include<bits/stdc++.h>const int inf=0x7f7f7f7f;//在int型中0x7f7f7f7f也就是无穷大int d[50];int a[][7] = { {0}, {0, 0, 2, 10, 5, 3, -1}, {0, -1, 0, 12...原创 2019-03-27 14:52:24 · 400 阅读 · 0 评论 -
剑指Offer - 斐波拉契数列
class Solution {public: int Fibonacci(int n) { if(n==0)return 0; if(n==1)return 1; int a=0,b=1,temp=0; while(n>1){ temp=a+b; a...原创 2019-03-29 14:53:45 · 203 阅读 · 0 评论 -
剑指Offer - 旋转数组的最小数字
//C++ 二分法class Solution {public: int minNumberInRotateArray(vector<int> rotateArray) { if (rotateArray.empty()) return 0; int left = 0, right = rotateArray.size() - 1; ...原创 2019-03-29 14:39:04 · 119 阅读 · 0 评论 -
剑指Offer - 重建二叉树
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solu...原创 2019-03-29 10:26:54 · 117 阅读 · 0 评论