Find the Difference
User Accepted: 812
User Tried: 861
Total Accepted: 1362
Total Submissions: 1552
Difficulty: Easy
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:Input:
s = "abcd" t = "abcde"
Output:
e
Explanation:
"e" is the letter that was added.
public class Solution { public char findTheDifference(String s, String t) { MapElimination Gamemap = new HashMap<>(); char[] schar = s.toCharArray(); char[] tchar = t.toCharArray(); for (int i = 0; i < s.length(); i++) { if (map.containsKey(schar[i])) map.put(schar[i], map.get(schar[i])+1); else map.put(schar[i], 1); } for (int i = 0; i < t.length(); i++) { if (map.containsKey(tchar[i]) && map.get(tchar[i]) > 0) map.put(tchar[i], map.get(tchar[i])-1); else return tchar[i]; } return "a"; } }
User Accepted: 6
User Tried: 26
Total Accepted: 8
Total Submissions: 40
Difficulty: Medium
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.
We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Find the last number that remains starting with a list of length n.
Example:Input:
n = 9, 1 2 3 4 5 6 7 8 9 2 4 6 8 2 6 6
Output:
6Solution
public class Solution { public int lastRemaining(int n) { int rest = n, start = 1, step = 2; boolean left = true; while (rest > 1) { rest /= 2; if (left) start = start + step * rest - step / 2; else start = start - step * rest + step / 2; step *= 2; left = !left; } return start; } }Perfect Rectangle
User Accepted: 7
User Tried: 136
Total Accepted: 8
Total Submissions: 338
Difficulty: Hard
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.
Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).
ExampleExample 1:
rectangles = [ [1,1,3,3], [3,1,4,2], [3,2,4,4], [1,3,2,4], [2,3,3,4] ]
Return true. All 5 rectangles together form an exact cover of a rectangular region.
Example 2:
rectangles = [ [1,1,2,3], [1,3,2,4], [3,1,4,2], [3,2,4,4] ]
Return false. Because there is a gap between the two rectangular regions.
Example 3:
rectangles = [ [1,1,3,3], [3,1,4,2], [1,3,2,4], [3,2,4,4] ]
Return false. Because there is a gap in the top center.
Example 4:
rectangles = [ [1,1,3,3], [3,1,4,2], [1,3,2,4], [2,2,4,4] ]
Return false. Because two of the rectangles overlap with each other.
Solution (26/27 passed)public class Solution { public boolean isRectangleCover(int[][] A) { int m = A.length; int minlbrow = Integer.MAX_VALUE, minlbcol = Integer.MAX_VALUE, maxrurow = 0, maxrucol = 0; for (int i = 0; i < m; i++) { minlbrow = Math.min(minlbrow, A[i][1]); minlbcol = Math.min(minlbcol, A[i][0]); maxrurow = Math.max(maxrurow, A[i][3]); maxrucol = Math.max(maxrucol, A[i][2]); } int[] largest = {minlbrow, minlbcol, maxrurow, maxrucol}; int alarge = area(largest); int asum = 0; for (int i = 0; i < m; i++) { asum += area(A[i]); } return asum == alarge; } public int area(int[] a) { if (a.length != 4) return 0; return (a[2]-a[0]) * (a[3]-a[1]); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65078.html
摘要:題目要求用一個(gè)二維數(shù)組來(lái)表示一堆矩形,二維數(shù)組中的每一行分別記錄矩形左下角和右上角的坐標(biāo)。該理想情況下矩形的面積應(yīng)當(dāng)?shù)扔谒芯匦蔚拿娣e之和。一旦不相等,則一定無(wú)法構(gòu)成大的矩形。其次,光判斷面積并不足夠,可以這樣三個(gè)矩形構(gòu)成的圖形。 題目要求 Given N axis-aligned rectangles where N > 0, determine if they all togeth...
摘要:只要出現(xiàn)當(dāng)前右邊界高度小于等于棧頂元素高度的情況,就取出棧頂元素當(dāng)前遍歷過(guò)最高的并對(duì)這個(gè)元素進(jìn)行取最大矩形的運(yùn)算。 Problem Given n non-negative integers representing the histograms bar height where the width of each bar is 1, find the area of largest ...
摘要:首先確定上下的邊界,左右線段按照橫坐標(biāo)排序。檢查填充滿上圖的情況就組成不了一個(gè)長(zhǎng)方形。找重合和有空隙只需要把所有橫坐標(biāo)在的線段排序之后檢查首位相連,且起點(diǎn),終點(diǎn)。且最后成的面積等于小矩形的面積和。 Perfect Rectangle 題目鏈接:https://leetcode.com/problems... 掃描線,哪個(gè)方向都行。我是從左往右掃,矩陣按照左右的邊來(lái)存。showImg(h...
Problem For a web developer, it is very important to know how to design a web pages size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose l...
摘要:代碼第一次寫入就先不比較第一次寫入就先不比較哈希表法復(fù)雜度時(shí)間空間思路因?yàn)闀?huì)多次調(diào)用,我們不能每次調(diào)用的時(shí)候再把這兩個(gè)單詞的下標(biāo)找出來(lái)。我們可以用一個(gè)哈希表,在傳入字符串?dāng)?shù)組時(shí),就把每個(gè)單詞的下標(biāo)找出存入表中。 Shortest Word Distance Given a list of words and two words word1 and word2, return the ...
閱讀 1355·2019-08-30 15:44
閱讀 2099·2019-08-30 11:04
閱讀 518·2019-08-29 15:17
閱讀 2540·2019-08-26 12:12
閱讀 3133·2019-08-23 18:09
閱讀 922·2019-08-23 15:37
閱讀 1522·2019-08-23 14:43
閱讀 2920·2019-08-23 13:13