记录下力扣上的剑指题。目前是第一遍,看了题解的前面标*

面试题06 从尾到头打印链表

解法一:利用stack的LIFO性质。时间31.14% 空间100.00%

class Solution {
public:
vector<int> reversePrint(ListNode* head) {
if(!head) return vector<int>{};
stack<int> st;
while(head) {
st.push(head->val);
head = head->next;
}
vector<int> ret;
while(!st.empty()) {
ret.push_back(st.top());
st.pop();
}
return ret;
}
};

解法二:递归 时间66.86%,空间100%

class Solution {
public:
vector<int> reversePrint(ListNode* head) {
if(!head) return vector<int>{}; vector<int> ret = reversePrint(head->next);
ret.push_back(head->val);
return ret;
}
};

面试题07 重建二叉树

解法一:递归.效率很低,时间5%需要优化

class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
//前序确定根,切分后递归
if(preorder.empty())
return nullptr;
int root_val = preorder[0];
int i = 0;
while(inorder[i] != root_val) ++i;
vector<int> in1, in2, pre1, pre2;
copy(inorder.begin(), inorder.begin() + i, back_inserter(in1));
copy(inorder.begin() + i + 1, inorder.end(), back_inserter(in2));
copy(preorder.begin() + 1, preorder.begin() + 1 + in1.size(), back_inserter(pre1));
copy(preorder.begin() + 1 + in1.size(), preorder.end(), back_inserter(pre2));
TreeNode* root = new TreeNode(root_val);
root->left = buildTree(pre1, in1);
root->right = buildTree(pre2, in2);
return root;
}
};

面试题09 用两个栈实现队列

class CQueue {
private:
stack<int> s1;
stack<int> s2;
public:
CQueue() { } void appendTail(int value) {
s1.push(value);
} int deleteHead() {
int ret = -1;
if(!s2.empty()) {
ret = s2.top();s2.pop();
} else if(!s1.empty()) {
while(!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
ret = s2.top();s2.pop();
} return ret;
}
}; /**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/

面试题10- I 斐波那契数列

class Solution {
public:
int fib(int n) {
int dp[101];
dp[0] = 0;dp[1] = 1;
for(int i = 2; i<=n; ++i) {
dp[i] = (dp[i-1] + dp[i-2])%1000000007;
}
return dp[n];
}
};

面试题11 旋转数组的最小数字

class Solution {
public:
int minArray(vector<int>& numbers) {
int len = numbers.size();
int lo = 0, hi = len - 1, mid = 0; while(lo < hi) {
mid = (lo + hi) >> 1;
if(numbers[mid] < numbers[hi])
hi = mid;
else if(numbers[mid] > numbers[hi])
lo = mid + 1;
else if(numbers[mid] == numbers[hi])
hi = hi - 1;
}
return numbers[lo];
}
};

面试题12. 矩阵中的路径

dfs遍历回溯

class Solution {
private:
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int rowlen;
int collen;
bool visited[201][201]; bool dfs(vector<vector<char>>& board, string word, int row, int col, int idx) { if(row<0 || row >= rowlen || col<0 || col>=collen || board[row][col] != word[idx]) return false;
if( visited[row][col]) return false;
visited[row][col] = true;
if(idx == word.size() - 1) return true;
bool ret=false;
for(int k = 0; k < 4; ++k) {
int rx = row + dx[k];
int ry = col + dy[k];
if(dfs(board, word, rx, ry, idx + 1))
{ret=true;break;}
}
if(!ret) visited[row][col]=false; // 记得回溯
return ret;
} public:
bool exist(vector<vector<char>>& board, string word) {
rowlen = board.size();
collen = board[0].size();
for(int i=0; i < rowlen; ++i)
for(int j=0; j < collen; ++j) {
memset(visited, false, sizeof(visited));
if(dfs(board, word, i, j, 0)) return true;
}
return false;
}
};

visited数组可以优化掉

class Solution {
private:
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int rowlen;
int collen; bool dfs(vector<vector<char>>& board, string word, int row, int col, int idx) { if(row<0 || row >= rowlen || col<0 || col>=collen || board[row][col] != word[idx]) return false;
if(idx == word.size() - 1) return true; char tmp = board[row][col];
board[row][col] = '/';
bool ret=false;
for(int k = 0; k < 4; ++k) {
int rx = row + dx[k];
int ry = col + dy[k];
if(dfs(board, word, rx, ry, idx + 1))
{ret=true;break;}
}
board[row][col] = tmp;
return ret;
} public:
bool exist(vector<vector<char>>& board, string word) {
rowlen = board.size();
collen = board[0].size();
for(int i=0; i < rowlen; ++i)
for(int j=0; j < collen; ++j) {
if(dfs(board, word, i, j, 0)) return true;
}
return false;
}
};

面试题52. 两个链表的第一个公共节点

class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* h1,*h2;
h1 = headA; h2 = headB;
while(h1 != NULL) {
for(h2 = headB; h2 != NULL; h2 = h2->next) {
if(h1 == h2) return h1;
}
h1 = h1->next;
}
return NULL;
}
};

死方法,效率低。可以用快慢指针

面试题53 - I. 在排序数组中查找数字 I

class Solution {
public:
int search(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for(auto c: nums)
++hash[c];
return hash[target];
}
};

哈希效率挺低的,注意是排好序的,想到二分法。求左右边界即可


leetcode-cn 剑指offer的更多相关文章

  1. LeetCode:“剑指 Offer”

    LeetCode:"剑指 Offer" 刷题小菜鸡,花了几天时间做了一遍 LeetCode 上给出的 "剑指 Offer" 在此做一下记录 LeetCode主页 ...

  2. 【LeetCode】剑指 Offer 04. 二维数组中的查找

    二维数组查找:线性查找法 有二维数组: [  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, ...

  3. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  4. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  5. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  6. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  7. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  8. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  9. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  10. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

随机推荐

  1. StringUtils、CollectionUtils工具类的常用方法

    唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...

  2. RESTful api 功能测试

    0 为什么要写测试代码 代码写好了,如果能点或者能看,开发人员一般会自己点点或看看,如果没有发现问题就提交测试:更进一步,代码写好后,运行测试代码,通过后提交测试.将流程抽象下: 功能1编码-> ...

  3. 用Linux感觉低效吗?来看看这几个技巧!

      Linux已经成为目前最火的操作系统之一,尽管现在的Linux用户很多,但很多使用Linux的同学发现,他们在Linux下的工作效率并不高,那么这是为什么呢?其实使用Linux也可以很舒适,通过一 ...

  4. 脚本学习一(echo、echo off、@、start)

    1.echo表示显示此命令后的字符 脚本: 输出结果: 2.echo off表示在此语句后所有运行的命令都不显示命令行本身 脚本: 输出结果: 3.@与echo off相象,但它是加在每个命令行的最前 ...

  5. Excel表格中无法中间插入新行列! 提示:在当前工作表的最后一行或列中,存在非空单元格,解决方案

    excel中新增行列时报错: 提示:在当前工作表的最后一行或列中,存在非空单元格,所以无法插入新行或新列.

  6. 02)php基础知识

    综述 学习网址 菜鸟教程-PHP 所有学习内容皆来自以上网站. 基础 语法 PHP 脚本以 结束; PHP 文件的默认文件扩展名是 ".php". <?php echo &q ...

  7. 七个生产案例告诉你BATJ为何选择ElasticSearch!应用场景和优势!

    本文来源于公众号[胖滚猪学编程],转载请注明出处. 从今天开始,想和你一起死磕ElasticSearch,学习分布式搜索引擎,跟着胖滚猪就对了! 既然是ES的第一课,那么最重要的是让你爱上它!不想说那 ...

  8. Beta冲刺 —— 6.1

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.讨论并解决每个人存在的问 ...

  9. Rocket - debug - TLDebugModuleInner - ROM Generation

    https://mp.weixin.qq.com/s/j_CgHU4PnY82NMwJzOqHYg 简单介绍Variable ROM Generation. 1. jalAbstract jalAbs ...

  10. 使用ADMT和PES实现window AD账户跨域迁移-介绍篇

    使用 ADMT 和 pwdmig 实现 window AD 账户跨域迁移系列: 介绍篇 ADMT 安装 PES 的安装 ADMT:迁移组 ADMT:迁移用户 ADMT:计算机迁移 ADMT:报告生成 ...