leetcode344

来源:互联网 发布:three.js 720度全景 编辑:程序博客网 时间:2024/04/29 17:06
  1. Reverse String
    Write a function that takes a string as input and returns the string reversed.
    Example:
    Given s = “hello”, return “olleh”.
    就是把一个字符串倒过来输出啦
class Solution {public:    string reverseString(string s) {        int i = 0, j = s.size() - 1;        while(i < j){            swap(s[i++], s[j--]);         }        return s;    }};

反正思路都一样~两头换一换

class Solution {public:    string reverseString(string s) {        char temp;        int length=s.length();        for(int i = 0; i < length/2; i++){            temp = s[length-1-i];            s[length-1-i] = s[i];            s[i] = temp;        }        return s;    }};

1、void swap (T& a, T& b):Exchanges the values of a and b.

0 0
原创粉丝点击