1 Arrays
PS: Arrays is under the java.util package
1 int binarySearch(type[] a, type key);
Bisection queries the index of the key element in an array, and returns a negative number if the array does not contain this value.This array is required to be in ascending order before use to get the correct result.
1 int binarySearch(type[] a, int fromIndex, int toIndex, type key);
Similar to above, but only looking for elements from fromIndex to toIndex requires the array to be in ascending order.
1 type[] copyOf(type[] originla , int length);
Copy an array, length is the length of the new array.If the length is less than the original array, only the value of the length of the previous length is copied; if it is greater than the original array, then 0, false, null are added after it.
1 type[] copyOf(type[] originla , int from, int to);
Similar to the previous one, but only copy elements within the specified range.
1 boolean equals(type[] a1 , type[] a2)
Returns true if a1 and a2 are equal in length and have the same elements, otherwise false.
1 void fill(type[] a , type val)
This method assigns all elements of an array to a val ue.
1 void fill(type[] , int fromIndex , int toIndex, type val)
Similar to the above method, the scope is specified.
1 void sort(type[] a)
This method sorts the elements of an array in ascending order.In particular, if a is a string array, it is sorted in ascending order by the length of the string.
1 void sort(type[] a , int fromIndex int toIndex)
Similarly, sort only within a range.
1 String toString(type[] a)
Converts an array to a string, which sequentially concatenates multiple array elements into a string, separating each element with a comma and a space and enclosing'[',']'outside.
Arrays enhancements after 2 Java 8
1 void parallelPrefix(xxx[] array, XxxBinaryOperator op) 2 void parallelPrefix(xxx[] array , int fromIndex , toIndex , XxxBinaryOperator op)
This method uses the result of the calculation formula specified by the op parameter as a new array element.The OP calculation formula includes two parameters, left and right, where left represents the element at the previous index of money in the new array and right represents the element at the current index in the array.The first element of the new array does not need to be calculated, it is directly equal to the first element of the array.
1 void setAll(xxx[] array , IntToXxxFunction generator)
This method sets values for all array elements with the specified generator, which controls the algorithm for generating the values of the array elements.
1 void parallelSetAll(xxx[] array , IntToXxxFunction generator)
Similar to the above approach, increased parallelism and improved performance with multiple cpUs
1 void parallelSort (xxx[] a) 2 void parallelSort (xxx[] a , int fromIndex , int toIndex)
Similar to the sort method in Arrays, it adds parallelism and can take advantage of multiple cpUs to improve performance.
Example programs for enhancements:
1 import java.util.Arrays; 2 import java.util.function.IntBinaryOperator; 3 import java.util.function.IntUnaryOperator; 4 public class ArraysTest { 5 public static void main(String[] args) { 6 int[] array1 = new int[] {3, -4, 25, 16, 30, 18}; 7 //use Arrays Method ordering of classes 8 Arrays.parallelSort(array1); 9 System.out.println(Arrays.toString(array1)); 10 int[] array2 = new int[] {3, -4, 25, 16, 30, 18}; 11 //The element of the previous index is multiplied by the current element to get a new array,Need import java.util.function.IntBinaryOperator 12 Arrays.parallelPrefix(array2, new IntBinaryOperator() { 13 public int applyAsInt(int left , int right) { 14 return left * right; 15 } 16 }); 17 System.out.println(Arrays.toString(array2)); 18 int array3[] = new int[5]; 19 //Use current index*5 Method generates elements that require import java.util.function.IntUnaryOperator 20 Arrays.parallelSetAll(array3, new IntUnaryOperator() { 21 //operand Is the current index 22 public int applyAsInt(int operand) { 23 return operand * 5; 24 } 25 }); 26 System.out.println(Arrays.toString(array3)); 27 } 28 }
Output results:
3-D Array
3.1 2-D Array Principle
Because array elements can be of reference type, you can have arrays as elements of arrays, resulting in two-dimensional arrays.Two-dimensional arrays in java do not require a matrix form.
3.2 Defining a two-dimensional array
1 type[][] arrayName;
It is still a one-dimensional array in essence, but the elements of the array are also references.
3.3 Initialization
1 arrayName = new type[length][]
This is equivalent to defining length variables of type [].Next proceed with initialization
1 arrayName[0] = new type[2]
Here, an array of length 2 is assigned to the first element of the previously defined one-dimensional array, where the values of arrayName[0][0] and arrayName[0][1] are both 0 (if the type is int), and the other elements of the one-dimensional array are null;
A diagram of an initialized two-dimensional array in memory:
3.4 Common Initialization Methods
- Define two dimensions of a two-dimensional array at the same time (rectangular array)
1 int[][] arrayName = new int[3][4]
- Display a defined (static) two-dimensional array (not necessarily a rectangle)
1 int [][] a = {{1,2},{3,4,0,9},{5,6,7}}; 2 perhaps 3 String[][] str1 = new String[][]{new String[3] , new String[]{"Hello"}} 4 Simplified version 5 String[][] str2 = {new String[3] , new String[]{"Hello"}};
4 LeetCode Exercise
13. Roman Number to Integer
Roman numerals contain the following seven characters: I, V, X, L, C, D and M.
character numerical value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, the Roman number 2 is written as II, which is two side-by-side 1.12 Write XII, which is X + II.27 Write XXVII, that is XX + V + II.
Normally, small numbers in Roman numbers are to the right of large numbers.But there are exceptions, such as 4 not written as IIII, but IV.Number 1 is on the left side of number 5 and represents the number equal to the value 4 obtained by the large number 5 and the small number 1.Similarly, the number 9 is represented as IX.This special rule applies only to the following six cases:
- I can be placed to the left of V (5) and X (10) to represent 4 and 9.
- X can be placed to the left of L (50) and C (100) to represent 40 and 90.
- C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Given a Roman number, convert it to an integer.Entry is guaranteed to range from 1 to 3999.
Solving ideas:
Following a logic, if a character represents a value greater than or equal to the value represented by its next character, it is added; if it is less than the value represented by its next character, it is subtracted.Note that the last character has no "next", it must be plus.
Source code:
1 class Solution { 2 public int romanToInt(String s) { 3 char[] ch = s.toCharArray(); 4 int result = 0; 5 for(int i = 0; i < ch.length; i++){ 6 if(i == ch.length - 1){ 7 result += getNum(ch[i]); 8 }else if (getNum(ch[i]) >= getNum(ch[i+1])){ 9 result += getNum(ch[i]); 10 }else{ 11 result -= getNum(ch[i]); 12 } 13 } 14 return result; 15 } 16 17 public static int getNum(char tmp){ 18 switch(tmp){ 19 case 'M': 20 return 1000; 21 case 'D': 22 return 500; 23 case 'C': 24 return 100; 25 case 'L': 26 return 50; 27 case 'X': 28 return 10; 29 case 'V': 30 return 5; 31 case 'I': 32 return 1; 33 } 34 return 0; 35 } 36 }
14. Longest common prefix
Write a function to find the longest common prefix in a string array.
Returns the empty string''if no public prefix exists.
Source code:
1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 String commonPrefix = ""; 4 int length = ~0>>>1; 5 if(strs.length == 0) 6 return commonPrefix; 7 for (int i = 0; i < strs.length; i++){ 8 if(strs[i].length() < length) 9 length = strs[i].length(); 10 }//Find the shortest character length 11 /*This method can be used to find the shortest length 12 Arrays.sort(strs);//Arrange by length 13 int len=Math.min(strs[0].length(),strs[strs.length-1].length()); 14 */ 15 HashMap<Integer , Character> map = new HashMap<Integer , Character>(); 16 for(int i = 0; i < strs[0].length();i++){ 17 map.put(i , strs[0].charAt(i)); 18 }//Add each character of the first string hashmap 19 outer: 20 for(int i =0; i < length; i++){ 21 for(int j = 0; j < strs.length; j++){ 22 if(map.get(i) == strs[j].charAt(i)){} 23 else{ 24 break outer; 25 }//Exit the loop if the character does not match that position 26 }//Add this position character to the result every time a loop is completed 27 commonPrefix += strs[0].charAt(i); 28 } 29 return commonPrefix; 30 } 31 }