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

面试题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. java对数据库性能测试

    package com.mysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepar ...

  2. 关于pytest使用allure生成报告时,报一堆警告和缺少XX模块

    因为最新的pytest 支持*.josn的用例报告,卸载旧的模块使用新的即可: 需要移除旧模块:pip uninstall pytest-allure-adaptor, 并安装:pip install ...

  3. LTE常用标识和参数

    1 基本标识 1 .1 IMSI 1.2 IMEI 1.3 MSISDN 1.4 TMSI 1.5 MSRN 2 区域类标识 2.1 GCI 其中 LA是GSM(2g)中的位置区,对应4G中的跟踪区T ...

  4. 小智的糖果(Candy) 51nod 提高组试题

    luogu AC通道! (官方数据) 题目描述 小智家里来了很多的朋友,总共有N个人,站成一排,分别编号为0到N-1,小智要给他们分糖果.但 是有的朋友有一些特殊的要求,有的人要求他左右的两个人(左边 ...

  5. Vim的三款实用插件

    Vim 是 Linux 下的常用文本编辑器,但也经常被称为是一个上古神器,因为它对于初学者而言相当不友好,也不好入门. 但是,对于高手而言,他们不仅将 Vim 玩得很溜,而且还将它当作代码开发的主要工 ...

  6. 50个SQL语句(MySQL版) 问题三

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...

  7. (Java实现) 洛谷 P1319 压缩技术

    题目描述 设某汉字由N X N的0和1的点阵图案组成,如下图.我们依照以下规则生成压缩码.连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从左到右,由上至下.第一个数表示连续有几个0 ...

  8. Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)

    744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...

  9. Java实现 LeetCode 695 岛屿的最大面积(DFS)

    695. 岛屿的最大面积 给定一个包含了一些 0 和 1 的非空二维数组 grid . 一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在水平或者竖直方向上相 ...

  10. Java实现 LeetCode 404 左叶子之和

    404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...