From 20aa3a2454e955e17f37ecd2e22a56451e44fab4 Mon Sep 17 00:00:00 2001 From: gpfpter <67033145+gpfpter@users.noreply.github.com> Date: Fri, 21 May 2021 16:27:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=2058=20-=20II.=20?= =?UTF-8?q?=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20Java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 剑指 Offer 58 - II. 左旋转字符串 Java代码提交,与作者思路相同的三次反转。 --- .../剑指Offer58-II.左旋转字符串.md | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 3e9ab11f..9bd639aa 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -96,10 +96,27 @@ public: ## 其他语言版本 - Java: - - +```java +class Solution { + public String reverseLeftWords(String s, int n) { + int len=s.length(); + StringBuilder sb=new StringBuilder(s); + reverseString(sb,0,n-1); + reverseString(sb,n,len-1); + return sb.reverse().toString(); + } + public void reverseString(StringBuilder sb, int start, int end) { + while (start < end) { + char temp = sb.charAt(start); + sb.setCharAt(start, sb.charAt(end)); + sb.setCharAt(end, temp); + start++; + end--; + } + } +} +``` Python: