Day8-String01
344. Reverse String
Node:
- Two pointers
- Reverse inplace
The reverse() method of Array instances reverses an array in place and returns the reference to the same array
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
1 | var reverseString = function(s) { |
541. Reverse String II
Node:
can’t change the string’s char by
string1[0] = "c"
, it doesn’t workIn JavaScript, strings are immutable, which means once a string is created, its characters cannot be changed directly.
- String:
let str = "hello"; str = str + " world"
; (This creates a new string rather than modifying the original str). - Numbers: Numbers are also immutable. Any operation on a number returns a new number rather than modifying the original value.
- Booleans: true and false are immutable.
- null and undefined: These are immutable by nature, as they represent the absence of value.
- Symbols: Introduced in ES6, symbols are unique and immutable. Once created, a symbol’s value cannot be changed.
(All primitive type are immutable, You cannot change a primitive’s value. Any operation that appears to change a primitive actually creates a new primitive.)
- String:
Convert to a array and then change it by
arr[0] = "c"
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
1 | var reverseStr = function(s, k) { |
- Title: Day8-String01
- Author: Guoyi
- Created at : 2024-08-30 23:09:25
- Updated at : 2024-12-07 03:58:41
- Link: https://guoyiwang.github.io/Algorithm/Day8-String01/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments