Цена за 48 часов в ленте | 2650,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
public class Solution {
public double dist(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}
public boolean check(int[] p1, int[] p2, int[] p3, int[] p4) {
return dist(p1,p2) > 0 && dist(p1, p2) == dist(p2, p3) && dist(p2, p3) == dist(p3, p4) && dist(p3, p4) == dist(p4, p1) && dist(p1, p3) == dist(p2, p4);
}
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
return check(p1, p2, p3, p4) || check(p1, p3, p2, p4) || check(p1, p2, p4, p3);
}
}
Input: expression = "-1/2+1/2+1/3"
Output: "1/3"
class Solution {
public String fractionAddition(String expression) {
Listsign = new ArrayList<>();
if (expression.charAt(0) != '-') sign.add('+');
for (int i = 0; i < expression.length(); i++) {
if (expression.charAt(i) == '+' || expression.charAt(i) == '-')
sign.add(expression.charAt(i));
}
int prev_num = 0, prev_den = 1, i = 0;
for (String sub : expression.split("(\\+)|(-)")) {
if (sub.length() > 0) {
String[] fraction = sub.split("/");
int num = (Integer.parseInt(fraction[0]));
int den = (Integer.parseInt(fraction[1]));
int g = Math.abs(gcd(den, prev_den));
if (sign.get(i++) == '+') prev_num = prev_num * den / g + num * prev_den / g;
else prev_num = prev_num * den / g - num * prev_den / g;
prev_den = den * prev_den / g;
g = Math.abs(gcd(prev_den, prev_num));
prev_num /= g;
prev_den /= g;
}
}
return prev_num + "/" + prev_den;
}
public int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
}
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
class Solution {
public Listpostorder(Node root) {
LinkedListstack = new LinkedList<>();
LinkedListoutput = new LinkedList<>();
if (root == null) {
return output;
}
stack.add(root);
while (!stack.isEmpty()) {
Node node = stack.pollLast();
output.addFirst(node.val);
for (Node item : node.children) {
if (item != null) {
stack.add(item);
}
}
}
return output;
}
class Node {
public int val;
public Listchildren;
public Node() {}
public Node(int _val, List_children) {
val = _val;
children = _children;
}
}
}
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
public class Solution {
public String reverseWords(String s) {
char[] chars = s.toCharArray();
int lastSpaceIndex = -1;
int len = chars.length;
for (int strIndex = 0; strIndex <= len; strIndex++) {
if (strIndex == len || chars[strIndex] == ' ') {
int startIndex = lastSpaceIndex + 1;
int endIndex = strIndex - 1;
while (startIndex < endIndex) {
char temp = chars[startIndex];
chars[startIndex] = chars[endIndex];
chars[endIndex] = temp;
startIndex++;
endIndex--;
}
lastSpaceIndex = strIndex;
}
}
return new String(chars);
}
}
Input: n = 12
Output: 21
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public String swap(String s, int i0, int i1) {
if (i0 == i1)
return s;
String s1 = s.substring(0, i0);
String s2 = s.substring(i0 + 1, i1);
String s3 = s.substring(i1 + 1);
return s1 + s.charAt(i1) + s2 + s.charAt(i0) + s3;
}
ArrayListlist = new ArrayList<>();
void permute(String a, int l, int r) {
int i;
if (l == r)
list.add(a);
else {
for (i = l; i <= r; i++) {
a = swap(a, l, i);
permute(a, l + 1, r);
a = swap(a, l, i);
}
}
}
public int nextGreaterElement(int n) {
String s = "" + n;
permute(s, 0, s.length() - 1);
Collections.sort(list);
int i;
for (i = list.size() - 1; i >= 0; i--) {
if (list.get(i).equals("" + n))
break;
}
return i == list.size() - 1 ? -1 : Integer.parseInt(list.get(i + 1));
}
}
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
Output: 2
public class Solution {
public int leastBricks(List> wall) {
int[] pos = new int[wall.size()];
int sum = 0;
int res = Integer.MAX_VALUE;
for (int el : wall.get(0)) {
sum += el;
}
while (sum != 0) {
int count = 0;
for (int i = 0; i < wall.size(); i++) {
Listrow = wall.get(i);
if (row.get(pos[i]) != 0) {
count++;
} else {
pos[i]++;
}
row.set(pos[i], row.get(pos[i]) - 1);
}
sum--;
res = Math.min(res, count);
}
return res;
}
}
Input: password = "a"
Output: 5
public class Solution {
public int strongPasswordChecker(String s) {
int n = s.length();
boolean hasLower = false, hasUpper = false, hasDigit = false;
int repeatCount = 0;
for (int i = 0; i < n;) {
if (Character.isLowerCase(s.charAt(i))) hasLower = true;
if (Character.isUpperCase(s.charAt(i))) hasUpper = true;
if (Character.isDigit(s.charAt(i))) hasDigit = true;
int start = i;
while (i < n && s.charAt(i) == s.charAt(start)) {
i++;
}
repeatCount += (i - start) / 3;
}
int missingTypes = (hasLower ? 0 : 1) + (hasUpper ? 0 : 1) + (hasDigit ? 0 : 1);
if (n < 6) {
return Math.max(missingTypes, 6 - n);
} else if (n <= 20) {
return Math.max(missingTypes, repeatCount);
} else {
int excessChars = n - 20;
int overLenReduction = 0;
for (int i = 2; i < n && excessChars > 0; i++) {
if (i % 3 == 2 && s.charAt(i) == s.charAt(i - 1) && s.charAt(i) == s.charAt(i - 2)) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + Math.max(missingTypes, repeatCount);
}
}
}
Input: nums = [1000,100,10,2]
Output: "1000/(100/10/2)"
Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority.
So you should return "1000/(100/10/2)".
Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
class Solution {
private ListaddStrings(List num1, List num2) {
Listans = new ArrayList<>();
int carry = 0;
int n1 = num1.size();
int n2 = num2.size();
for (int i = 0; i < Math.max(n1, n2) + 1; ++i) {
int digit1 = i < n1 ? num1.get(i) : 0;
int digit2 = i < n2 ? num2.get(i) : 0;
int sum = digit1 + digit2 + carry;
carry = sum / 10;
ans.add(sum % 10);
}
return ans;
}
private ListmultiplyOneDigit(String firstNumber, char secondNumberDigit, int numZeros) {
ListcurrentResult = new ArrayList<>(Collections.nCopies(numZeros, 0));
int carry = 0;
for (char digit : firstNumber.toCharArray()) {
int multiplication = (secondNumberDigit - '0') * (digit - '0') + carry;
carry = multiplication / 10;
currentResult.add(multiplication % 10);
}
if (carry != 0) {
currentResult.add(carry);
}
return currentResult;
}
public String multiply(String firstNumber, String secondNumber) {
if (firstNumber.equals("0") || secondNumber.equals("0")) {
return "0";
}
firstNumber = new StringBuilder(firstNumber).reverse().toString();
secondNumber = new StringBuilder(secondNumber).reverse().toString();
Listans = new ArrayList<>(Collections.nCopies(firstNumber.length() + secondNumber.length(), 0));
for (int i = 0; i < secondNumber.length(); ++i) {
ans = addStrings(multiplyOneDigit(firstNumber, secondNumber.charAt(i), i), ans);
}
while (ans.get(ans.size() - 1) == 0) {
ans.remove(ans.size() - 1);
}
StringBuilder answer = new StringBuilder();
for (int i = ans.size() - 1; i >= 0; --i) {
answer.append(ans.get(i));
}
return answer.toString();
}
}
Input: sentence = ["hello","world"], rows = 2, cols = 8
Output: 1
public class Solution {
public int wordsTyping(String[] sentence, int rows, int cols) {
String sentenceStr = String.join(" ", sentence) + " ";
int length = sentenceStr.length();
int count = 0;
for (int i = 0; i < rows; i++) {
count += cols;
if (sentenceStr.charAt(count % length) == ' ') {
count++;
} else {
while (count > 0 && sentenceStr.charAt((count - 1) % length) != ' ') {
count--;
}
}
}
return count / length;
}
}
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List> pacificAtlantic(int[][] heights) {
int m = heights.length;
int n = heights[0].length;
boolean[][] pacific = new boolean[m][n];
boolean[][] atlantic = new boolean[m][n];
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int i = 0; i < m; i++) {
dfs(i, 0, pacific, heights, directions);
dfs(i, n - 1, atlantic, heights, directions);
}
for (int j = 0; j < n; j++) {
dfs(0, j, pacific, heights, directions);
dfs(m - 1, j, atlantic, heights, directions);
}
List> result = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
Listcell = new ArrayList<>();
cell.add(i);
cell.add(j);
result.add(cell);
}
}
}
return result;
}
private void dfs(int r, int c, boolean[][] ocean, int[][] heights, int[][] directions) {
ocean[r][c] = true;
for (int[] dir : directions) {
int nr = r + dir[0], nc = c + dir[1];
if (nr >= 0 && nc >= 0 && nr < heights.length && nc < heights[0].length && !ocean[nr][nc] && heights[nr][nc] >= heights[r][c]) {
dfs(nr, nc, ocean, heights, directions);
}
}
}
}
Input: nums = [1,5,11,5]
Output: true
public class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) sum += num;
if (sum % 2 != 0) return false;
int target = sum / 2;
boolean[] dp = new boolean[target + 1];
dp[0] = true;
for (int num : nums) {
for (int j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
}
Input: num1 = "11", num2 = "123"
Output: "134"
import java.util.HashSet;
import java.util.Set;
public class Solution {
public int thirdMax(int[] nums) {
Integer first = null;
Integer second = null;
Integer third = null;
for (Integer num : nums) {
if (num.equals(first) || num.equals(second) || num.equals(third)) {
continue;
}
if (first == null || num > first) {
third = second;
second = first;
first = num;
} else if (second == null || num > second) {
third = second;
second = num;
} else if (third == null || num > third) {
third = num;
}
}
return third != null ? third : first;
}
}
Input: nums = [3,2,1]
Output: 1
import java.util.HashSet;
import java.util.Set;
public class Solution {
public int thirdMax(int[] nums) {
Integer first = null;
Integer second = null;
Integer third = null;
for (Integer num : nums) {
if (num.equals(first) || num.equals(second) || num.equals(third)) {
continue;
}
if (first == null || num > first) {
third = second;
second = first;
first = num;
} else if (second == null || num > second) {
third = second;
second = num;
} else if (third == null || num > third) {
third = num;
}
}
return third != null ? third : first;
}
}
Input: nums = [1,2,3,4]
Output: 3
public class Solution {
public int numberOfArithmeticSlices(int[] nums) {
if (nums.length < 3) return 0;
int count = 0;
int currentLength = 0;
for (int i = 2; i < nums.length; i++) {
if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
currentLength++;
count += currentLength;
} else {
currentLength = 0;
}
}
return count;
}
}
Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
import java.util.ArrayList;
import java.util.List;
public class Solution {
public ListfizzBuzz(int n) {
Listanswer = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
answer.add("FizzBuzz");
} else if (i % 3 == 0) {
answer.add("Fizz");
} else if (i % 5 == 0) {
answer.add("Buzz");
} else {
answer.add(String.valueOf(i));
}
}
return answer;
}
}
Input: target = "apple", dictionary = ["blade"]
Output: "a4"
import java.util.HashSet;
import java.util.Set;
public class Solution {
public String minAbbreviation(String target, String[] dictionary) {
SettargetAbbrs = generateAbbreviations(target);
SetdictAbbrs = new HashSet<>();
for (String word : dictionary) {
dictAbbrs.addAll(generateAbbreviations(word));
}
targetAbbrs.removeAll(dictAbbrs);
return targetAbbrs.stream().min((a, b) -> Integer.compare(a.length(), b.length())).orElse("");
}
private SetgenerateAbbreviations(String word) {
Setresult = new HashSet<>();
generateAbbreviationsHelper(word.toCharArray(), "", 0, 0, result);
return result;
}
private void generateAbbreviationsHelper(char[] word, String current, int pos, int count, Setresult) {
if (pos == word.length) {
result.add(current + (count > 0 ? count : ""));
return;
}
generateAbbreviationsHelper(word, current, pos + 1, count + 1, result);
generateAbbreviationsHelper(word, current + (count > 0 ? count : "") + word[pos], pos + 1, 0, result);
}
}
Input: nums = [7,2,5,10,8], k = 2
Output: 18
public class Solution {
public int splitArray(int[] nums, int k) {
int left = 0, right = 0;
for (int num : nums) {
left = Math.max(left, num);
right += num;
}
while (left < right) {
int mid = left + (right - left) / 2;
if (canSplit(nums, k, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private boolean canSplit(int[] nums, int k, int maxSum) {
int currentSum = 0, subarrays = 1;
for (int num : nums) {
if (currentSum + num > maxSum) {
currentSum = num;
subarrays++;
if (subarrays > k) {
return false;
}
} else {
currentSum += num;
}
}
return true;
}
}