From ceaaa2fd2d3114c75794b89fbb08005dd991b81c Mon Sep 17 00:00:00 2001 From: Epoch <75031971+messenger1th@users.noreply.github.com> Date: Mon, 21 Feb 2022 14:37:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E7=9A=84CPP=E6=9B=B4=E7=AE=80=E6=B4=81=E7=9A=84?= =?UTF-8?q?=E7=89=88=E6=9C=AC2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用LC27的原理使得更为简洁。 --- problems/0151.翻转字符串里的单词.md | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index ead5fa12..677a8f64 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -222,7 +222,42 @@ public: 效率: +```CPP +//版本二: +class Solution { +public: + void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。 + for(int i=start;i<(end-start)/2+start;++i){ + swap(s[i],s[end-1-i+start]); + } + } + void trim(string& s){//去除所有空格并在相邻单词之间添加空格 + int slow = 0; + for(int i=0;i Date: Mon, 14 Mar 2022 13:33:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20?= =?UTF-8?q?=E5=90=8C=E7=90=86CPP=20=E7=89=88=E6=9C=AC2=E7=AE=80=E6=B4=81?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 规范代码,优化留白。 同时添加详细注解, 并给出同理题目练习链接。 --- problems/0151.翻转字符串里的单词.md | 36 +++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 677a8f64..e7abd1d8 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -224,34 +224,36 @@ public: ```CPP //版本二: +//原理同版本1,更简洁实现。 class Solution { public: - void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。 - for(int i=start;i<(end-start)/2+start;++i){ - swap(s[i],s[end-1-i+start]); + void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 [] + for (int i = start, j = end; i < j; i++, j--) { + swap(s[i], s[j]); } } - void trim(string& s){//去除所有空格并在相邻单词之间添加空格 - int slow = 0; - for(int i=0;i