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

Commit40960d2

Browse files
committed
0344: Reverse String
1 parent0bcd899 commit40960d2

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

‎0344-Reverse_String/main.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ function reverseStringUsingTempVariable(s) {
2222
right--;
2323
}
2424
}
25-
reverseStringUsingDestructuring(strArray1);
26-
reverseStringUsingTempVariable(strArray2);
27-
console.log(strArray1);
28-
console.log(strArray2);
25+
letstrArrayForDestructuring=[...strArray1];
26+
console.time("reverseStringUsingDestructuring");
27+
reverseStringUsingDestructuring(strArrayForDestructuring);
28+
console.timeEnd("reverseStringUsingDestructuring");
29+
letstrArrayForTempVariable=[...strArray1];
30+
console.time("reverseStringUsingTempVariable");
31+
reverseStringUsingTempVariable(strArrayForTempVariable);
32+
console.timeEnd("reverseStringUsingTempVariable");
33+
console.log(strArrayForDestructuring);
34+
console.log(strArrayForTempVariable);

‎0344-Reverse_String/main.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,38 @@ let strArray1 = ["h", "e", "l", "l", "o"]
22
letstrArray2=["H","a","n","n","a","h"]
33

44
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-
}
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+
}
1212
}
1313

1414
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-
}
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+
}
2626
}
2727

28-
reverseStringUsingDestructuring(strArray1)
29-
reverseStringUsingTempVariable(strArray2)
28+
letstrArrayForDestructuring=[...strArray1]
29+
console.time("reverseStringUsingDestructuring")
30+
reverseStringUsingDestructuring(strArrayForDestructuring)
31+
console.timeEnd("reverseStringUsingDestructuring")
3032

31-
console.log(strArray1)
32-
console.log(strArray2)
33+
letstrArrayForTempVariable=[...strArray1]
34+
console.time("reverseStringUsingTempVariable")
35+
reverseStringUsingTempVariable(strArrayForTempVariable)
36+
console.timeEnd("reverseStringUsingTempVariable")
37+
38+
console.log(strArrayForDestructuring)
39+
console.log(strArrayForTempVariable)

‎0344-Reverse_String/readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
Write a function that reverses a string. The input string is given as an array of characterss.
1+
Write a function that reverses a string. The input string is given as an array of characters`s`.
22

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.
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.
44

55
Example 1:
66

7-
>Input: s =["h","e","l","l","o"]<br>
8-
>Output:["o","l","l","e","h"]
7+
><spanstyle="color:white;">Input</span>: s =["h","e","l","l","o"]<br>
8+
><spanstyle="color:white;">Output</span>:["o","l","l","e","h"]
99
1010
Example 2:
1111

12-
>Input: s =["H","a","n","n","a","h"]<br>
13-
>Output:["h","a","n","n","a","H"]
12+
><spanstyle="color:white;">Input</span>: s =["H","a","n","n","a","h"]<br>
13+
><spanstyle="color:white;">Output</span>:["h","a","n","n","a","H"]
1414
1515
Constraints:
1616

17-
- 1 <= s.length <=105
18-
- s[i] is a[printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
17+
- 1 <= s.length <=10<sup>5</sup>
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