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] * ...
随机推荐
- Verilog代码和FPGA硬件的映射关系(五)
既然我们可以指定寄存器放在IOB内,那我们同样也可以指定PLL的位置.首先要确保我们有多个PLL才行.如图1所示,我们所使用的EP4CE10F17C8芯片刚好有两个. 图 1 为了演示这个例子,我们使 ...
- web自动化之执行js脚本
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- 问答题:你下班后是选择关电脑?Or,只关闭显示器?
首百问答的答案:jingmentudou 因为你永远不知道什么时间会被叫醒.开个远程就能避免半夜去公司了. 月尧jade 干这一行的,电脑自按下开机键开始,除了驱动会自动启动外,你需要重新检查各大运行 ...
- [Flutter] 音频播放插件 audioplayers 的一个路径坑
pubsepc.yaml 文件 flutter: assets: video/video-01.mp4 video/video-02.mp4 audio/bg.mp3 以上关于视频的文件的配置,都能正 ...
- Android_适配器(adapter)之SimpleAdapter
概述 SimpleAdapter是一种 简单的适配器,将静态数据映射到布局xml对应的视图上.它也是BaseAdapter的子类. SimpleAdapter数据映射的组件有3类(从官网api或Sim ...
- css:选择器(标签、类、ID、通配符)
1.css概述 主要的使用场景就是美化网页,布局页面 (1)html的局限性 它只关注内容的语义,只能做一些简单的样式,并且非常的臃肿和繁琐 (2)css对网页美化的作用 css是层叠样式表的简称,它 ...
- 派生类Student的构造函数和析构函数 代码参考
#include <iostream> #include <cstring> using namespace std; class Person { private: char ...
- Spring boot Sample 006之spring-boot-custom-servlet
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.步骤 2.1.点击File -> New Project -> Spring Initializer,点击next 2 ...
- Java Word中的文本、图片替换功能
Word中的替换功能以查找指定文本然后替换为新的文本,可单个替换或全部替换.以下将要介绍的内容,除常见的以文本替换文本外,还将介绍使用不同对象进行替换的方法,具体可包括: 1. 指定字符串内容替换文本 ...
- Rocket - diplomacy - misaligned
https://mp.weixin.qq.com/s/poCJBcx45clXHm6Uuv8M6w 介绍AddressSet.misaligned的实现.之前介绍的比较概括,也有偏差.这里根据实际执行 ...