diff --git a/contains-duplicate/pastelto.java b/contains-duplicate/pastelto.java new file mode 100644 index 000000000..461824e26 --- /dev/null +++ b/contains-duplicate/pastelto.java @@ -0,0 +1,14 @@ +/* + Return true when array contains duplicated number + */ +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet set = new HashSet<>(); + + for (int num : nums) { + if (set.contains(num)) return true; + set.add(num); + } + return false; + } +} diff --git a/two-sum/pastelto.java b/two-sum/pastelto.java new file mode 100644 index 000000000..eb0d05138 --- /dev/null +++ b/two-sum/pastelto.java @@ -0,0 +1,12 @@ +class Solution { + public int[] twoSum(int[] nums, int target) { + for (int i = 0; i < nums.length - 1; i++) { + for (int j = i+1; j < nums.length; j++) { + if (nums[i] + nums[j] == target) { + return new int[]{i, j}; + } + } + } + return new int[]{}; + } +}