Leetcode进阶之路——Weekly Contest 130

37 篇文章 0 订阅

1017. Convert to Base -2

Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).
The returned string must have no leading zeroes, unless the string is “0”.
Example 1:
Input: 2
Output: “110”
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
Example 2:
Input: 3
Output: “111”
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3

题意很简单,负进制的转换,2转换为-2进制为110
同样可以用短除法解,但是要注意标准的出发,余数和被除数是同号的, 也就是会出现余数为负的情况,这时候就要做个判断,使余数为非负(其实就加该负进制的绝对值即可)
举例如下:
-15| -2
———
   8| …余1 因为-15减8×(-2)=1 (实际应该是7余-1,为了保证余数为正,商+1,余数+2)
———
  -4| …余0
———
   2| …余0
———
  -1| …余0
———
   1| …余1 实际是0余-1, 保证余数为正,商+1,余数+2
———
  0| …余1
结束。
于是可写出代码:

class Solution {
public:
    string baseNeg2(int N) {
		if (N == 0) return "0";
		string s = "";
		while (N)
		{
			int mod = N % -2;
			N /= -2;
            if(mod < 0)
            {
                N += 1;
                mod += 2;
            }
            s += char(mod + 48);
		}
		reverse(s.begin(), s.end());
		return s;
    }
};

1018. Binary Prefix Divisible By 5

Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)
Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
Example 1:
Input: [0,1,1]
Output: [true,false,false]
Explanation:
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
Example 2:
Input: [1,1,1]
Output: [false,false,false]

给定一个整型数组,判断从A[0] 到 A[i]组成的二进制数,能否被5整除
很简单,遍历整个数组,暴力解即可

class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
        int ans = 0;
        vector<bool> v;
        if(A.size() == 0) return v;
        ans = A[0];
        v.emplace_back((ans == 0? true: false));
        for(int i = 1; i < A.size(); ++i)
        {
            ans = ans * 2 + A[i];
            ans %= 5;
            v.emplace_back((ans == 0? true: false));
        }
        return v;
    }
};

1019. Next Greater Node In Linked List

We are given a linked list with head as the first node. Let’s number the nodes in the list: node_1, node_2, node_3, … etc.
Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.
Return an array of integers answer, where answer[i] = next_larger(node_{i+1}).
Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.
Example 1:
Input: [2,1,5]
Output: [5,5,0]
Example 2:
Input: [2,7,4,3,5]
Output: [7,0,5,5,0]
Example 3:
Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0]

给定一个数组,找出它后面第一个比它大的数
O(n^2)的方法我就不多说了,介绍一种采用了哈希表和栈的方法
先遍历整个链表,同时将其转化为数组:
若此时栈不为空,则比较栈顶元素与当前元素
若当前元素较大,则当前元素即为栈顶元素的右边第一个比它大的数
直到栈为空或栈顶元素更大
要注意由于数字可能重复,因此哈希表中存的并不是一个索引或数量,而是一个队列(先进先出),最后遍历一遍上面的数组,找到对应的元素,保存即可

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
		ListNode* ln = head;
		stack<int> st;
		unordered_map<int, queue<int>> um;
		vector<int> v;
		while (ln)
		{
			int val = ln->val;
			v.emplace_back(val);
			ln = ln->next;
			while (st.empty() == false && st.top() < val)
			{
				um[st.top()].emplace(val);
				st.pop();
			}
			st.push(val);
		}
		//um[v[v.size() - 1]].emplace(0);
		vector<int> res;
		for (int i = 0; i < v.size(); ++i)
		{
            if(um[v[i]].size() == 0) res.emplace_back(0);
			else 
            {
                res.emplace_back(um[v[i]].front());
			    um[v[i]].pop();
            }
		}
		return res;
	}
};

1020. Number of Enclaves

Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)
A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.
Example 1:
Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation:
There are three 1s that are enclosed by 0s, and one 1 that isn’t enclosed because its on the boundary.
Example 2:
Input: [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation:
All 1s are either on the boundary or can reach the boundary.

给定一个二维数组,数一下不在四个边缘或者无法间接抵达边缘的1的个数
很典型的深搜,遍历四条边,然后把找到的1和与其(间接)相邻的所有1都变为0,这样最后剩下的1的个数即为答案

class Solution {
public:
    int numEnclaves(vector<vector<int>>& A) {
		for (int i = 0; i < A.size(); ++i)
		{
			if (A[i][0] == 1)
			{
				numEnclavesHelper(A, i, 0);
			}
			if (A[i][A[0].size() - 1] == 1)
			{
				numEnclavesHelper(A, i, A[0].size() - 1);
			}
		}
		for (int i = 0; i < A[0].size(); ++i)
		{
			if (A[0][i] == 1)
			{
				numEnclavesHelper(A, 0, i);
			}
			if (A[A.size() - 1][i] == 1)
			{
				numEnclavesHelper(A, A.size() - 1, i);
			}
		}
		int cnt = 0;
		for (int i = 0; i < A.size(); ++i)
		{
			for (int j = 0; j < A[0].size(); ++j)
			{
				if (A[i][j] == 1) cnt++;
			}
		}
		return cnt;
	}

	void numEnclavesHelper(vector<vector<int>>& A, int i, int j)
	{
		if (i < 0 || i >= A.size()) return;
		if (j < 0 || j >= A[0].size()) return;
		if (A[i][j] == 1)
		{
			A[i][j] = 0;
			numEnclavesHelper(A, i + 1, j);
			numEnclavesHelper(A, i - 1, j);
			numEnclavesHelper(A, i, j + 1);
			numEnclavesHelper(A, i, j - 1);
		}
	}
};

1019的方法虽然看起来复杂度降低,但实际测试的效果反而效率不高= =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值