札记——Leetcode编程刷题记录
- 1. [剑指 Offer 35. 复杂链表的复制](https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/submissions/)
- 2.[剑指 Offer 05. 替换空格](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/)
- 3.[剑指 Offer 53 - I. 在排序数组中查找数字 I](https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/submissions/)
- 4.[剑指 Offer 04. 二维数组中的查找](https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/submissions/)
- 5.[面试题50. 第一个只出现一次的字符](https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/)
- 6.[剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
- 7.[剑指 Offer 10- I. 斐波那契数列](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/)
- 8.[剑指 Offer 46. 把数字翻译成字符串](https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/)
- 8.[剑指 Offer 15. 二进制中1的个数](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/)
- 9.[剑指 Offer 52. 两个链表的第一个公共节点](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/)
1. 剑指 Offer 35. 复杂链表的复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
- 深拷贝, 回溯法
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
unordered_map<Node* , Node*> cacheNode; // 用哈希表映射当前链表节点和深拷贝新链表节点
//回溯法 先完成一个新节点的创建和赋值,然后赋值当前节点的下一个指针和随机指针
Node* copyRandomList(Node* head) {
// 当前节点为空
if(head == nullptr) {
return nullptr;
}
// 当前节点对应的哈希映射节点还未创建
while(!cacheNode.count(head)) {
//创建新节点
Node* newNode = new Node(head->val);
cacheNode[head] = newNode;
newNode->next = copyRandomList(head->next);
newNode->random = copyRandomList(head->random);
}
// 返回当前节点哈希表中对应的新节点
return cacheNode[head];
}
};
2.剑指 Offer 05. 替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
*find(), replace()
class Solution {
public:
string replaceSpace(string s) {
int pos = string::npos;
while((pos = s.find(' ')) != string::npos) {//寻址目标子串位置
s.replace(pos, 1, "%20"); //用目标子串代替当前pos特定长度的子串
}
return s;
}
};
3.剑指 Offer 53 - I. 在排序数组中查找数字 I
统计一个数字在排序数组中出现的次数。
二分查找
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int mid = 0;
int ll = -1, rr = -1;
//left
while(left <= right) {
mid = left + ((right - left) / 2);
if(target > nums[mid]) {
left = mid + 1;
}
if(target < nums[mid]) {
right = mid - 1;
}
if(target == nums[mid]) {
do{
--mid;
}while(left <= mid && target == nums[mid]);
ll = mid + 1;
break;
}
}
//right
while(left <= right) {
mid = left + ((right - left) / 2);
if(target > nums[mid]) {
left = mid + 1;
}
if(target < nums[mid]) {
right = mid - 1;
}
if(target == nums[mid]) {
do{
++mid;
}while(right >= mid &&target == nums[mid]);
rr = mid - 1;
break;
}
}
if(ll == -1) {
return 0;
}
return rr - ll + 1;
}
};
/*
int search(vector<int>& nums, int target) {
int count = 0;
auto it = nums.begin();
while((it = find(it, nums.end(), target)) != nums.end() ) {
++count;
++it;
}
return count;
}
*/
/*
int search(vector<int>& nums, int target) {
map<int, int> count;
for(auto v : nums) {
++count[v];
}
return count[target];
}
*/
4.剑指 Offer 04. 二维数组中的查找
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
-
题解 从左下角开始,向右上角搜索
-
复杂度分析:
时间复杂度: O(M+N),其中,NN 和 MM 分别为矩阵行数和列数,此算法最多循环 M+NM+N 次。
空间复杂度: O(1) : i, j 指针使用常数大小额外空间。
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
//利用有序二维数组的性质,转化为树;从左下角开始,所在行右侧>每个元素>所在列上方。
int row = matrix.size();
if(row == 0) {
return false;
}
int cow = matrix[0].size();
int i = row -1, j = 0;
while(i >= 0 && j < cow) {
if(matrix[i][j] > target) {
// target < 当前元素所在行右侧,上移一行
--i;
continue;
}
if(matrix[i]