力扣 第 295 场周赛

力扣 第 295 场周赛

6078. 重排字符形成目标字符串

啪的一下啊,思路就来了啊,直接提交的啊,没有犹豫,打比赛就要笑着打,wa了啊,小问题,修修改改,过了
代码很烂,以后不要写这么烂的代码呀

class Solution {
	public int rearrangeCharacters(String s, String target) {
		int res = Integer.MAX_VALUE;
		int len = target.length();
		StringBuffer sb = new StringBuffer(s);
		int[] val = new int[26];
		int max = 0;
		int flag = 0;
		for(int i = 0; i < len; i++){
			val[target.charAt(i) - 'a']++;
		}
		System.out.println((char)'a');
		int[] val1 =  new int[26];
		for(int i = 0; i < s.length(); i++){
			if(val[s.charAt(i) - 'a'] != 0){
				val1[s.charAt(i) - 'a']++;
			}
		}
//		Arrays.sort(val);
//		Arrays.sort(val1);
		List<Integer> list = new ArrayList<>();
		List<Integer> list1 = new ArrayList<>();
		for(int i = 0; i < 26; i++){
			if(val[i] != 0){
				list.add(val[i]);
			}
			if(val1[i] != 0){
				list1.add(val1[i]);
			}
		}
		if(list.size() == list1.size()){
			for(int i = 0; i < list.size(); i++){
				if(list.get(i) <= list1.get(i)){
					if(res > list1.get(i) / list.get(i))
					res = list1.get(i) / list.get(i);
				}else return 0;
			}
			return res;
		}else return 0;
	}
}

优美的代码

class Solution {
	public int rearrangeCharacters(String s, String target) {
		int count[] = new int[26], count2[] = new int[26], min = Integer.MAX_VALUE;
		for (char c : s.toCharArray()) {
			count[c - 'a']++;
		}
		for (char c : target.toCharArray()) {
			count2[c - 'a']++;
		}
		for (char c : target.toCharArray()) {
			min = Math.min(min, count[c - 'a'] / count2[c - 'a']);
		}
		return min;
	}
}

6079. 价格减免

困难题我唯唯诺诺,简单题我重拳出击的呀,啪的一下很快呀,过了
class Solution {
public String discountPrices(String sentence, int discount) {
		String[] temp = sentence.split(" ");
		String ta = "";
		for(int i = 0; i < temp.length; i++){
			if(temp[i].length() >= 2 && temp[i].charAt(0) == '$' &&
					temp[i].charAt(1) != '-' && isLong(temp[i].substring(1))){
				String cur = temp[i].substring(1);
				double pre = Long.valueOf(cur);
                double x = 1 - (double)discount / 100;
                // System.out.println(x);
				double res = (double)pre*x;
                System.out.println();
                System.out.printf("%.2f",res);
				String s = String.format("%.2f", (double)res);
				temp[i] = "$" + s;
			}
			if(i != temp.length - 1) {
				// System.out.print(temp[i] + " ");
				ta += temp[i] + " ";
			}
			else {
				// System.out.print(temp[i]);
				ta += temp[i];
			}
		}
		return ta;
	
	}
	/**
	 * 判断字符串是否是long类型
	 */
	public static boolean isLong(String value) {
		try {
			Long.valueOf(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}

优美代码

class Solution {

	public String discountPrices(String sentence, int discount) {
		return Stream.of(sentence.split(" "))
				.map(t -> !t.matches("\\$\\d+") ? t: String.format("$%.2f", Long.parseLong(t.substring(1)) * (1 - discount / 100.)))
				.collect(Collectors.joining(" "));
	}
}

6080. 使数组按非递减顺序排列

乍一看没有头绪,我直接下一个呀 单调栈模拟

class Solution {
	public int totalSteps(int[] nums) {
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		int dp[] = new int[nums.length], max = 0;
		for (int i = nums.length - 1; i >= 0; deque.push(i--)) {
			while (!deque.isEmpty() && nums[deque.peek()] < nums[i]) {
				max = Math.max(max, dp[i] = Math.max(dp[i] + 1, dp[deque.pop()]));
			}
		}
		return max;
	}
}

6081. 到达角落需要移除障碍物的最小数目

一看简简单单呀,别的不说,爆搜可是做的不少,直接广搜的呀,好吧没时间了,我直接退出答题

class Solution {
	public int minimumObstacles(int[][] grid) {
		PriorityQueue<int[]> queue = new PriorityQueue<>((o, p) -> o[0] - p[0]);
		boolean[][] visited = new boolean[grid.length][grid[0].length];
		for (queue.offer(new int[3]);;) {
			int[] poll = queue.poll();
			if (poll[1] == grid.length - 1 && poll[2] == grid[0].length) {
				return poll[0];
			} else if (poll[1] >= 0 && poll[1] < grid.length && poll[2] >= 0 && poll[2] < grid[0].length
					&& !visited[poll[1]][poll[2]]) {
				visited[poll[1]][poll[2]] = true;
				queue.offer(new int[] { poll[0] + grid[poll[1]][poll[2]], poll[1] - 1, poll[2] });
				queue.offer(new int[] { poll[0] + grid[poll[1]][poll[2]], poll[1], poll[2] - 1 });
				queue.offer(new int[] { poll[0] + grid[poll[1]][poll[2]], poll[1], poll[2] + 1 });
				queue.offer(new int[] { poll[0] + grid[poll[1]][poll[2]], poll[1] + 1, poll[2] });
			}
		}
	}
}

溜溜球,我是菜鸡,下次一定变厉害

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值