-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/jutem/cases/java8/lambda/ToListCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.jutem.cases.java8.lambda; | ||
|
||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
/** | ||
* @author [email protected] | ||
* @create 2019-09-28 | ||
*/ | ||
public class ToListCollector implements Collector{ | ||
/** | ||
* @return | ||
*/ | ||
@Override | ||
public Supplier supplier() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public BiConsumer accumulator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public BinaryOperator combiner() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Function finisher() { | ||
return Function.identity(); | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() { | ||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/jutem/cases/leetcode/DeleteNodeInALinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.jutem.cases.leetcode; | ||
|
||
/** | ||
* @author [email protected] | ||
* @create 2020-09-13 | ||
*/ | ||
public class DeleteNodeInALinkedList { | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public void deleteNode(ListNode node) { | ||
node.val = node.next.val; | ||
node.next = node.next.next; | ||
} | ||
|
||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
ListNode(int x) { | ||
val = x; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.jutem.cases.leetcode; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class TwoSum { | ||
public static void main(String[] args) { | ||
twoSum(new int[]{3,2,4}, 6); | ||
} | ||
|
||
public static int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
Integer index = map.get(nums[i]); | ||
if(index == null || i > index) { | ||
map.put(nums[i], i); | ||
} | ||
} | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int least = target - nums[i]; | ||
Integer index = map.get(least); | ||
if(index != null && i != index) { | ||
return new int[]{i, index}; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private int[] firstSolution(int[] nums, int target) { | ||
int[] cal = Arrays.copyOf(nums, nums.length); | ||
Arrays.sort(cal); | ||
for(int head = 0, tail = nums.length - 1; head != tail;) { | ||
int sum = cal[head] + cal[tail]; | ||
if(sum == target) { | ||
return new int[]{indexFirst(nums, cal[head]), indexLast(nums, cal[tail])}; | ||
} | ||
if(sum > target) { | ||
tail --; | ||
} else { | ||
head ++; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private int indexLast(int[] nums, int num) { | ||
for(int i = nums.length - 1; i >= 0; i --) { | ||
if(nums[i] == num) { | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private int indexFirst(int[] nums, int num) { | ||
for(int i = 0; i < nums.length; i ++) { | ||
if(nums[i] == num) { | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters