Цена за 48 часов в ленте | 2650,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
List> adj = new ArrayList<>(numCourses);
for (int i = 0; i < numCourses; i++) {
adj.add(new ArrayList<>());
}
for (int[] prerequisite : prerequisites) {
adj.get(prerequisite[1]).add(prerequisite[0]);
indegree[prerequisite[0]]++;
}
Queuequeue = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
queue.offer(i);
}
}
int nodesVisited = 0;
while (!queue.isEmpty()) {
int node = queue.poll();
nodesVisited++;
for (int neighbor : adj.get(node)) {
indegree[neighbor]--;
if (indegree[neighbor] == 0) {
queue.offer(neighbor);
}
}
}
return nodesVisited == numCourses;
}
}
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}
Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
class Solution {
public int countPrimes(int n) {
if (n <= 2) {
return 0;
}
boolean[] numbers = new boolean[n];
for (int p = 2; p <= (int) Math.sqrt(n); ++p) {
if (numbers[p] == false) {
for (int j = p * p; j < n; j += p) {
numbers[j] = true;
}
}
}
int numberOfPrimes = 0;
for (int i = 2; i < n; i++) {
if (numbers[i] == false) {
++numberOfPrimes;
}
}
return numberOfPrimes;
}
}
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode sentinel = new ListNode(0);
sentinel.next = head;
ListNode prev = sentinel, curr = head;
while (curr != null) {
if (curr.val == val) prev.next = curr.next;
else prev = curr;
curr = curr.next;
}
return sentinel.next;
}
}
Input: n = 2
Output: false
class Solution {
private int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
public boolean isHappy(int n) {
Setseen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getNext(n);
}
return n == 1;
}
}
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
class Solution {
private int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
public boolean isHappy(int n) {
Setseen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getNext(n);
}
return n == 1;
}
}
Input: left = 5, right = 7
Output: 4
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int shift = 0;
while (m < n) {
m >>= 1;
n >>= 1;
++shift;
}
return m << shift;
}
}
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
class Solution {
void dfs(char[][] grid, int r, int c) {
int nr = grid.length;
int nc = grid[0].length;
if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
return;
}
grid[r][c] = '0';
dfs(grid, r - 1, c);
dfs(grid, r + 1, c);
dfs(grid, r, c - 1);
dfs(grid, r, c + 1);
}
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
dfs(grid, r, c);
}
}
}
return num_islands;
}
}
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
class Solution {
public ListrightSideView(TreeNode root) {
if (root == null) return new ArrayList();
ArrayDequenextLevel = new ArrayDeque() {
{
offer(root);
}
};
ArrayDequecurrLevel = new ArrayDeque();
Listrightside = new ArrayList();
TreeNode node = null;
while (!nextLevel.isEmpty()) {
currLevel = nextLevel;
nextLevel = new ArrayDeque<>();
while (!currLevel.isEmpty()) {
node = currLevel.poll();
if (node.left != null) nextLevel.offer(node.left);
if (node.right != null) nextLevel.offer(node.right);
}
if (currLevel.isEmpty()) rightside.add(node.val);
}
return rightside;
}
}
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
class Solution {
private int[] memo;
public int rob(int[] nums) {
this.memo = new int[100];
Arrays.fill(this.memo, -1);
return this.robFrom(0, nums);
}
private int robFrom(int i, int[] nums) {
if (i >= nums.length) {
return 0;
}
if (this.memo[i] > -1) {
return this.memo[i];
}
int ans = Math.max(
this.robFrom(i + 1, nums),
this.robFrom(i + 2, nums) + nums[i]
);
this.memo[i] = ans;
return ans;
}
}
Input: n = 11
Output: 3
Explanation:
The input binary string 1011 has a total of three set bits.
public int hammingWeight(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
bits++;
}
mask <<= 1;
}
return bits;
}
Input: n = 00000010100101000001111010011100
Output: 964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Example 2:
class Solution {
private int reverseByte(int byteVal, Mapcache) {
if (cache.containsKey(byteVal)) {
return cache.get(byteVal);
}
int value = (int)(((byteVal * 0x0202020202L) & 0x010884422010L) % 1023);
cache.put(byteVal, value);
return value;
}
public int reverseBits(int n) {
int ret = 0;
int power = 24;
Mapcache = new HashMap<>();
while (n != 0) {
ret += reverseByte(n & 0xff, cache) << power;
n >>>= 8; // Use unsigned shift
power -= 8;
}
return ret;
}
}
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
class Solution {
public void rotate(int[] nums, int k) {
int[] a = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
a[(i + k) % nums.length] = nums[i];
}
for (int i = 0; i < nums.length; i++) {
nums[i] = a[i];
}
}
}
Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
public class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n <= 0 || k <= 0) {
return 0;
}
if (k * 2 >= n) {
int res = 0;
for (int i = 1; i < n; i++) {
res += Math.max(0, prices[i] - prices[i - 1]);
}
return res;
}
int[][][] dp = new int[n][k + 1][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j][0] = -1000000000;
dp[i][j][1] = -1000000000;
}
}
dp[0][0][0] = 0;
dp[0][1][1] = -prices[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j <= k; j++) { dp[i][j][0] = Math.max(
dp[i - 1][j][0],
dp[i - 1][j][1] + prices[i]
);
if (j > 0) {
dp[i][j][1] = Math.max(
dp[i - 1][j][1],
dp[i - 1][j - 1][0] - prices[i]
);
}
}
}
int res = 0;
for (int j = 0; j <= k; j++) {
res = Math.max(res, dp[n - 1][j][0]);
}
return res;
}
}
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC","CCCCCAAAAA"]
class Solution {
public ListfindRepeatedDnaSequences(String s) {
int L = 10, n = s.length();
if (n <= L) return new ArrayList();
int a = 4, aL = (int) Math.pow(a, L);
MaptoInt = new HashMap() {
{
put('A', 0);
put('C', 1);
put('G', 2);
put('T', 3);
}
};
int[] nums = new int[n];
for (int i = 0; i < n; ++i) nums[i] = toInt.get(s.charAt(i));
int h = 0;
Setseen = new HashSet();
Setoutput = new HashSet();
for (int start = 0; start < n - L + 1; ++start) {
if (start != 0) h = h * a -
nums[start - 1] * aL +
nums[start + L - 1];
else for (int i = 0; i < L; ++i) h = h * a + nums[i];
if (seen.contains(h)) output.add(s.substring(start, start + L));
seen.add(h);
}
return new ArrayList(output);
}
}
Input: s = ["a"]
Output: ["a"]
class Solution {
public void reverse(char[] s, int left, int right) {
while (left < right) {
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
public void reverseEachWord(char[] s) {
int n = s.length;
int start = 0, end = 0;
while (start < n) {
while (end < n && s[end] != ' ') ++end;
reverse(s, start, end - 1); start = end + 1;
++end;
}
}
public void reverseWords(char[] s) {
reverse(s, 0, s.length - 1);
reverseEachWord(s);
}
}