7-week2

1.替换空格

 题解:

可以直接用java的替换函数

class Solution {
    public String replaceSpace(String s) {
        return s.replace(" ", "%20") ;
    }
}

或者c++遍历原字符串,遇到空格就替换

class Solution {
public:
    string replaceSpace(string s) {
        int n=s.size();
        string str="";
        for(int i=0;i<n;++i)
            str=(s[i]==' ' ? str+"%20" : str+s[i]);
        return str;
    }
};

2.从尾到头打印链表

题解:用递归从头遍历到尾,然后逆序把数据塞进动态数组里

class Solution {
public:
   vector<int>v1;
    vector<int> reversePrint(ListNode* head) {
        if(head!=NULL){
            reversePrint(head->next);
            v1.push_back(head->val);
        } 
        return v1;
    }
};

猜你喜欢

转载自blog.csdn.net/sign_river/article/details/132008714