Skip to content

Commit a4ca455

Browse files
committed
Some Code
1 parent b06d359 commit a4ca455

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Candy.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
There are N children standing in a line. Each child is assigned a rating value.
2+
3+
You are giving candies to these children subjected to the following requirements:
4+
5+
Each child must have at least one candy.
6+
Children with a higher rating get more candies than their neighbors.
7+
8+
What is the minimum candies you must give?
9+
10+
public class Solution {
11+
public int candy(int[] ratings) {
12+
int[] A = new int[ratings.length];
13+
for (int i = 0; i < A.length; i++) {
14+
A[i] = 1;
15+
}
16+
for (int j = 1; j < A.length; j++) {
17+
check(A, ratings, j);
18+
}
19+
int sum = 0;
20+
for (int k = 0; k < A.length; k++) {
21+
sum += A[k];
22+
}
23+
return sum;
24+
}
25+
26+
private void check(int[] plan, int[] ratings, int start) {
27+
while (start > 0) {
28+
if (ratings[start - 1] > ratings[start] && plan[start - 1] <= plan[start]) {
29+
plan[start - 1] = plan[start] + 1;
30+
} else if (ratings[start - 1] < ratings[start] && plan[start - 1] >= plan[start]) {
31+
plan[start] = plan[start - 1] + 1;
32+
} else {
33+
return;
34+
}
35+
start--;
36+
}
37+
}
38+
}

Copy List with Random Pointer.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
2+
3+
Return a deep copy of the list.
4+
5+
/**
6+
* Definition for singly-linked list with a random pointer.
7+
* class RandomListNode {
8+
* int label;
9+
* RandomListNode next, random;
10+
* RandomListNode(int x) { this.label = x; }
11+
* };
12+
*/
13+
public class Solution {
14+
public RandomListNode copyRandomList(RandomListNode head) {
15+
if (head == null) return head;
16+
RandomListNode cur = head;
17+
while (cur != null) {
18+
RandomListNode newNode = new RandomListNode(cur.label);
19+
newNode.next = cur.next;
20+
cur.next = newNode;
21+
cur = newNode.next;
22+
}
23+
cur = head;
24+
while (cur != null) {
25+
if (cur.random != null) {
26+
cur.next.random = cur.random.next;
27+
}
28+
cur = cur.next.next;
29+
}
30+
cur = head.next;
31+
RandomListNode newHead = cur;
32+
RandomListNode old = head;
33+
while (old != null) {
34+
old.next = old.next.next;
35+
if (old.next != null) {
36+
cur.next = old.next.next;
37+
}
38+
cur = cur.next;
39+
old = old.next;
40+
}
41+
return newHead;
42+
}
43+
}

Single Number.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Given an array of integers, every element appears twice except for one. Find that single one.
2+
3+
Note:
4+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
5+
6+
public class Solution {
7+
public int singleNumber(int[] A) {
8+
int ret = A[0];
9+
for (int i = 1; i < A.length; i++) {
10+
ret ^= A[i];
11+
}
12+
return ret;
13+
}
14+
}

0 commit comments

Comments
 (0)