Leetcode每日随机2021/4/28

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

leetcode93
普通DFS。
leetcode1551
这是在逗我吗?这是中等题?这也太傻比了
leetcode1154
傻卵,考常识?

代码

class Solution {
    private List<List<String>> res = new ArrayList<List<String>>();
	private Stack<String> stack = new Stack<String>();

	public List<String> restoreIpAddresses(String s) {
		dfs(stack, s, 0);
		List<String> list = new ArrayList<String>();
		for (List<String> ipList : res) {
			list.add(ipList.stream().collect(Collectors.joining(".")));
		}
		return list;
	}

	private void dfs(Stack<String> stack, String s, int idx) {
		if (stack.size() == 4) {
			if (idx == s.length()) {
				res.add(new ArrayList<String>(stack));
			}
			return;
		}
		for (int i = 1; i < 4; i++) {
			if (idx + i > s.length()) {
				break;
			}
			String temp = s.substring(idx, idx + i);
			if (temp.length() > 1 && temp.startsWith("0")) {
				break;
			}
			if (Integer.valueOf(temp) < 256) {
				stack.push(temp);
				dfs(stack, s, idx + i);
				stack.pop();
			}
		}
	}
}

leetcode1551

class Solution {
    public int minOperations(int n) {
		int sum = 0;
		for (int i = 1; i < n; i += 2) {
			sum += n - i;
		}
		return sum;
	}
}

leetcode1154

class Solution {
    public int dayOfYear(String date) {
		int[] count = new int[12];
		count[0] = 31;
		count[1] = 28;
		count[2] = 31;
		count[3] = 30;
		count[4] = 31;
		count[5] = 30;
		count[6] = 31;
		count[7] = 31;
		count[8] = 30;
		count[9] = 31;
		count[10] = 30;
		count[11] = 31;
		String[] dates = date.split("-");
		int year = Integer.valueOf(dates[0]);
		int month = Integer.valueOf(dates[1]);
		int day = Integer.valueOf(dates[2]);
		if (year % 4 == 0) {
			count[1] = 29;
		}
		if (year == 1900) {
			count[0] = 28;
		}
		int days = 0;
		for (int i = 0; i < month; i++) {
			if (i == month - 1) {
				days += day;
			} else {
				days += count[i];
			}
		}
		return days;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值