Leetcode周赛184

这次周赛排名:3839 / 9816

破天荒的做出3题,但是却丝毫不觉得开心。 为什么呢?因为这个题实在是和上周的没法比,太水了。最后一题hard看了半天,没想到方法。hard的题目从现在起要开始搞一搞了。另外,可能是题目太水了,因此排名还是在一40%左右(错三次,罚时15分钟)。阿西吧。继续加油。

第一题 1408. String Matching in an Array
Given an array of string words. Return all strings in words which is substring of another word in any order.

public List<String> stringMatching(String[] words) {
		int n = words.length;
		List<String> res = new ArrayList<>();

		for (int i = 0; i < n; i++) { // examine each word i
			inner: for (int j = 0; j < n; j++) { // see if other word contains word i
				if (j != i) { // not check i word itself
					if (words[j].indexOf(words[i]) != -1) {
						res.add(words[i]);
						break inner; // 立刻返回
					}
				}
			}
		}
		return res;
	}

注意,如果一个word i 是被其他word包含的话,立刻return,因为一个单词可能被很多单词包含。这里提交错误,罚时了。



> 第二题 1409. Queries on a Permutation With Key > Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.
    public int[] processQueries(int[] queries, int m) {
		int n = queries.length;
		int[] res = new int[n];

		int[] p = new int[m];
		for (int i = 0; i < m; i++) {
			p[i] = i + 1;
		}

		// 把int[] 转换成 List;或者可以逐个加入,因为需要boxing
		List<Integer> li = Arrays.stream(p).boxed().collect(Collectors.toList());

		// do query
		for (int i = 0; i < n; i++) {
			int pos = li.indexOf(queries[i]); // queries[i]元素的位置
			res[i] = pos; // 加入结果中

			li.remove(pos); // 取出pos位置的元素,pos是index
			li.add(0, queries[i]); // 把queries[i]的元素加入list头的位置
		}
		return res;
	}

List可以实现加入/取出指定index位置的元素的功能。

第三题 1410. HTML Entity Parser
HTML entity parser is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.

public static String entityParser(String text) {
		text = text.replaceAll(""", "\"");
		text = text.replaceAll("'", "'");
		text = text.replaceAll("&", "&");
		text = text.replaceAll(">", ">");
		text = text.replaceAll("<", "<");
		text = text.replaceAll("⁄", "/");
		return text;
	}

记住string 的replace函数就行了,没有难点。

PS:
北京的天气回暖了。没想到这个冬天会这样的过去,真是奇迹;奇迹没有在上海被冻死,奇迹能够在这条路上走了五个月这么久。还记得去年11月在其灵自如寓在室友游戏的机械键盘声中度过的刷bobo老师视频的日子。一个上下求索的冬天。
春天来了,没有哪个春天不会到来。希望一切回到正轨,调整心态,加快速度向hard进攻,夺取最后的胜利。

你可能感兴趣的:(leetcode周赛)