Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
60 lines (42 sloc)
1.43 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ReverseString.swift | |
// LeetCode.swift | |
// | |
// Created by 叶帆 on 2020/3/7. | |
// Copyright © 2020 Suzhou Coryphaei Information&Technology Co., Ltd. All rights reserved. | |
// | |
/** | |
Write a function that reverses a string. The input string is given as an array of characters char[]. | |
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. | |
You may assume all the characters consist of printable ascii characters. | |
Example 1: | |
Input: ["h","e","l","l","o"] | |
Output: ["o","l","l","e","h"] | |
Example 2: | |
Input: ["H","a","n","n","a","h"] | |
Output: ["h","a","n","n","a","H"] | |
===== | |
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 | |
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 | |
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 | |
示例 1: | |
输入:["h","e","l","l","o"] | |
输出:["o","l","l","e","h"] | |
示例 2: | |
输入:["H","a","n","n","a","h"] | |
输出:["h","a","n","n","a","H"] | |
*/ | |
import Foundation | |
class Solution { | |
func reverseString(_ s: inout [Character]) { | |
// 为了实现 reverse() 函数 | |
var i = 0 | |
var j = s.count - 1 | |
while i < j { | |
s.swapAt(i, j) | |
i += 1 | |
j -= 1 | |
} | |
} | |
} |