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] * ...
随机推荐
- 什么是cookie?
cookie是什么? 其实cookies是由网络服务器存储在你电脑硬盘上的一个txt类型的小文件,它和你的网络浏览行为有关,所以存储在你电脑上的cookies就好像你的一张身份证,你电脑上的cooki ...
- 2018京东校招Java笔试题
相比阿里巴巴,京东的题都是考研基础题,加上一点java基础知识和linux命令. 1. 单选题(19道题,每题2分): 1)4个并发进程都需要5个同类资源,则至少需要多少个资源,才不会导致死锁? 2) ...
- IT笑话十则(二)
一.女程序员征婚 女程序员是这么征婚的: SELECT * FROM 男人们 WHERE 未婚=true and 同性恋=false and 有房=true and 有车=true and 条件 in ...
- 记一次使用windbg排查内存泄漏的过程
一.背景 近期有一个项目在运行当中出现一些问题,程序顺利启动,但是观察一阵子后发现内存使用总量在很缓慢地升高, 虽然偶尔还会往下降一些,但是总体还是不断上升:内存运行6个小时候从33M上升到80M: ...
- 自动化测试: Selenium 自动登录授权,再 Requests 请求内容
Selenium 自动登录网站.截图及 Requests 抓取登录后的网页内容.一起了解下吧. Selenium: 支持 Web 浏览器自动化的一系列工具和库的综合项目. Requests: 唯一的一 ...
- 2003 can't connect to mysql server
在电脑中打开 计算机管理 点击 服务与应用程序 点击 服务 右侧找到 mysql 右键启动
- eclipse 界面复原
Windows-----Perspective-----Reset perspective
- Spring Boot笔记(六) springboot 集成 timer 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...
- 从SpringBoot源码分析 主程序配置类加载过程
1.@Import(AutoConfigurationPackages.Registrar.class) 初始SpringBoot 我们知道在SpringBoot 启动类上有一个@SpringBoot ...
- (Java实现) 洛谷 P1223 排队接水
题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小. 输入输出格式 输入格式: 输入文件共两行,第一行为n:第二行分别 ...