leetcode-cn 剑指offer
记录下力扣上的剑指题。目前是第一遍,看了题解的前面标*
面试题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的更多相关文章
- LeetCode:“剑指 Offer”
LeetCode:"剑指 Offer" 刷题小菜鸡,花了几天时间做了一遍 LeetCode 上给出的 "剑指 Offer" 在此做一下记录 LeetCode主页 ...
- 【LeetCode】剑指 Offer 04. 二维数组中的查找
二维数组查找:线性查找法 有二维数组: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 剑指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] * ...
随机推荐
- DFS与DP算法
名词解释: DFS(Dynamic Plan):动态规划 DFS(Depth First Search):深度优先搜索 DFS与DP的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两 ...
- rocketmq初识
概念说明 通常一个消息队列需要掌握的知识点有Topic(主体).Producer(生产者).Consumer(消费者).Queue(队列).Delivery Semantics(消息传递范式) 蛋疼的 ...
- [SD心灵鸡汤]004.每月一则 - 2015.08
1.事常与人违,事总在人为. 2.骏马是跑出来的,强兵是打出来的. 3.驾驭命运的舵是奋斗.不抱有一丝幻想,不放弃一点机会,不停止一日努力. 4.如果惧怕前面跌宕的山岩,生命就永远只能是死水一潭. 5 ...
- [工具-008] C#邮件发送系统
邮件发送系统很多,但是我这边给大家展示下我最近开发的一款邮件发送系统,有参照网上的一个兄弟的界面,进行了升级,界面如下. 从界面上我们可以看到了该邮件系统有如下功能: 1)服务器的设置 2)发件人的设 ...
- static关键字修饰属性
static 静态的,可以修饰属性,方法,代码块(或初始化块) , 内部内 非static修饰的属性(实例变量):各个对象各自拥有一套各自的副本 static修饰属性(l类变量): 1.由类创建的所有 ...
- Rocket - debug - TLDebugModuleInner - DMI Register Control and Status
https://mp.weixin.qq.com/s/tI41wu0xaIQ5PRq6F82tNw 简单介绍TLDebugModuleInner中生成DMI控制和状态寄存器使用到的状态. 1. abs ...
- js匿名函数和date对象,math对象
匿名函数: <script type="text/javascript"> function (参数列表){ 要执行的语句块; } </script> 对象 ...
- Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
606. 根据二叉树创建字符串 你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原 ...
- Java实现 LeetCode 89 格雷编码
89. 格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输 ...
- 第七届蓝桥杯JavaB组国(决)赛部分真题
解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.愤怒小鸟 题目描述 X星球愤怒的小鸟喜欢撞火车! 一根平直的铁轨上两火车间相距 1000 米 两火车 (不妨称A和B) 以时速 10米 ...