From ab4d42cf0c41ce9089838dee087e961aac1b6357 Mon Sep 17 00:00:00 2001 From: n4feng Date: Sat, 14 May 2022 12:52:11 -0400 Subject: [PATCH 01/14] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 少了一个关键词“高” --- problems/0739.每日温度.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 5f53e412..367e521a 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -34,7 +34,7 @@ 那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢? -单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素的元素,优点是只需要遍历一次。 +单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素高的元素,优点是只需要遍历一次。 在使用单调栈的时候首先要明确如下几点: From 78b367e5c5ee493a96cfe4ebcb440059909db285 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 15 May 2022 12:57:39 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=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.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index d03de421..0e25fc4d 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -758,7 +758,63 @@ func reverseWord(_ s: inout [Character]) { } ``` +Scala: +```scala +object Solution { + def reverseWords(s: String): String = { + var sb = removeSpace(s) // 移除多余的空格 + reverseString(sb, 0, sb.length - 1) // 翻转字符串 + reverseEachWord(sb) + sb.mkString + } + + // 移除多余的空格 + def removeSpace(s: String): Array[Char] = { + var start = 0 + var end = s.length - 1 + // 移除字符串前面的空格 + while (start < s.length && s(start) == ' ') start += 1 + // 移除字符串后面的空格 + while (end >= 0 && s(end) == ' ') end -= 1 + var sb = "" // String + // 当start小于等于end的时候,执行添加操作 + while (start <= end) { + var c = s(start) + // 当前字符不等于空,sb的最后一个字符不等于空的时候添加到sb + if (c != ' ' || sb(sb.length - 1) != ' ') { + sb ++= c.toString + } + start += 1 // 指针向右移动 + } + sb.toArray + } + + // 翻转字符串 + def reverseString(s: Array[Char], start: Int, end: Int): Unit = { + var (left, right) = (start, end) + while (left < right) { + var tmp = s(left) + s(left) = s(right) + s(right) = tmp + left += 1 + right -= 1 + } + } + + // 翻转每个单词 + def reverseEachWord(s: Array[Char]): Unit = { + var i = 0 + while (i < s.length) { + var j = i + 1 + // 向后迭代寻找每个单词的坐标 + while (j < s.length && s(j) != ' ') j += 1 + reverseString(s, i, j - 1) // 翻转每个单词 + i = j + 1 // i往后更新 + } + } +} +``` From c7d356f8559fd2c46eb4e9e2889fdbe0bdaafed1 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 15 May 2022 12:59:38 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Of?= =?UTF-8?q?fer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../剑指Offer58-II.左旋转字符串.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index fec83e1d..581ac1c7 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -290,7 +290,34 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { } ``` +Scala: +```scala +object Solution { + def reverseLeftWords(s: String, n: Int): String = { + var str = s.toCharArray // 转换为Array + // abcdefg => ba cdefg + reverseString(str, 0, n - 1) + // ba cdefg => ba gfedc + reverseString(str, n, str.length - 1) + // ba gfedc => cdefgab + reverseString(str, 0, str.length - 1) + // 最终返回,return关键字可以省略 + new String(str) + } + // 翻转字符串 + def reverseString(s: Array[Char], start: Int, end: Int): Unit = { + var (left, right) = (start, end) + while (left < right) { + var tmp = s(left) + s(left) = s(right) + s(right) = tmp + left += 1 + right -= 1 + } + } +} +``` From f1061d0f8962fe8e4a4c0691d8401ee5fe505f99 Mon Sep 17 00:00:00 2001 From: GitHubQAQ <31883473+GitHubQAQ@users.noreply.github.com> Date: Sun, 15 May 2022 19:12:13 +0800 Subject: [PATCH 04/14] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4.md=20=20-=20-=E8=A7=84=E8=8C=83C++=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E7=9A=84lambda=E8=A1=A8=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index b44d602c..52a98d70 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -96,7 +96,7 @@ public: vector> merge(vector>& intervals) { vector> result; if (intervals.size() == 0) return result; - // 排序的参数使用了lamda表达式 + // 排序的参数使用了lambda表达式 sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b){return a[0] < b[0];}); result.push_back(intervals[0]); From 224b3d293b6b2c9465e00b2127cf1d75f44775df Mon Sep 17 00:00:00 2001 From: wangning Date: Sun, 15 May 2022 20:15:11 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A0404=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8Cjavascript=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index d7fd629e..4ef68d3b 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -336,8 +336,8 @@ var sumOfLeftLeaves = function(root) { if(node===null){ return 0; } - let leftValue = sumOfLeftLeaves(node.left); - let rightValue = sumOfLeftLeaves(node.right); + let leftValue = nodesSum(node.left); + let rightValue = nodesSum(node.right); // 3. 单层递归逻辑 let midValue = 0; if(node.left&&node.left.left===null&&node.left.right===null){ From 14afd23b9ea5e491a60d91f07c0b8211f4f9ae00 Mon Sep 17 00:00:00 2001 From: HanMengnan <1448189829@qq.com> Date: Mon, 16 May 2022 10:46:43 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E6=9B=B4=E6=96=B00516.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=E7=9A=84Go?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=8F=90=E4=BE=9B=E4=BA=86=E4=B8=80=E7=A7=8D?= =?UTF-8?q?=E6=9B=B4=E4=BC=98=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0516.最长回文子序列.md | 39 +++++++++++++------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index 69536cef..63120b14 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -186,29 +186,28 @@ class Solution: Go: ```Go func longestPalindromeSubseq(s string) int { - lenth:=len(s) - dp:=make([][]int,lenth) - for i:=0;i b { + return a + } + return b + } + dp := make([][]int, size) + for i := 0; i < size; i++ { + dp[i] = make([]int, size) + dp[i][i] = 1 + } + for i := size - 1; i >= 0; i-- { + for j := i + 1; j < size; j++ { + if s[i] == s[j] { + dp[i][j] = dp[i+1][j-1] + 2 + } else { + dp[i][j] = max(dp[i][j-1], dp[i+1][j]) } } } - for i:=lenth-1;i>=0;i--{ - for j:=i+1;j Date: Mon, 16 May 2022 14:51:05 +0900 Subject: [PATCH 07/14] chore: Add new solution to LeetCode 203 Without using pre Node --- problems/0203.移除链表元素.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index c34831b7..9cedfe93 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -234,6 +234,27 @@ public ListNode removeElements(ListNode head, int val) { } return head; } +/** + * 不添加虚拟节点and pre Node方式 + * 时间复杂度 O(n) + * 空间复杂度 O(1) + * @param head + * @param val + * @return + */ +public ListNode removeElements(ListNode head, int val) { + while(head!=null && head.val==val){ + head = head.next; + } + ListNode curr = head; + while(curr!=null){ + while(curr.next!=null && curr.next.val == val){ + curr.next = curr.next.next; + } + curr = curr.next; + } + return head; +} ``` Python: From 08147d132c4611e190d03a47bc870294d3cc1292 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 16 May 2022 14:28:02 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200232.=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md=20Scala?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1a56d9f3..d9ba8e26 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) { obj->stackOutTop = 0; } ``` +Scala: +```scala +class MyQueue() { + import scala.collection.mutable + val stackIn = mutable.Stack[Int]() // 负责出栈 + val stackOut = mutable.Stack[Int]() // 负责入栈 + // 添加元素 + def push(x: Int) { + stackIn.push(x) + } + + // 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut + def dumpStackIn(): Unit = { + if (!stackOut.isEmpty) return + while (!stackIn.isEmpty) { + stackOut.push(stackIn.pop()) + } + } + + // 弹出元素 + def pop(): Int = { + dumpStackIn() + stackOut.pop() + } + + // 获取队头 + def peek(): Int = { + dumpStackIn() + val res: Int = stackOut.pop() + stackOut.push(res) + res + } + + // 判断是否为空 + def empty(): Boolean = { + stackIn.isEmpty && stackOut.isEmpty + } +} +``` -----------------------
From f4d98a744ee9c14ed88d07be559aa310508d89f9 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 16 May 2022 14:56:10 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20Scala?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 3457c4b3..3c134870 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -815,6 +815,89 @@ class MyStack { } } ``` +Scala: +使用两个队列模拟栈: +```scala +import scala.collection.mutable + +class MyStack() { + + val queue1 = new mutable.Queue[Int]() + val queue2 = new mutable.Queue[Int]() + + def push(x: Int) { + queue1.enqueue(x) + } + + def pop(): Int = { + var size = queue1.size + // 将queue1中的每个元素都移动到queue2 + for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + // 再将queue2中的每个元素都移动到queue1 + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + res + } + + def top(): Int = { + var size = queue1.size + for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + // 最终还需要把res送进queue1 + queue1.enqueue(res) + res + } + + def empty(): Boolean = { + queue1.isEmpty + } +} +``` +使用一个队列模拟: +```scala +import scala.collection.mutable + +class MyStack() { + + val queue = new mutable.Queue[Int]() + + def push(x: Int) { + queue.enqueue(x) + } + + def pop(): Int = { + var size = queue.size + for (i <- 0 until size - 1) { + queue.enqueue(queue.head) // 把头添到队列最后 + queue.dequeue() // 再出队 + } + queue.dequeue() + } + + def top(): Int = { + var size = queue.size + var res = 0 + for (i <- 0 until size) { + queue.enqueue(queue.head) // 把头添到队列最后 + res = queue.dequeue() // 再出队 + } + res + } + + def empty(): Boolean = { + queue.isEmpty + } +} +``` -----------------------
From 8df1b4b237552c189fa1a5788442c422c9296d07 Mon Sep 17 00:00:00 2001 From: madeai Date: Mon, 16 May 2022 16:38:53 +0800 Subject: [PATCH 10/14] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 5f53e412..2305d135 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -192,7 +192,7 @@ class Solution { 否则的话,可以直接入栈。 注意,单调栈里 加入的元素是 下标。 */ - Stackstack=new Stack<>(); + Deque stack=new LinkedList<>(); stack.push(0); for(int i=1;istack=new Stack<>(); + Deque stack=new LinkedList<>(); for(int i=0;itemperatures[stack.peek()]){ From 61f5d920d05ea01858272bc04f8bcea0a89d6991 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 16 May 2022 17:07:23 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 7bb7f746..a0df0d07 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -400,6 +400,27 @@ bool isValid(char * s){ return !stackTop; } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def isValid(s: String): Boolean = { + if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false + val stack = mutable.Stack[Char]() + // 循环遍历字符串 + for (i <- s.indices) { + val c = s(i) + if (c == '(' || c == '[' || c == '{') stack.push(c) + else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false + // 以下三种情况,不满足则直接返回false + else if(c==')' && stack.pop() != '(') return false + else if(c==']' && stack.pop() != '[') return false + else if(c=='}' && stack.pop() != '{') return false + } + // 如果为空则正确匹配,否则还有余孽就不匹配 + stack.isEmpty + } +} +``` -----------------------
From 98bdccbe16bbb6c8c66e027d901f4fbd3baafffe Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 16 May 2022 17:24:47 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md=20Sca?= =?UTF-8?q?la=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 638c8f4e..a92a3911 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -374,6 +374,27 @@ func removeDuplicates(_ s: String) -> String { return String(stack) } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def removeDuplicates(s: String): String = { + var stack = mutable.Stack[Int]() + var str = "" // 保存最终结果 + for (i <- s.indices) { + var tmp = s(i) + // 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素 + if (!stack.isEmpty && tmp == stack.head) { + str = str.take(str.length - 1) + stack.pop() + } else { + stack.push(tmp) + str += tmp + } + } + str + } +} +``` -----------------------
From 349383321ff1ed393effb9ebaad075a7736cf98e Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Tue, 17 May 2022 17:45:40 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0KMP=20php=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index d67e5f70..1cdd5292 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -1166,5 +1166,80 @@ func strStr(_ haystack: String, _ needle: String) -> Int { ``` +PHP: + +> 前缀表统一减一 +```php +function strStr($haystack, $needle) { + if (strlen($needle) == 0) return 0; + $next= []; + $this->getNext($next,$needle); + + $j = -1; + for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j >= 0 && $haystack[$i] != $needle[$j + 1]) { + $j = $next[$j]; + } + if ($haystack[$i] == $needle[$j + 1]) { + $j++; + } + if ($j == (strlen($needle) - 1) ) { + return ($i - strlen($needle) + 1); + } + } + return -1; +} + +function getNext(&$next, $s){ + $j = -1; + $next[0] = $j; + for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j >= 0 && $s[$i] != $s[$j + 1]) { + $j = $next[$j]; + } + if ($s[$i] == $s[$j + 1]) { + $j++; + } + $next[$i] = $j; + } +} +``` + +> 前缀表统一不减一 +```php +function strStr($haystack, $needle) { + if (strlen($needle) == 0) return 0; + $next= []; + $this->getNext($next,$needle); + + $j = 0; + for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j > 0 && $haystack[$i] != $needle[$j]) { + $j = $next[$j-1]; + } + if ($haystack[$i] == $needle[$j]) { + $j++; + } + if ($j == strlen($needle)) { + return ($i - strlen($needle) + 1); + } + } + return -1; +} + +function getNext(&$next, $s){ + $j = 0; + $next[0] = $j; + for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j > 0 && $s[$i] != $s[$j]) { + $j = $next[$j-1]; + } + if ($s[$i] == $s[$j]) { + $j++; + } + $next[$i] = $j; + } +} +``` -----------------------
From 0281b82d48a4f21429d35a89270843357f801c15 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Mon, 16 May 2022 15:26:21 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../剑指Offer58-II.左旋转字符串.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index fec83e1d..8781ffb4 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -290,6 +290,25 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { } ``` +### PHP + +```php +function reverseLeftWords($s, $n) { + $this->reverse($s,0,$n-1); //反转区间为前n的子串 + $this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串 + $this->reverse($s,0,strlen($s)-1); //反转整个字符串 + return $s; +} + +// 按指定进行翻转 【array、string都可】 +function reverse(&$s, $start, $end) { + for ($i = $start, $j = $end; $i < $j; $i++, $j--) { + $tmp = $s[$i]; + $s[$i] = $s[$j]; + $s[$j] = $tmp; + } +} +```