Leetcode周赛183

这次周赛排名:6750 / 12542

疲惫。这次的赛感觉是真的难,也看到评论里说的了,但是Lee215寒神的评论是异常简单。第二题就卡住在大二位数上面了。学到了BigInteger的用法。第三题更是没有思路。

第一题 Q1403
Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.

public List<Integer> minSubsequence(int[] nums) {
        int n = nums.length;
		List<Integer> res = new ArrayList<>();
		Arrays.sort(nums);

		int total = 0;
		for (int a : nums)
			total += a;
			
		int temp = 0;
		int left = total;

		for (int i = n - 1; i >= 0; i--) {
			res.add(nums[i]);
			temp += nums[i]; 
			left -= nums[i];

			if (temp > left) 
				return res;
		}
		return res;
    }


> 第二题 Q1404 > Given a number s in their binary representation. Return the number of steps to reduce it to 1 under the following rules: > If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it.
import java.math.BigInteger;

class Solution {
    public int numSteps(String s) {
        BigInteger i = new BigInteger(s, 2);
		int res = 0;
        
		while (!BigInteger.ONE.equals(i)) {
			if (i.testBit(0))
				i = i.add(BigInteger.ONE); // add 1
			else 
				i = i.shiftRight(1); // divide 2
			
			res++;
		}
		return res;
    }
}

这个题学到了BIgInteger。当然这个可以用位操作的方法。

第三题第四题有空回来再补吧。累了。
第四题是minimax的题。看起来很有意思。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值