swap

Finding the minimum number of swaps to convert one string to another, where the strings may have repeated characters

感情迁移 提交于 2019-12-28 05:22:05
问题 I was looking through a programming question, when the following question suddenly seemed related. How do you convert a string to another string using as few swaps as follows. The strings are guaranteed to be interconvertible (they have the same set of characters, this is given), but the characters can be repeated . I saw web results on the same question, without the characters being repeated though. Any two characters in the string can be swapped. For instance : "aabbccdd" can be converted

Potential Problem in “Swapping values of two variables without using a third variable”

天涯浪子 提交于 2019-12-28 04:12:46
问题 I recently came along this method for swapping the values of two variables without using a third variable. a^=b^=a^=b But when I tried the above code on different compilers, I got different results, some gave correct results, some didn't. Is anything terribly wrong with the code? 回答1: Is anything terribly wrong with the code? Yes! a^=b^=a^=b in fact invokes Undefined Behaviour in C and in C++ because you are trying to change the value of a more than once between two sequence points. Try

Why does swapping values with XOR fail when using this compound form?

混江龙づ霸主 提交于 2019-12-28 03:31:11
问题 I found this code to swap two numbers without using a third variable, using the XOR ^ operator. Code: int i = 25; int j = 36; j ^= i; i ^= j; j ^= i; Console.WriteLine("i:" + i + " j:" + j); //numbers Swapped correctly //Output: i:36 j:25 Now I changed the above code to this equivalent code. My Code: int i = 25; int j = 36; j ^= i ^= j ^= i; // I have changed to this equivalent (???). Console.WriteLine("i:" + i + " j:" + j); //Not Swapped correctly //Output: i:36 j:0 Now, I want to know, Why

Swap two variables without using a temporary variable

删除回忆录丶 提交于 2019-12-27 10:37:42
问题 I'd like to be able to swap two variables without the use of a temporary variable in C#. Can this be done? decimal startAngle = Convert.ToDecimal(159.9); decimal stopAngle = Convert.ToDecimal(355.87); // Swap each: // startAngle becomes: 355.87 // stopAngle becomes: 159.9 回答1: First of all, swapping without a temporary variable in a language as C# is a very bad idea . But for the sake of answer, you can use this code: startAngle = startAngle + stopAngle; stopAngle = startAngle - stopAngle;

Define a preprocessor macro swap(t, x, y)

帅比萌擦擦* 提交于 2019-12-25 21:01:53
问题 I need to define a preprocessor macro swap(t, x, y) that will swap two arguments x and y of a given type t in C/C++.Can anyone have any opinion on how can i do it? 回答1: If you want to swap basic types like int or char (which implement the XOR operator) you can use the tripple XOR trick to swap the values without the need of an additional variable: #define SWAP(a, b) \ { \ (a) ^= (b); \ (b) ^= (a); \ (a) ^= (b); \ } If you're swapping complex types (float, structs, ...) you need a helper

Define a preprocessor macro swap(t, x, y)

非 Y 不嫁゛ 提交于 2019-12-25 21:00:12
问题 I need to define a preprocessor macro swap(t, x, y) that will swap two arguments x and y of a given type t in C/C++.Can anyone have any opinion on how can i do it? 回答1: If you want to swap basic types like int or char (which implement the XOR operator) you can use the tripple XOR trick to swap the values without the need of an additional variable: #define SWAP(a, b) \ { \ (a) ^= (b); \ (b) ^= (a); \ (a) ^= (b); \ } If you're swapping complex types (float, structs, ...) you need a helper

How to swap maximums with the minimums? (python)

 ̄綄美尐妖づ 提交于 2019-12-25 18:55:18
问题 Is there a method to swap the maximum and the minimum of a list? The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second maximum swapped with the second minimum and third maximum swapped with the third minimum. Eg. Enter input- 0 1 2 3 4 5 6 7 8 9 -1 Output- 9873456210 a = [] nums = raw_input("Enter input- ") for n in nums.split(): n = int(n) if n < 0: break a.append(n) if len(a)<7: print "please enter more than 7

Swapping two rows in a matrix

橙三吉。 提交于 2019-12-25 17:08:50
问题 I am having a problem in swapping two rows in a matrix that is a 2D-dynamic array. I wanted to know if there is a function to use directly or if there is none I would like to know how to make one. Thanks in advance. Here is how I made the dynamic array: int **ptrMatrix = new int*[row]; for (int i = 0; i < row; i++) ptrMatrix[i] = new int[column]; 回答1: I found the solution ... it is done simply by making a temporary variable (X) and appling the following code for (int i=0;i<columns;i++) { X

How to pass the objects of two arraylists to one another at a specified index

六眼飞鱼酱① 提交于 2019-12-25 12:48:06
问题 So, I am trying to print out my arraylists in order of the objects area. I cannot however seem to figure out how to pass the values of objects to one another at an index. (I must do it recursively). Here is my code thusfar private static void recursionSort(ArrayList<GeometricObject> data) { if(data.size() <= 1) return; // Base case: just 1 elt ArrayList<GeometricObject> a = new ArrayList<GeometricObject>(data.size() / 2); ArrayList<GeometricObject> b = new ArrayList<GeometricObject>(data.size

Swap two pointers using XOR

别来无恙 提交于 2019-12-25 07:26:30
问题 I have a quick question about using XOR two swap two string literals. so I have the following: #include <stdio.h> #include <stdlib.h> #include <string.h> void intSwap(int *a, int *b){ *a=*a^*b; *b=*a^*b; *a=*a^*b; } void swapString(char **a, char **b){ char *temp=*a; *a=*b; *b=temp; } void main(){ char *s= "ha"; char *t= "Oh"; printf("%s and %s \n",s,t); // prints ha Oh swapString(&s,&t); printf("%s and %s \n",s,t); // prints Oh ha int a=10; int b=5; printf("%d %d\n",a,b); //print 10 5