Цена за 48 часов в ленте | 1000,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: root = [2,1,3]
Output: 1
public class Solution {
private int maxDepth = -1;
private int bottomLeftValue = 0;
public int FindBottomLeftValue(TreeNode root) {
Dfs(root, 0);
return bottomLeftValue;
}
private void Dfs(TreeNode current, int depth) {
if (current == null) return;
if (depth > maxDepth) {
maxDepth = depth;
bottomLeftValue = current.val;
}
Dfs(current.left, depth + 1);
Dfs(current.right, depth + 1);
}
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
public class Solution {
public int FindIntegers(int num) {
int count = 0;
for (int i = 0; i <= num; i++) {
if (Check(i)) {
count++;
}
}
return count;
}
public bool Check(int n) {
int i = 31;
while (i > 0) {
if ((n & (1 << i)) != 0 && (n & (1 << (i - 1))) != 0) {
return false;
}
i--;
}
return true;
}
}
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
using System;
using System.Collections.Generic;
public class Solution {
public string[] FindRestaurant(string[] list1, string[] list2) {
var map = new Dictionary>();
for (int i = 0; i < list1.Length; i++) {
for (int j = 0; j < list2.Length; j++) {
if (list1[i] == list2[j]) {
if (!map.ContainsKey(i + j)) {
map[i + j] = new List();
}
map[i + j].Add(list1[i]);
}
}
}
int minIndexSum = int.MaxValue;
foreach (var key in map.Keys) {
minIndexSum = Math.Min(minIndexSum, key);
}
return map[minIndexSum].ToArray();
}
}
Input: nums = [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
using System;
using System.Collections.Generic;
public class Solution {
public int FindLHS(int[] nums) {
var count = new Dictionary();
int res = 0;
foreach (var num in nums) {
if (count.ContainsKey(num)) {
count[num]++;
} else {
count[num] = 1;
}
if (count.ContainsKey(num + 1)) {
res = Math.Max(res, count[num] + count[num + 1]);
}
if (count.ContainsKey(num - 1)) {
res = Math.Max(res, count[num] + count[num - 1]);
}
}
return res;
}
}
Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.
public class Solution {
public int MaxCount(int m, int n, int[][] ops) {
int minA = m;
int minB = n;
foreach (var op in ops) {
minA = Math.Min(minA, op[0]);
minB = Math.Min(minB, op[1]);
}
return minA * minB;
}
}
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
public class Solution {
private int Dist(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}
private bool 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 bool 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"
using System;
using System.Collections.Generic;
public class Solution {
public string FractionAddition(string expression) {
var sign = new List();
if (expression[0] != '-') sign.Add('+');
foreach (char ch in expression) {
if (ch == '+' || ch == '-') sign.Add(ch);
}
int prevNum = 0, prevDen = 1, i = 0;
var fractions = expression.Replace("-", "+-").Split(new[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var sub in fractions) {
var fraction = sub.Split('/');
int num = int.Parse(fraction[0]);
int den = int.Parse(fraction[1]);
int g = Math.Abs(Gcd(prevDen, den));
if (sign[i++] == '+') prevNum = prevNum * den / g + num * prevDen / g;
else prevNum = prevNum * den / g - num * prevDen / g;
prevDen = prevDen * den / g;
g = Math.Abs(Gcd(prevDen, prevNum));
prevNum /= g;
prevDen /= g;
}
return $"{prevNum}/{prevDen}";
}
private 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]
using System.Collections.Generic;
public class Node {
public int val;
public IListchildren;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, IList_children) {
val = _val;
children = _children;
}
}
public class Solution {
public IListPostorder(Node root) {
LinkedListoutput = new LinkedList ();
if (root == null) {
return output;
}
Stackstack = new Stack ();
stack.Push(root);
while (stack.Count > 0) {
Node node = stack.Pop();
output.AddFirst(node.val);
foreach (Node child in node.children) {
if (child != null) {
stack.Push(child);
}
}
}
return new List(output);
}
}
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
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public string Swap(string s, int i0, int i1) {
if (i0 == i1) return s;
var chars = s.ToCharArray();
var temp = chars[i0];
chars[i0] = chars[i1];
chars[i1] = temp;
return new string(chars);
}
private Listlist = new List ();
private void Permute(string a, int l, int r) {
if (l == r) {
list.Add(a);
} else {
for (int 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.ToString();
Permute(s, 0, s.Length - 1);
list.Sort();
int index = list.IndexOf(s);
if (index != -1 && index < list.Count - 1) {
int result = int.Parse(list[index + 1]);
if (result <= int.MaxValue) return result;
}
return -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(IList> wall) {
int[] pos = new int[wall.Count];
int sum = 0, res = int.MaxValue;
foreach (int el in wall[0])
sum += el;
while (sum != 0) {
int count = 0;
for (int i = 0; i < wall.Count; i++) {
if (wall[i][pos[i]] != 0)
count++;
else
pos[i]++;
wall[i][pos[i]]--;
}
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;
bool hasLower = false, hasUpper = false, hasDigit = false;
int repeatCount = 0;
for (int i = 0; i < n;) {
if (char.IsLower(s[i])) hasLower = true;
if (char.IsUpper(s[i])) hasUpper = true;
if (char.IsDigit(s[i])) hasDigit = true;
int start = i;
while (i < n && s[i] == s[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[i] == s[i - 1] && s[i] == s[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
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
private ListAddStrings(List num1, List num2) {
var ans = new List();
int carry = 0;
int n1 = num1.Count;
int n2 = num2.Count;
for (int i = 0; i < Math.Max(n1, n2) + 1; ++i) {
int digit1 = i < n1 ? num1[i] : 0;
int digit2 = i < n2 ? num2[i] : 0;
int sum = digit1 + digit2 + carry;
carry = sum / 10;
ans.Add(sum % 10);
}
return ans;
}
private ListMultiplyOneDigit(string firstNumber, char secondNumberDigit, int numZeros) {
var currentResult = new List(new int[numZeros]);
int carry = 0;
foreach (char digit in firstNumber) {
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 == "0" || secondNumber == "0") {
return "0";
}
firstNumber = new string(firstNumber.Reverse().ToArray());
secondNumber = new string(secondNumber.Reverse().ToArray());
var ans = new List(new int[firstNumber.Length + secondNumber.Length]);
for (int i = 0; i < secondNumber.Length; ++i) {
ans = AddStrings(MultiplyOneDigit(firstNumber, secondNumber[i], i), ans);
}
while (ans.Last() == 0) {
ans.RemoveAt(ans.Count - 1);
}
var answer = new System.Text.StringBuilder();
for (int i = ans.Count - 1; i >= 0; --i) {
answer.Append(ans[i]);
}
return answer.ToString();
}
}