We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个整数数组(无重复元素)和一个目标值,找出数组中和为目标值的两个数
Input: numbers={2, 7, 11, 15}, target=9 Output: {2, 7}
i+1
O(n^2)
end—
start++
public static int[] twoSum(int[] nums, int target) { int[] result = new int[2]; int start = 0, end = nums.length - 1; // 1. 对数组进行排序 Arrays.sort(nums); while (start < end) { if (nums[start] + nums[end] == target) { result[0] = nums[start]; result[1] = nums[end]; break; } // 小于目标值,前指针后移 if (nums[start] + nums[end] < target) { start++; } // 大于目标值,后指针前移 if (nums[start] + nums[end] > target) { end--; } } return result; }
排序:O(nlogn),两根指针算法O(n)
O(nlogn)
O(n)
O(nlogn) + O(n) = O(nlogn)
给定一个包含n个整数的数组(无重复元素)nums和一个目标值target,找出数组中和为目标值的三个数
input: nums = [-1, 0, 1, 2, 4], target = 0 output: [-1, 0, 1]
public static int[] threeSum(int[] nums, int target) { int[] result = new int[3]; if (nums.length < 3) { return nums; } Arrays.sort(nums); // 第三个指针 int second = nums.length - 1; // 遍历数组 for (int i = 0; i < nums.length - 2; i++) { // 第二个指针 int first = i + 1; while (first < second) { if (nums[first] + nums[second] == target - nums[i]) { result[0] = nums[i]; result[1] = nums[first]; result[2] = nums[second]; return result; } if (nums[second] + nums[first] < target - nums[i]) { first++; } if (nums[second] + nums[first] > target - nums[i]) { second--; } } } return result; }
排序: nlogn 遍历n个数,进行2数之和(n*O(n))
O(nlogn)+n*O(n) = O(n^2)
给定一个数组,反转数组中所有的数字
Input: {1,2,3,45,6,7} output: {7,6,45,3,2,1}
public static void swap(int start, int end, int[] nums) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; } public static int[] reserveArray(int[] nums) { int start = 0, end = nums.length - 1; while (start < end) { // 交换完成更新索引 swap(start++, end--, nums); } return nums; }
给定一组整数,对它们进行排序,以便所有的奇数整数在偶数整数之前出现,元素的顺序可以改变。排序的奇数和偶数的顺序无关紧要
public static int[] oddEvenSort(int[] nums) { int start = 0, end = nums.length - 1; while (start < end) { // start 为奇数, 就偏移指针只到为偶数 while (start < end && nums[start] % 2 != 0) { start++; } // end 为偶数 while (start < end && nums[end] % 2 == 0) { end--; } if(start < end ) { swap(start++, end--, nums); } } return nums; } public static void swap(int start, int end, int[] nums) { int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; }
start < end
while (start < end && nums[start] % 2 != 0)
给定两个有序整数数组num1和num2 , 请按照递增顺序将他们合并到一个排序数组中
intput: {1,3, 5} {2,4,6} output: {1,2,3,4,5,6}
public static int[] merge(int[] num1, int[] num2) { int[] result = new int[num1.length + num2.length]; int index = 0, index1 = 0, index2 = 0; // 判定index1 index2必须要在num1 和 num2 while (index1 < num1.length && index2 < num2.length) { if (num1[index1] < num2[index2]) { result[index++] = num1[index1++]; } else { result[index++] = num2[index2++]; } } for (int i = index1; i < num1.length; i++) { result[index++] = num1[i]; } for (int i = index2; i < num2.length; i++) { result[index++] = num2[i]; } return result; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
数组
注意点
常见面试题
1. 两数之和
给定一个整数数组(无重复元素)和一个目标值,找出数组中和为目标值的两个数
暴力解决
i+1
取2个数之和O(n^2)
正确解法 (排序+双指针)
end—
),反之则表明现在两数过小,应使start指针指向更大的数,即索引增加(start++
)时间复杂度 O(nlogn)
排序:
O(nlogn)
,两根指针算法O(n)
O(nlogn) + O(n) = O(nlogn)
2. 三数之和
给定一个包含n个整数的数组(无重复元素)nums和一个目标值target,找出数组中和为目标值的三个数
解法(排序+三指针)
时间复杂度(O(n^2)
排序: nlogn 遍历n个数,进行2数之和(n*O(n))
O(nlogn)+n*O(n) = O(n^2)
3. 反转数组
给定一个数组,反转数组中所有的数字
解法(双指针)
时间复杂度 O(n)
4. 奇数偶数排序
给定一组整数,对它们进行排序,以便所有的奇数整数在偶数整数之前出现,元素的顺序可以改变。排序的奇数和偶数的顺序无关紧要
解法(双指针)
易错点
start < end
的时候,内部也需要判定最基本的条件while (start < end && nums[start] % 2 != 0)
5. 合并两个有序数组
给定两个有序整数数组num1和num2 , 请按照递增顺序将他们合并到一个排序数组中
解法(双指针+同方向)
The text was updated successfully, but these errors were encountered: