Leetcode Weekly Contest 130 题解

第一题:

Binary Prefix Divisible By 5

  1. 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]
Example 3:

Input: [0,1,1,1,1,1]
Output: [true,false,false,false,true,false]
Example 4:

Input: [1,1,1,0,1]
Output: [false,false,false,false,false]

Note:

1 <= A.length <= 30000
A[i] is 0 or 1

这道题很简单,因为长度很大,30000,所以可以边取模边存可以整除
代码:

class Solution
{
public:
    vector<bool> prefixesDivBy5(vector<int>& A)
    {
    	int len = A.size();
    	vector<bool> ans(len,false);
    	int cnt = 0;
    	int base =  0;
    	for(int i = 0; i<len; i++)
    	{
    		base  = (base << 1) + A[i];
    		//cout << base << ends;
    		if(base%5==0)  ans[cnt++] = true;
    		else ans[cnt++] = false;
    		base %= 5;
    	}
    	cout << endl;
    	for(int i = 0; i<len ;i++) cout << ans[i] << ends;
    	cout << endl;
    	return ans;
    }
};

第二题:Convert to Base -25

这个题,就是取r进制,可能会碰面负数的情况,那么比如 a / -b = r ··· d,那么d是负数怎么办 这个时候
a = -b* r + b+ d -b = -b(r+1) + (b+d) ,那么余数变为正数就行了。

代码:

class Solution 
{
public:
    string baseNeg2(int N) 
    {
    	if(N==0) return to_string(0);
    	stack<int> st;
    	string ans = "";
    	int c,s;
    	while(N)
    	{
    		c = N / -2; // 除数
    		s = N % -2; // 商
    		if(s >= 0) st.push(s);
    		else
    		{
    			s += 2;
    			c++;
    			st.push(s);
    		}
    		N = c;
    	}
    	while(!st.empty())
    	{
    		ans +=  to_string(st.top());
    		st.pop();
    	}
    	return ans;
    }
};

第三题:Next Greater Node In Linked List5
1019. Next Greater Node In Linked List
Medium

49

0

Favorite

Share
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]

这个题就是单调栈的应用,如果后进栈的元素比栈里的元素大,那么就更新结果,然后不停的出栈,直到栈里没有比它大的,然后将这个数入栈。 记得存pair
代码

/**
 * 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) 
    {
    	int len = 0 ;
    	ListNode* p = head;
    	while(p)
    	{
    		++len;
    		p = p->next;
    	}
    	vector<int> ans(len,0);
    	p = head;
    	if(p==nullptr) return ans;
    	stack<pair<int,int>> st;
    	int cur;
    	st.push(make_pair(p->val,cur++));
    	p = p->next;
    	while(p)
    	{
    		int list_value = p->val;
    		while(!st.empty())
    		{
    			pair<int,int> temp =  st.top();
    			if(temp.first < list_value)
    			{
    				ans[temp.second] = list_value;
    				st.pop();
    				if(st.empty())
    				{
    					st.push(make_pair(list_value,cur++));
    					break;
    				}
    			}
    			else 
    			{
    				st.push(make_pair(list_value, cur++));
    				break;
    			}
    		}
    		p = p->next;
    	}
    	return ans;
    }
};

第四题:Number of Enclaves

  1. Number of Enclaves
    Medium

29

2

Favorite

Share
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.

Note:

1 <= A.length <= 500
1 <= A[i].length <= 500
0 <= A[i][j] <= 1
All rows have the same size.
Accepted

就是求不和边缘陆地联通的land个数,直接dfs边缘即可。

代码:

class Solution
{
public:
	bool judge(int x, int y, int row, int col)
	{
		if(x<0 || x>row || y<0 || y>col) return false;
		return true;
	}

	void dfs(int x, int y, int row, int col, vector<vector<int> > &vis, vector<vector<int>>& A)
	{
	   // cout << x << ends << y <<endl;
		vis[x][y] = true;
		int dx[] = {-1,1,0,0};
		int dy[] = {0,0,-1,1};
		for(int i = 0; i < 4; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if(judge(xx,yy,row,col) && !vis[xx][yy] && A[xx][yy]==1 ) dfs(xx, yy, row, col, vis, A);
		}
	}
    int numEnclaves(vector<vector<int>>& A)
    {
    	int row = A.size();
    	if(row == 0) return 0;
    	int col = A[0].size();
    	if(col == 0) return 0;
   		vector<vector<int> > vis;
   		vis.resize(row);
   		for(int i = 0; i < row; i++) vis[i].resize(col);
   		for(int i = 0; i < row; i++)
   			for(int j = 0; j < col; j++)
   				vis[i][j] = false;
   		for(int i = 0; i < row; i ++)
   			if(A[i][0] == 1 && !vis[i][0]) dfs(i,0,row-1,col-1,vis,A);

   		for(int i = 0; i < row; i ++)
   			if(A[i][col-1] == 1 && !vis[i][col-1]) dfs(i,col-1,row-1,col-1,vis,A);

   		for(int i = 0; i< col; i++)
   			if(A[0][i] == 1 && !vis[0][i]) dfs(0,i,row-1,col-1,vis,A);

   		for(int i = 0; i < col; i++)
   			if(A[row-1][i] == 1 && !vis[row-1][i]) dfs(row-1,i,row-1,col-1,vis,A);

        int ans = 0;
        for(int i = 0; i < row; i++)
   			for(int j = 0; j < col; j++)
   				if(A[i][j]==1 && !vis[i][j]) ++ans;
   		return ans;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值