标签:element exception size == strong exce array not throw
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
Example:
public int[] twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[j] == target - nums[i]) { return new int[] { i, j }; } } } throw new IllegalArgumentException("No two sum solution"); }
class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { vector<int> result; int N = nums.size(); for (int i = 0; i < N - 1; i++) { for (int j = i+1; j < N; j++) { if (numbers[i] + numbers[j] == target) { result.push_back(i+1); result.push_back(j+1); return result; } } } return result; } };
标签:element exception size == strong exce array not throw
原文地址:http://www.cnblogs.com/hozhangel/p/7726359.html