avatar

手写,不在怕的;轮子,没想到的。

📝题目

1
2
3
4
5
6
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:

输入:s = "We are happy."
输出:"We%20are%20happy."


📝题解

1
2
3
4
5
//轮子

string replaceSpace(string s) {
return s.replace(" ","%20");
}
1
2
3
4
5
6
7
8
9
10
11
//手写

string replaceSpace(string s) {
int len = s.size();
string result = "";
for (int i = 0; i < len; ++i){
if (s[i] == ' ') result += "%20";
else result += s[i];
}
return result;
}



📝题目

1
2
3
4
5
6
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]


📝题解

1
2
3
4
5
6
7
8
9
10
11
12
13
//轮子

vector<int> reversePrint(ListNode* head) {
vector<int> result;
ListNode* temp = head;
while (temp){
result.push_back(temp->val);
temp = temp->next;
}
//指的是下面这一步
reverse(result.begin(), result.end());
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//手写

vector<int> reversePrint(ListNode* head) {
vector<int> result, tmp;
ListNode* temp = head;
int len = 0;
while (temp){
tmp.push_back(temp->val);
++len;
temp = temp->next;
}
//指的是下面这一步
for (int i = 0; i < len; ++i){
result.push_back(tmp.back());
tmp.pop_back();
}
return result;
}
Author:WhiteBeerHouse
Link:https://github.com/WhiteBeerHouse/WhiteBeerHouse.github.io/tree/master/2020/04/25/%E6%89%8B%E5%86%99%EF%BC%8C%E4%B8%8D%E5%9C%A8%E6%80%95%E7%9A%84%EF%BC%9B%E8%BD%AE%E5%AD%90%EF%BC%8C%E6%B2%A1%E6%83%B3%E5%88%B0%E7%9A%84%E3%80%82/
Copyright Notice:All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.