diff --git a/Algorithms/344. Reverse String/344. Reverse String.go b/Algorithms/344. Reverse String/344. Reverse String.go new file mode 100644 index 00000000..e809364a --- /dev/null +++ b/Algorithms/344. Reverse String/344. Reverse String.go @@ -0,0 +1,9 @@ +package leetcode + +func reverseString(s []byte) { + for i, j := 0, len(s)-1; i < j; { + s[i], s[j] = s[j], s[i] + i++ + j-- + } +} diff --git a/Algorithms/344. Reverse String/344. Reverse String_test.go b/Algorithms/344. Reverse String/344. Reverse String_test.go new file mode 100644 index 00000000..71f4f554 --- /dev/null +++ b/Algorithms/344. Reverse String/344. Reverse String_test.go @@ -0,0 +1,49 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question344 struct { + para344 + ans344 +} + +// para 是参数 +// one 代表第一个参数 +type para344 struct { + one []byte +} + +// ans 是答案 +// one 代表第一个答案 +type ans344 struct { + one []byte +} + +func Test_Problem344(t *testing.T) { + + qs := []question344{ + + question344{ + para344{[]byte{'h', 'e', 'l', 'l', 'o'}}, + ans344{[]byte{'o', 'l', 'l', 'e', 'h'}}, + }, + + question344{ + para344{[]byte{'H', 'a', 'n', 'n', 'a', 'h'}}, + ans344{[]byte{'h', 'a', 'n', 'n', 'a', 'H'}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 344------------------------\n") + + for _, q := range qs { + _, p := q.ans344, q.para344 + fmt.Printf("【input】:%v ", p.one) + reverseString(p.one) + fmt.Printf("【output】:%v\n", p.one) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/344. Reverse String/README.md b/Algorithms/344. Reverse String/README.md new file mode 100644 index 00000000..1095c730 --- /dev/null +++ b/Algorithms/344. Reverse String/README.md @@ -0,0 +1,30 @@ +# [344. Reverse String](https://leetcode.com/problems/reverse-string/) + +## 题目 + +Write a function that reverses a string. The input string is given as an array of characters char[]. + +Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. + +You may assume all the characters consist of printable ascii characters. + +Example 1: + +```c +Input: ["h","e","l","l","o"] +Output: ["o","l","l","e","h"] +``` + +Example 2: + +```c +Input: ["H","a","n","n","a","h"] +Output: ["h","a","n","n","a","H"] +``` + +## 题目大意 + +题目要求我们反转一个字符串。 + +这一题的解题思路是用 2 个指针,指针对撞的思路,来不断交换首尾元素,即可。 +