[GeeksForGeeks] Reverse a string without using any temporary variables

Given a string, reverse it without using any temporary variables.

This problem is a variation of the problem swapping two integers without using any temporary variables by XOR.

For integers x and y, to swap them using XOR, we do the following.

x = x ^ y;

y = y ^ x;

x = x ^ y;

Proof of correctness.

y = y ^ (x ^ y) = (y ^ y) ^ x = 0 ^ x = x;

x = (x ^ y) ^ x = (x ^ x) ^ y = 0 ^ y = y;

QED.

Apply this 3 steps XOR operations to each pair of characters that need to be swapped.

 1 public class Solution {
 2     public char[] reverseString(char[] str, int start, int end){
 3         while(start < end){
 4             str[start] ^= str[end];
 5             str[end] ^= str[start];
 6             str[start] ^= str[end];
 7             start++;
 8             end--;
 9         }
10         return str;
11     }
12 }
时间: 2024-10-21 06:12:55

[GeeksForGeeks] Reverse a string without using any temporary variables的相关文章

reverse the string word by word

题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 要求: 1)首尾有空格的时候,反转后的string要将空格去掉 2)当string有多个连续空格的时候,只保留一个空格. 代码分析: 对多余空格剔除: 思路分析: 1)从原始s 的最末尾开始扫描,如果遇到空格,用whil

Reverse a String

翻转字符串 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: Global String Object String.split() Array.reverse() Array.join() function reverseString(str) { // 请把你的代码写在这里 str = str.split("").

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number

一:Reverse Integer 题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 链接:https://leetcode.com/problems/reverse-integer/ 分析:这题通过不断取余将余数存放在一个vector中,然后乘以相应的10^i次方相加即可,这里主要考虑是否overflow,因此将result设为long long int

FCC JS基础算法题(0):Reverse a String(翻转字符串)

题目描述: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串.你的结果必须得是一个字符串. 算法: function reverseString(str) { // 请把你的代码写在这里 str = str.split("").reverse().join(""); return str; } reverseString("hello"); 原文地址:https://www.cnblogs.com/hume

186. Reverse Words in a String II

https://leetcode.com/problems/reverse-words-in-a-string-ii/#/description Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and

Leetcode: Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input

Reverse Words in a String

Problem: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 这个题目叙述的不够精确,有些边界的情况需要考虑.题目的下方给出了Clarification澄清了几个问题,也是面试的时候需要确认的. 第一个问题是什么构成了一个词(word),回答是一串不包含空格(non

Lintcode53 Reverse Words in a String solution 题解

[题目描述] Given an input string, reverse the string word by word. 给定一个字符串,逐个翻转字符串中的每个单词. [题目链接] http://www.lintcode.com/en/problem/reverse-words-in-a-string/ [题目解析] 这道题让我们翻转字符串中的单词,题目中给了我们写特别说明,如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),所以我们只能对