General Practice (I did it myself too haha)
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, Sum solution");
O(n? 2?? )
Public int[] Twosum (int[] nums, int target) {
Map<integer, integer> map = new hashmap<> ();
for (int i = 0; i < nums.length; i++) {
Map.put (Nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = Target-nums[i];
if (Map.containskey (complement) && map.get (complement)! = i) {
return new int[] {i, Map.get (complement)};
}
}
throw new IllegalArgumentException ("No, Sum solution");
}
This approach leverages the characteristics of the map without double loops, reducing the complexity of O (n)
This is more absolute ... Go forward and look back!!
Public int[] Twosum (int[] nums, int target) {
Map<integer, integer> map = new hashmap<> ();
for (int i = 0; i < nums.length; i++) {
int complement = Target-nums[i];
if (Map.containskey (complement)) {
return new int[] {map.get (complement), i};
}
Map.put (Nums[i], i);
}
throw new IllegalArgumentException ("No, Sum solution");
}
Hashcode is the value of an int of the JDK based on the address of the object or the string or number
In Java, a hash code represents the characteristics of an object. For example object String str1 = "AA", str1.hashcode= 3104String str2 = "BB", str2.hashcode= 3106String str3 = "AA", str3.hashcode= 3104 According to hashcode this can be obtained STR1!=STR2,STR1==STR3 below gives a few commonly used hash code algorithm. The hashcode of the 1:object class. Returns the memory address of the object after the processing of the structure, because each object's memory address is different, so the hash code is not the same. The hashcode of the 2:string class. Based on the contents of the string contained in the strings class, the hash code is returned according to a special algorithm, as long as the string is in the same heap space as the hash code returned. In the 3:integer class, the hash code returned is the numeric value of the integer contained in the integer object, such as the integer I1=new integer, and the value of I1.hashcode is 100. This shows that 2 of the same size of an integer object, the same hash code is returned.
The Map collection allows the value object to be null, and there is no number limit, so when the return value of the Get () method is null, there may be two cases where the key object is not in the collection, and the key object does not map any value objects, that is, the value object is null. Therefore, the get (Object key) method should not be used in the Map collection to determine if a key exists, but the ContainsKey () method should be used to determine whether the ContainsKey method is to determine if the Map collection object contains the specified key name.
Syntax Boolean containskey (Object key)
Return value: Returns TRUE if the Map collection contains the specified key name, otherwise false.
Parameter: Key is the name object of the map collection to query.
Get method
Also, when key is NULL, special processing is performed, and a null-key element is found on the linked list of table[0]
Get is the process of calculating the hash and then calculating the index value by hashing and table.length, then traversing the list on Table[index] until key is found, and then returns
Given an array and a target value, if any of the two numbers in the array are equal to the target value, output both positions