File tree Expand file tree Collapse file tree 3 files changed +95
-0
lines changed Expand file tree Collapse file tree 3 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments