Leetcode cWeekly Contest 129

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014303647/article/details/88775626

Leetcode 1020. Partition Array Into Three Parts With Equal Sum

题目:
Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + … + A[i] == A[i+1] + A[i+2] + … + A[j-1] == A[j] + A[j-1] + … + A[A.length - 1])

Example 1:

Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:

Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false
Example 3:

Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4

解析:其实就是前缀和的应用。找到l,r即可。

代码:

class Solution
{
public:
    bool canThreePartsEqualSum(vector<int>& A)
    {
    	int sum = 0;
    	int size = A.size();
    	vector<int> dp(size,0);
    	for(int i = 0; i < size; i++)
    	{
    		if(i==0) dp[0] = A[i];
    		else dp[i] = dp[i-1] + A[i];
    		sum += A[i];
    	}
    	if(sum%3!=0) return false;
    	int l=-1,r=-1;
    	for(int i = 0; i<size;i++)
    	{
    		if(dp[i] == sum/3)
    		{
    			l = i;
    			break;
    		}
    	}
    	if(l==-1) return false;
    	for(int i = l+1;i<size;i++)
    	{
    		if(dp[i]-dp[l] == sum/3)
    		{
    			r = i;
    			break;
    		}
    	}
    	if(r==-1) return false;
    	if(dp[size-1]-dp[r]==sum/3) return true;
    	else return false;
    }
};

Leetcode 1022. Smallest Integer Divisible by K
Given a positive integer K, you need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1.

Return the length of N. If there is no such N, return -1.

Example 1:

Input: 1
Output: 1
Explanation: The smallest answer is N = 1, which has length 1.
Example 2:

Input: 2
Output: -1
Explanation: There is no such positive integer N divisible by 2.
Example 3:

Input: 3
Output: 3
Explanation: The smallest answer is N = 111, which has length 3.

Note:

1 <= K <= 10^5

题意:是否存在只由1构成的数,整数K。

解析:用模拟除法的方法去不停的mod,并且假装后面有很多1。

代码:

class Solution
{
public:
    int smallestRepunitDivByK(int K)
    {
    	int cnt = 0;
    	int value = 0;
        int i;
    	for(i = 0; i < 1e6; i++)
    	{
    		int value = (value*10+1)%K;
    		++cnt;
            if(value==0) break;
    	}
    	if(i==1e6) return -1;
    	return cnt;
    }
};

Leetcode 1021. Best Sightseeing Pair
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.

The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.

Example 1:

Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

Note:

2 <= A.length <= 50000
1 <= A[i] <= 1000

题意:就是给很多个景点,然后两个景点之间有个衡量标准,求一个最大值。

解析:我是左扫一次,比如到第i点,我得到的是从[0,i)的一个左闭右开的区间的最大值,而从右往前扫,我维护的是一个[i,size-1] 左右皆闭合的区间。这样最后扫一次,就可以得到最大值了。

代码:

class Solution
{
public:
    int maxScoreSightseeingPair(vector<int>& A)
    {
    	int size = A.size();
    	int ans = -1;
    	vector<int> l_max(size,0);
    	vector<int> r_max(size,0);
    	l_max[1] = max(l_max[0],A[0]+0);
    	for(int i = 2;i<size;i++)
    		l_max[i] = max(l_max[i-1],A[i-1]+i-1);
    	r_max[size-1] = A[size-1]-(size-1);
    	r_max[size-2] = max(r_max[size-1],A[size-2]-(size-2));
    	for(int i = size-3;i>=1;i--)
    		r_max[i] = max(r_max[i+1],A[i]-i);
    	for(int i = 0;i<size;i++)
        {
           //cout << l_max[i] << " " << r_max[i] <<endl;
            ans =  max(l_max[i]+r_max[i],ans);
        }
    	return ans;
    }
};

Leetcode 1023. Binary String With Substrings Representing 1 To N
Given a binary string S (a string consisting only of ‘0’ and '1’s) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.

Example 1:

Input: S = “0110”, N = 3
Output: true
Example 2:

Input: S = “0110”, N = 4
Output: false

Note:

1 <= S.length <= 1000
1 <= N <= 10^9

题意:就是给一个母串,给一个数N,看是否从1-N的所有的数的二进制表示都是母串的子串。

这个题没想到是直接暴力。。。

class Solution 
{
public:
    bool queryString(string S, int N) 
    {
    	for(int i = 1;i<=N;i++)
    	{
    		string str = "";
    		int x = i;
    		while(x)
    		{
    			str += x%2+'0';
    			x /= 2;
    		}
    		reverse(str.begin(), str.end());
    		if(S.find(str)==string::npos) return false;
    	}
    	return true;
    }
};

猜你喜欢

转载自blog.csdn.net/u014303647/article/details/88775626
129
今日推荐