天天看点

CareerCup之1.2C风格字符串翻转

原文:

write code to reverse a c-style string. (c-string means that “abcd” is represented as five characters, including the null character.)

译文:

写代码翻转一个c风格的字符串。(c风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符)

这是一道经典的面试题目,虽然看似简单,但仍然有陷阱。唯一的陷阱就是空间复杂度为o(1)即原地置换,要特别注意null字符。

交换的进一步优化,使用异或进行交换:

继续阅读