当前位置:网站首页>Brush the title "sword finger offer" day01
Brush the title "sword finger offer" day01
2022-07-25 08:05:00 【Pac Man programming】
Title source : Power button 《 The finger of the sword Offer》 The second edition
Completion time :2022/07/21
List of articles
03. Repeated numbers in an array

My explanation
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int> check(nums.size());
for (int i = 0;i < nums.size(); i++){
if(check[nums[i]] == 1){
return nums[i];
}
check[nums[i]] = 1;
}
return -1;
}
};
04. Search in a two-dimensional array

My explanation
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if(matrix.size() == 0){
return false;
}
int x = matrix[0].size() - 1,y = 0;
while(x >= 0 && y < matrix.size()){
if(matrix[y][x] == target){
return true;
}else if(matrix[y][x] > target){
x--;
}else {
y++;
}
}
return false;
}
};
05. Replace blank space

My explanation
class Solution {
public:
string replaceSpace(string s) {
string r = "";
for(int i = 0;i < s.size();i++) {
if(s[i] == ' '){
r += "%20";
}else {
r += s[i];
}
}
return r;
}
};
06. Print linked list from end to end

My explanation
Conventional methods
/** * 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> result;
while(head != nullptr) {
result.push_back(head->val);
head = head->next;
}
reverse(result.begin(),result.end());
return result;
}
};
Recursive implementation
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
// Recursive implementation
void check(vector<int>& result,ListNode* node) {
if(node->next != nullptr){
check(result,node->next);
}
result.push_back(node->val);
}
vector<int> reversePrint(ListNode* head) {
vector<int> result;
if(head != nullptr)
check(result,head);
return result;
}
};
边栏推荐
- Supplementary notes on Relevant Issues of complete model group
- CAS操作
- New version 8.6 SEO quick release system (can be built at source level)
- Learn when playing No 6 | the magic of document library lies in
- [paper notes] next vit: next generation vision transformer for efficient deployment in real industry
- mysql 如何获取两个集合的交集/差集/并集
- Advanced C language (XIII) - Example Analysis of dynamic memory management
- Surpassing transformer, Tsinghua, byte significantly refresh parallel text generation SOTA performance | ICML 2022
- Efcore's solution of multi tenant zero script, table and database read-write separation under SaaS system
- Uiautomator2 common commands
猜你喜欢

Oracle trigger creation

Enq: HW - fault analysis caused by content waiting

Raspberry connects EC20 for PPP dialing

【音视频】图片YUV数据格式

刷题《剑指Offer》day01

Science: listening to music can really relieve pain. Chinese scientists reveal the neural mechanism behind it
Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once
![[unity introduction program] basic concepts GameObject & components](/img/fc/7e0a6f057394a6fd4409a7bf518ba5.png)
[unity introduction program] basic concepts GameObject & components

文件详细操作

一款强大的端口扫描工具(nmap)
随机推荐
batchnorm 和layernorm的区别
[paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
How to create a simple electron desktop program
Use of toolbar
Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
People who lose weight should cry: it's no good not eating food, because your brain will be inflamed
滴滴 - dispatching
Open source, innovators win, 2022 "science and innovation China" open source innovation list selection is fully open!
CAS操作
P1086 [noip2004 popularization group question 2] peanut picking
eval与assert一句话木马分析
P1048 [NOIP2005 普及组 T3] 采药
[unity introduction program] basic concepts GameObject & components
The value of integer a after bitwise negation (~) is - (a+1)
Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
由两个栈组成的队列
如何仅用递归函数和栈操作逆序一个栈
P1047 [NOIP2005 普及组 T2] 校门外的树
Acnet: asymmetric convolution for image hypersegmentation (with implementation code)
文献学习(part101)--CONVEX BICLUSTERING