public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) { // write your code int length = A.size(); ArrayList<Long> result = new ArrayList<>(); if (length < 1) return result; long tmp = 1; for (int i = 0; i < length; i++) { result.add(tmp); tmp *= A.get(i); } tmp = 1; for (int i = length - 1; i >= 0; i--) { Long data = result.get(i); result.set(i, data * tmp); tmp *= A.get(i); } return result; }
习题地址 Product of Array Exclude Itself
First Missing Positive - medium
题目
1 2 3 4 5
Given an unsorted integer array, find thefirst missing positive integer.
Example Given [1,2,0] return3, and [3,4,-1,1] return2.
public int firstMissingPositive(int[] A) { // write your code here intlength = A.length; if (length < 1) return1; int i = 0; while (i < length) { if (A[i] >= 0 && A[i] < length && A[A[i]] != A[i]) swap(A, i, A[i]); else i++; } int k = 1; while (k < length && A[k] == k) k++; if (length == 0 || k < length) return k; elsereturn A[0] == k? k + 1: k; }
public void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
习题地址 First Missing Positive
3Sum Closest - medium
题目
1 2 3 4 5 6
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.
Example For example, given array S = [-1 2 1 -4], and target = 1. The sum that is closest to the target is 2. (-1 2 1 = 2).
publicint threeSumClosest(int[] numbers, inttarget) { // write your code here int result = 0; if (numbers == null || numbers.length < 3) return result; intmin = Integer.MAX_VALUE; int i = 0; Arrays.sort(numbers); while (i < numbers.length - 2) { int j = i 1; int k = numbers.length - 1; while (j < k) { intsum = numbers[i] numbers[j] numbers[k]; if (sum == target) returnsum; elseif (sum < target) while (numbers[j] == numbers[++j] && j < k); elsewhile (numbers[k] == numbers[--k] && j < k); int diff = Math.abs(sum - target); if (diff < min) { min = diff; result = sum; } } while (numbers[i] == numbers[++i] && i < numbers.length - 2); } return result; }
习题地址 3Sum Closest
3Sum - medium
题目
1 2 3 4 5 6 7 8
Given an array S of n integers, are there elements a, b, c in S such that a b c = 0? Find all unique triplets in the array which gives the sum of zero.
Example For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:
publicArrayList<ArrayList<Integer>> threeSum(int[] numbers) { // write your code here ArrayList<ArrayList<Integer>> result = new ArrayList<>(); if (numbers == null || numbers.length < 3) return result; Arrays.sort(numbers); int i = 0; while (i < numbers.length - 2) { if (numbers[i] > 0) break; int j = i 1; int k = numbers.length - 1; while (j < k) { int sum = numbers[i] numbers[j] numbers[k]; if (sum == 0) { ArrayList<Integer> tmp = new ArrayList<>(); tmp.add(numbers[i]); tmp.add(numbers[j]); tmp.add(numbers[k]); result.add(tmp); } if (sum <= 0) while (numbers[j] == numbers[++j] && j < k); if (sum >= 0) while (numbers[k] == numbers[--k] && j < k); } while (numbers[i] == numbers[++i] && i < numbers.length - 2); }
return result; }
习题地址 3Sum
Two Sum - medium
题目
1 2 3 4 5 6 7 8 9 10 11
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.
Example numbers=[2, 7, 11, 15], target=9 return [1, 2]
publicint[] twoSum(int[] numbers, int target) { // write your code here if (numbers == null ||numbers.length < 1) returnnull; HashMap<Integer, Integer> hashMap = new HashMap<>(); int [] result = new int[2]; for (int i = 0; i < numbers.length; i++) { intkey = target - numbers[i]; if (hashMap.containsKey(key)) { result[0] = hashMap.get(key); result[1] = i + 1; break; } else { hashMap.put(numbers[i], i + 1); } } return result; }
习题地址 Two Sum
Partition Array - medium
题目
1 2 3 4 5 6 7 8 9
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:
All elements < k are moved to the left All elements >= k are moved to the right Return the partitioning index, i.e the first index i nums[i] >= k.
Example If nums = [3,2,2,1] and k=2, a valid answer is 1.