334 to reverse a string

1. Requirements: You can only modify the original array, not create a new space

2, the idea: I is the length of the odd and even sub-determination

3, code implementation

. 1  class Solution {
 2  public :
 . 3      void ReverseString (Vector < char > & S) {
 . 4      IF (! S.empty ()) {
 . 5          int n-= s.size ();
 . 6          IF (n-% 2 == 0 )
 . 7          {
 . 8              for ( int I = 0 ; I <= n-/ 2 - . 1 ; I ++) { // second correction, subtract attention. 1 
. 9                   char chartemp; // first written int, clearly character 
10                  chartemp=s[i];
11                 s[i]=s[n-1-i];
12                 s[n-1-i]=chartemp;
13             }
14             return ;
15         }
16         else{
17             int mid=n/2;
18             for(int i=0;i<mid;i++){
19                 char chartemp;
20                  chartemp=s[i];
21                 s[i]=s[n-1-i];
22                 s[n-1-i]=chartemp;
23             }
24             return ;
25         }
26     }
27     }
28 };

4 results

 

Guess you like

Origin www.cnblogs.com/hehesunshine/p/11577752.html