Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: 【0055右旋字符串】JavaScript部分实现 #2547

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions problems/kamacoder/0055.右旋字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,37 @@ func main(){


### JavaScript:
```javascript
// JS中字符串内不可单独修改

// 右旋转
function reverseLeftWords(s, k) {
const reverse = (sList, start, end) => {
for (let i = start, j = end; i < j; i++, j--) {
[sList[i], sList[j]] = [sList[j], sList[i]];
}
}
const sList = Array.from(s);
reverse(sList, 0, sList.length - k - 1);
reverse(sList, sList.length - k, sList.length - 1);
reverse(sList, 0, sList.length - 1);
return sList.join('');
}

// 左旋转
var reverseLeftWords = function(s, n) {
const reverse = (sList, start, end) => {
for (let i = start, j = end; i < j; i++, j--) {
[sList[i], sList[j]] = [sList[j], sList[i]];
}
}
const sList = s.split('');
reverse(sList, 0, n - 1);
reverse(sList, n, sList.length - 1);
reverse(sList, 0, sList.length - 1);
return sList.join('');
};
```

### TypeScript:

Expand Down