刷题记录leetcode 剑指 Offer(第 2 版)03-11
Posted I"ll carry you
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题记录leetcode 剑指 Offer(第 2 版)03-11相关的知识,希望对你有一定的参考价值。
文章目录
刷题地址: https://leetcode.cn/problem-list/xb9nqhhg/
剑指 Offer 03. 数组中重复的数字
// 交换法:不断 交换 到 正确的位置上去
class Solution
public:
int findRepeatNumber(vector<int>& nums)
int n = nums.size();
for (int i = 0;i < n;i ++ )
while(nums[nums[i]] != nums[i]) swap(nums[nums[i]], nums[i]);
if(nums[i] != i) return nums[i];
return 0;
;
剑指 Offer 04. 二维数组中的查找
// 以 右上角 为 查找基准
class Solution
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target)
if(matrix.empty() || matrix[0].empty()) return false;
int n = matrix.size(), m = matrix[0].size();
int i = 0, j = m - 1;
while(i < n && j >= 0)
if(matrix[i][j] == target) return true;
else if(matrix[i][j] > target) j --;
else i ++ ;
return false;
;
剑指 Offer 05. 替换空格
class Solution
public:
string replaceSpace(string s)
string res = "";
for(auto c : s)
if(c == ' ') res += "%20";
else res += c;
return res;
;
剑指 Offer 06. 从尾到头打印链表
/**
* Definition for singly-linked list.
* struct ListNode
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL)
* ;
*/
class Solution
public:
vector<int> reversePrint(ListNode* head)
// 链表遍历
vector<int> res;
while(head)
res.push_back(head->val);
head = head->next;
reverse(res.begin(), res.end());
return res;
;
剑指 Offer 07. 重建二叉树
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL)
* ;
*/
class Solution
public:
unordered_map<int,int> pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
for(int i =0;i < inorder.size();i ++ ) pos[inorder[i]] = i;
return dfs(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size());
TreeNode* dfs(vector<int>& preorder, vector<int>& inorder, int pl, int pr, int il, int ir)
if(pl > pr) return NULL;
int root_val = preorder[pl];
int p = pos[root_val];
int len = p - il;
auto root = new TreeNode(root_val);
root->left = dfs(preorder, inorder, pl + 1, pl + len, il, il + len);
root->right = dfs(preorder, inorder, pl + len + 1, pr, p + 1, ir);
return root;
;
剑指 Offer 09. 用两个栈实现队列
class CQueue
public:
stack<int> s, cache;
void copy(stack<int>& s, stack<int>& cache)
while(s.size())
cache.push(s.top());
s.pop();
CQueue()
void appendTail(int value)
copy(s, cache);
s.push(value);
copy(cache, s);
int deleteHead()
if(s.empty()) return -1;
int t = s.top();
s.pop();
return t;
;
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
文章目录
剑指 Offer 10- I. 斐波那契数列
class Solution
public:
int fib(int n)
if(n <= 1) return n;
int a = 0, b = 1, c;
int mod = 1e9 + 7;
for(int i = 0;i < n - 1;i ++ )
c = (a + b) % mod;
a = b, b = c;
return c;
;
剑指 Offer 10- II. 青蛙跳台阶问题 LCOF
class Solution
public:
int numWays(int n)
int mod = 1e9 + 7;
int a = 1, b = 1;
for(int i = 0; i< n;i ++ )
int c = (a + b) % mod;
a = b, b = c;
return a;
;
剑指 Offer 11. 旋转数组的最小数字
class Solution
public:
int minArray(vector<int>& numbers)
// 二分,满足二段性: nums[i] < nums[0]
int n = numbers.size() - 1;
while(n > 0 && numbers[n] == numbers[0]) n -- ; // 去除重复的数
if(numbers[n] >= numbers[0]) return numbers[0]; // 已经满足 单调性
int l = 0, r = n;
while(l < r)
int mid = l + r >> 1;
if(numbers[mid] < numbers[0]) r = mid;
else l = mid + 1;
return numbers[r];
;
leetcode刷题总目录
以上是关于刷题记录leetcode 剑指 Offer(第 2 版)03-11的主要内容,如果未能解决你的问题,请参考以下文章