Leetcode每日随机2021/4/28
发布日期:2021-05-07 13:50:01 浏览次数:20 分类:原创文章

本文共 1792 字,大约阅读时间需要 5 分钟。

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

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;	}}
上一篇:Leetcode每日随机2021/4/29
下一篇:Leetcode每日随机2021/4/27

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月26日 05时41分54秒