剑指Offer05、06|8月更文挑战

89 阅读1分钟

剑指 Offer 05. 替换空格

题目

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:

输入:s = "We are happy."
输出:"We%20are%20happy."

限制:

0 <= s 的长度 <= 10000

方法一

模拟即可:遍历字符串,遇到空格替换成%20

class Solution {
    public String replaceSpace(String s) {
        StringBuilder res = new StringBuilder();
        for (Character  c : s.toCharArray()) 
            if (c != ' ') res.append(c);
            else res.append("%20");
        return res.toString();
    }
}

时间复杂度: O(n)

空间复杂度: O(n)

注意: 在java中String是不可变的,所以无法在原有的参数s中进行修改,只能创建新的对象。另外,对于字符串的拼接,StringBuilder比String会好一些。

方法二

原地修改:C++中可以对参数s进行原地修改。首先遍历一个字符串统计空格的数量,接着重新声明字符串的大小,最后从后往前遍历修改字符串。从后往前遍历的原因是当遇到原字符串中的非空字符时,在此之前该字符已经被遍历过,所以覆盖即可。

class Solution {
public:
    string replaceSpace(string s) {
        int cnt = 0, n = s.size();
        for (char c : s)
            if (c == ' ')
                cnt ++;
​
        s.resize(n + 2 * cnt);
        for (int i = n - 1, j = s.size() - 1; i >= 0; i -- ) {
            if (s[i] != ' ') s[j --] = s[i];
            else {
                s[j --] = '0';
                s[j --] = '2';
                s[j --] = '%';
            }
        }
        return s;
    }
};

时间复杂度: O(n)

空间复杂度: O(1)

剑指 Offer 06. 从尾到头打印链表

题目

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

限制:

0 <= 链表长度 <= 10000

方法一

递归:根据题意,后面的节点要先出来,那么自然的想到了递归和栈的方法;当还有后继节点时,先遍历后继节点,遍历完再将该点的值加入到集合中。最后将集合从前往后的顺序包装到数组中返回即可;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    ArrayList<Integer> temp = new ArrayList<Integer>();
    public int[] reversePrint(ListNode head) {
        dfs(head);
        int[] res = new int[temp.size()];
        for (int i = 0; i < temp.size(); i ++ )
            res[i] = temp.get(i);
        return res;
    }
    void dfs(ListNode root) {
        if (root == null) return ;
        dfs(root.next);
        temp.add(root.val);
    }
}

时间复杂度: O(n)

空间复杂度: O(n)

方法二

辅助栈:思路与递归一样,递归是用系统栈实现;这里是我们自己开一个辅助栈来实现;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        Stack<Integer> stack = new Stack<Integer>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int[] res = new int[stack.size()];
        for (int i = 0; i < res.length; i ++ ) {
            res[i] = stack.peek();
            stack.pop();
        }
        return res;
    }
}

时间复杂度: O(n)

空间复杂度: O(n)

方法三

反转链表:将链表的next指针反转;再从原来的尾节点,也就是翻转后的头结点,根据next指针的方向遍历一遍即可;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        if (head == null) return new int[0];
        ListNode a = head, b = head.next;
        int cnt = 0;
        while (b != null) {
            ListNode c = b.next;
            b.next = a;
            a = b;
            b = c;
            cnt ++;
        }
        head.next = null;
        
        int[] res = new int[cnt + 1];
        for (int i = 0; i < res.length; i ++ ) {
            res[i] = a.val;
            a = a.next;
        }
        return res;
    }
}

时间复杂度: O(n)

空间复杂度: O(1)