Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0bcd899

Browse files
authored
0344: Reverse String (#1)
1 parent56d14cd commit0bcd899

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

‎0344-Reverse_String/main.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
letstrArray1=["h","e","l","l","o"];
2+
letstrArray2=["H","a","n","n","a","h"];
3+
functionreverseStringUsingDestructuring(s){
4+
letleft=0;
5+
letright=s.length-1;
6+
while(left<right){
7+
;
8+
[s[left],s[right]]=[s[right],s[left]];
9+
left++;
10+
right--;
11+
}
12+
}
13+
functionreverseStringUsingTempVariable(s){
14+
letlength=s.length;
15+
letleft=0,right=length-1;
16+
lettempStr;
17+
while(left<right){
18+
tempStr=s[left];
19+
s[left]=s[right];
20+
s[right]=tempStr;
21+
left++;
22+
right--;
23+
}
24+
}
25+
reverseStringUsingDestructuring(strArray1);
26+
reverseStringUsingTempVariable(strArray2);
27+
console.log(strArray1);
28+
console.log(strArray2);

‎0344-Reverse_String/main.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
letstrArray1=["h","e","l","l","o"]
2+
letstrArray2=["H","a","n","n","a","h"]
3+
4+
functionreverseStringUsingDestructuring(s:string[]):void{
5+
letleft=0
6+
letright=s.length-1
7+
while(left<right){
8+
;[s[left],s[right]]=[s[right],s[left]]
9+
left++
10+
right--
11+
}
12+
}
13+
14+
functionreverseStringUsingTempVariable(s:string[]):void{
15+
letlength:number=s.length
16+
letleft:number=0,
17+
right:number=length-1
18+
lettempStr:string
19+
while(left<right){
20+
tempStr=s[left]
21+
s[left]=s[right]
22+
s[right]=tempStr
23+
left++
24+
right--
25+
}
26+
}
27+
28+
reverseStringUsingDestructuring(strArray1)
29+
reverseStringUsingTempVariable(strArray2)
30+
31+
console.log(strArray1)
32+
console.log(strArray2)

‎0344-Reverse_String/readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Write a function that reverses a string. The input string is given as an array of characters s.
2+
3+
You must do this by modifying the input array[in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with O(1) extra memory.
4+
5+
Example 1:
6+
7+
>Input: s =["h","e","l","l","o"]<br>
8+
>Output:["o","l","l","e","h"]
9+
10+
Example 2:
11+
12+
>Input: s =["H","a","n","n","a","h"]<br>
13+
>Output:["h","a","n","n","a","H"]
14+
15+
Constraints:
16+
17+
- 1 <= s.length <= 105
18+
- s[i] is a[printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp