diff --git "a/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" "b/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" index cc4ab00cdc..703e891311 100644 --- "a/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" +++ "b/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" @@ -40,6 +40,8 @@ exection -> execution (插入 'u') * 0 <= word1.length, word2.length <= 500 * word1 和 word2 由小写英文字母组成 +# 算法公开课 +**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" index 4474f1c613..6dd3cd4975 100644 --- "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" +++ "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" @@ -467,9 +467,57 @@ class Solution: return " ".join(words) ``` - Go: +版本一: + +```go +func reverseWords(s string) string { + b := []byte(s) + + // 移除前面、中间、后面存在的多余空格 + slow := 0 + for i := 0; i < len(b); i++ { + if b[i] != ' ' { + if slow != 0 { + b[slow] = ' ' + slow++ + } + for i < len(b) && b[i] != ' ' { // 复制逻辑 + b[slow] = b[i] + slow++ + i++ + } + } + } + b = b[0:slow] + + // 翻转整个字符串 + reverse(b) + // 翻转每个单词 + last := 0 + for i := 0; i <= len(b); i++ { + if i == len(b) || b[i] == ' ' { + reverse(b[last:i]) + last = i + 1 + } + } + return string(b) +} + +func reverse(b []byte) { + left := 0 + right := len(b) - 1 + for left < right { + b[left], b[right] = b[right], b[left] + left++ + right-- + } +} +``` + +版本二: + ```go import ( "fmt"