Monthly Archives: July 2020

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

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

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

示例 1:

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

限制:

0 <= 链表长度 <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


给定一个单链表,从尾到头打印链表的值。先顺序遍历链表,将结果存到一个数组中,然后逆序数组。代码如下:

class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        if(head == NULL) return {};
        vector<int> ans;
        while(head != NULL) {
            ans.push_back(head->val);
            head = head->next;
        }
        int i = 0, j = ans.size() - 1;
        while(i < j) {
            swap(ans[i++], ans[j--]);
        }
        return ans;
    }
};

本代码提交AC,用时4MS。

剑指 Offer 05. 替换空格

剑指 Offer 05. 替换空格

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

示例 1:

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

限制:

0 <= s 的长度 <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


直接暴力替换即可,代码如下:

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

本代码提交AC,用时0MS。

剑指 Offer 04. 二维数组中的查找

剑指 Offer 04. 二维数组中的查找

在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

示例:

现有矩阵 matrix 如下:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。

给定 target = 20,返回 false。

限制:

0 <= n <= 1000

0 <= m <= 1000

注意:本题与主站 240 题相同:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


给定一个矩阵,每行从左到右升序排列,每列从上到下升序排列,给定一个数,问这个数在不在矩阵中。

从矩阵右上角开始查找,代码如下:

class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
        if(matrix.empty() || matrix[0].empty()) return false;
        int n = matrix.size(), m = matrix[0].size();
        int i = 0, j = m - 1;
        while(i < n && j >= 0) {
            if(matrix[i][j] == target) return true;
            if(target > matrix[i][j]) ++i;
            else --j;
        }
        return false;
    }
};

本代码提交AC,用时64MS。

剑指 Offer 03. 数组中重复的数字

剑指 Offer 03. 数组中重复的数字

找出数组中重复的数字。

在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

示例 1:

输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
 

限制:

2 <= n <= 100000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


查找数组中的重复元素。直接用数组hash,代码如下:

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        int n = nums.size();
        vector<int> hash(n, 0);
        for(int i = 0; i < n; ++i) {
            ++hash[nums[i]];
            if(hash[nums[i]] > 1) {
                return nums[i];
            }
        }
        return 0;
    }
};

本代码提交AC,用时84MS。

二刷。这题有陷阱,请注意和Find the Duplicate Number不一样!Leetcode英文题中,数组长度是n+1,数的范围是[1,n],也就是说根据鸽巢原理,数组必定有一个重复元素。而本题中,数组长度是n,数的范围是[0,n-1],也就是说数组可以没有重复元素,只不过这题告诉你至少有一个重复元素,要找出来。

这题不能用英文题中的二分的方法,比如n=6数组[0,0,2,3,4,5],l=0,r=5,m=2,统计[0,2]区间的数的个数=3,满足3<=m+1,因为即使是无重复数组[0,1,2,3,4,5],也是3<=m+1,所以无法区分左边区间是否有重复。

看题解,O(1)的方法是。如果数组没有重复元素,则排序之后nums[i]=i,所以我们模拟排序的过程,遍历数组,如果nums[i]!=i,则把nums[i]放到它排序后应该在的位置,即放到nums[nums[i]]的位置。如果nums[i]和它要放的位置nums[nums[i]]相等,说明这个元素重复出现了。完整代码如下:

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        int n = nums.size();
        for(int i = 0; i < n; ++i) {
            while(nums[i] != i) {
                if(nums[i] == nums[nums[i]]) return nums[i];
                swap(nums[i],nums[nums[i]]);
            }
        }
        return 0;
    }
};

本代码提交AC,用时92MS。

LeetCode Last Moment Before All Ants Fall Out of a Plank

5453. Last Moment Before All Ants Fall Out of a Plank

We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with speed 1 unit per second. Some of the ants move to the left, the other move to the right.

When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions doesn’t take any additional time.

When an ant reaches one end of the plank at a time t, it falls out of the plank imediately.

Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right. Return the moment when the last ant(s) fall out of the plank.

Example 1:

Input: n = 4, left = [4,3], right = [0,1]
Output: 4
Explanation: In the image above:
-The ant at index 0 is named A and going to the right.
-The ant at index 1 is named B and going to the right.
-The ant at index 3 is named C and going to the left.
-The ant at index 4 is named D and going to the left.
Note that the last moment when an ant was on the plank is t = 4 second, after that it falls imediately out of the plank. (i.e. We can say that at t = 4.0000000001, there is no ants on the plank).

Example 2:

Input: n = 7, left = [], right = [0,1,2,3,4,5,6,7]
Output: 7
Explanation: All ants are going to the right, the ant at index 0 needs 7 seconds to fall.

Example 3:

Input: n = 7, left = [0,1,2,3,4,5,6,7], right = []
Output: 7
Explanation: All ants are going to the left, the ant at index 7 needs 7 seconds to fall.

Example 4:

Input: n = 9, left = [5], right = [4]
Output: 5
Explanation: At t = 1 second, both ants will be at the same intial position but with different direction.

Example 5:

Input: n = 6, left = [6], right = [0]
Output: 6

Constraints:

  • 1 <= n <= 10^4
  • 0 <= left.length <= n + 1
  • 0 <= left[i] <= n
  • 0 <= right.length <= n + 1
  • 0 <= right[i] <= n
  • 1 <= left.length + right.length <= n + 1
  • All values of left and right are unique, and each value can appear only in one of the two arrays.

很有意思的一个题。一块长木板上,放了若干只蚂蚁,有的蚂蚁向左走,有的蚂蚁向右走。当两个蚂蚁碰头时,会各自掉头走。问所有蚂蚁走出木板的时间。

有点像智力题,分析了半天,发现挺简单的。想象一下,如果蚂蚁A向右走,蚂蚁B向左走,它们在坐标0点碰头了,则A掉头向左走,B掉头向右走。对于A和B来说,虽然它们的方向变了,但是对于整个木板来说,并没有差别,依然是有一只蚂蚁从0点向右走,有一只蚂蚁从0点向左走。相当于A和B交换了身份,各自帮对方走了剩下的流程。但是从宏观分析看,A和B不管掉不掉头,效果是一样的。

综上所述,完全不用考虑碰头掉头的问题,直接对向左走和向右走的蚂蚁单独分析。对于向左走的蚂蚁,最右边的蚂蚁要走最长的时间。对于向右走的蚂蚁,最左边的蚂蚁要走最长的时间。总时间就是这两种情况的最大值。代码如下:

class Solution {
public:
	int getLastMoment(int n, vector<int>& left, vector<int>& right) {
		sort(left.begin(), left.end());
		sort(right.begin(), right.end());
		int ans = 0;
		if (!left.empty())ans = max(ans, left.back());
		if (!right.empty())ans = max(ans, n - right.front());
		return ans;
	}
};

本代码提交AC。

LeetCode Can Make Arithmetic Progression From Sequence

5452. Can Make Arithmetic Progression From Sequence

Given an array of numbers arr. A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Return true if the array can be rearranged to form an arithmetic progression, otherwise, return false.

Example 1:

Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.

Example 2:

Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.

Constraints:

  • 2 <= arr.length <= 1000
  • -10^6 <= arr[i] <= 10^6

给一个数组,问能否通过排列组合让这个数组变成等差数列。

直接排序,看看是否是等差数列即可:

class Solution {
public:
	bool canMakeArithmeticProgression(vector<int>& arr) {
		sort(arr.begin(), arr.end());
		int diff = arr[1] - arr[0];
		for (int i = 2; i < arr.size(); ++i) {
			if (arr[i] - arr[i - 1] != diff)return false;
		}
		return true;
	}
};

本代码提交AC。