Skip to content

Commit 2e5a583

Browse files
add solution for leetcode 26
1 parent c9f5fe0 commit 2e5a583

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ LeetCode
386386
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)| |Medium|
387387
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| |Easy|
388388
|27|[Remove Element](https://leetcode.com/problems/remove-element/)| [js](./algorithms/removeElement/removeElement.js) [python](./algorithms/removeElement/removeElement.python) |Easy|
389-
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| |Easy|
389+
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [java](./algorithms/Remove Duplicates From Sorted Array/Soultion.java) |Easy|
390390
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| |Hard|
391391
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| |Medium|
392392
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| |Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int j = 0;
4+
for(int i = 1; i<nums.length; i++){
5+
if(nums[j]!=nums[i]){
6+
j++;
7+
nums[j] = nums[i];
8+
}
9+
}
10+
return j+1;
11+
}
12+
}

algorithms/twoSum/twoSum.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
public int[] twoSum(int[] nums, int target) {
2-
Map<Integer, Integer> numsIndexMap = new HashMap();
3-
for (int i = 0; i < nums.length; i++) {
4-
if (numsIndexMap.containsKey(target - nums[i])) {
5-
return new int[]{numsIndexMap.get(target - nums[i]), i};
1+
import java.util.Map;
2+
3+
public class TwoSum {
4+
public int[] twoSum(int[] nums, int target) {
5+
Map<Integer, Integer> numsIndexMap = new HashMap();
6+
for (int i = 0; i < nums.length; i++) {
7+
if (numsIndexMap.containsKey(target - nums[i])) {
8+
return new int[]{numsIndexMap.get(target - nums[i]), i};
9+
}
10+
numsIndexMap.put(nums[i], i);
611
}
7-
numsIndexMap.put(nums[i], i);
12+
throw new IllegalArgumentException();
813
}
9-
throw new IllegalArgumentException();
1014
}
15+

0 commit comments

Comments
 (0)