Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
📢 introduction
- 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻
- 🌲 from today on, punch in an algorithm every day, both a learning process, but also a process of sharing 😜
- 🌲 tip: the programming languages used in this column are C# and Java
- 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
- 🌲 today is the buckle algorithm continued to punch card day 1 🎈!
- 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻 🌻
🌲 Example of original problem
Topic:
- Given an integer array nums and an integer target value target, find the two integers in the array and the target value target and return their array subscripts.
- You can assume that there is only one answer for each type of input. However, the same element in the array cannot be repeated in the answer.
- You can return the answers in any order.
Example 1: input: nums = [2,7,11,15], target = 9 output: [0,1] description: because nums[0] + nums[1] == 9, return [0,1].
Example 2:
Input: nums = [3,2,4], target = 6
Example 3:
Input: nums = [3,3], target = 6
Tip:
- 2 <= nums.length <= 104
- -109 <= nums[i] <= 109
- -109 <= target <= 109
- There can only be one valid answer
🌞
🌻C# method 1: violence method
If the sum is target, return the index values of the two values
// Method 1:
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[i] + nums[j] == target)
{
return new int[] { i, j }; }}}return new int[] { 0.0 };
}
Copy the code
Result the execution result is successful. The execution time is 480ms, and the memory consumption is 29.6MB.
Complexity analysis Time complexity: O(n^2) Space complexity: O(1)
🌻C# method two: hash
Solution: Use hash table, save the detailed traversal: through the key-value pair to save the relationship between the array value and index, in the later search value (find target-nums[I]) can be found in one step, save the time to traverse the number group again.
public class Solution {
/ / method 2
public int[] TwoSum(int[] nums, int target) {
int[] result = new int[2];
Dictionary<int.int> dic = new Dictionary<int.int> ();for (int i = 0; i < nums.Length; i++) {
if (dic.ContainsKey(nums[i]))
{
dic[nums[i]] += 1;
}
else {
dic[nums[i]] = 0; }}for (int i = 0; i < nums.Length; i++)
{
if (nums.Contains(target - nums[i]))
{
result[0] = i;
result[1] = nums.ToList().FindIndex(item => item == target - nums[i]);
if (result[0] != result[1]) { break; }}}returnresult; }}Copy the code
Result Time: 272 ms; Memory consumption: 32.4 MB
🎋Java Method 1: Violence enumeration
Ideas and Algorithms
The easiest way to think about it is to enumerate each number x in the array and find if target-x exists in the array.
When we look for target-x by walking through the array, we notice that every element before x has already been matched to x, so we don’t need to match any more. Each element can’t be used twice, so we just look for target-x in the element after x.
code
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j}; }}}return new int[0]; }}Copy the code
Result Time: 51 ms. Memory consumption: 38.6 MB
Complexity analysis Time complexity: O(N^2)O(N 2), where NN is the number of elements in the array. In the worst case, any two numbers in the array must be matched once. Space complexity: O(1)O(1).
🎋Java Method 2: Hash table
Ideas and Algorithms
Notice that the time complexity of method 1 is higher because the time complexity of finding target-x is too high. Therefore, we need a better way to quickly find out if there is a target element in an array. If it exists, we need to find its index.
Using a hash table, you can reduce the time complexity of finding target-x from O(N)O(N) to O(1)O(1).
So we create a hash table, and for each x, we first check if target-x exists in the hash table, and then we insert x into the hash table to ensure that x doesn’t match us.
code
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0]; }}Copy the code
Result Execution time: 2 ms. Memory consumption: 38.6 MB
Complexity analysis Time complexity: O(N)O(N), where NN is the number of elements in the array. For each element x, we can find target-x O(1)O(1). Space complexity: O(N)O(N), where NN is the number of elements in the array. Mainly the overhead of the hash table.
💬 summary
- Today is the first day of buckle algorithm punch card, just started some strange, behind will be more and more skilled!
- This paper uses C# and Java programming languages to solve the problem
- Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
- That’s the end of today’s algorithm sharing, see you tomorrow!