Abstract:The essence of algorithm practice is also to exercise programming thinking and strengthen the internal force of programmers. Therefore, give yourself will continue to update the algorithm skills content referred to as the algorithm by easy tendon.

This article is shared from Huawei Cloud Community “Java algorithm easy to use > common Java-API”, original author: Breakdraw.

The Yi Jin Jing originated from the guiding technique of ancient Chinese medicine, which has the effect of strengthening the body and preventing diseases, and has been widely circulated among Buddhism and folk martial arts practitioners for a long time. The essence of algorithm practice is also to exercise programming thinking and strengthen the internal force of programmers. Therefore, give yourself will continue to update the algorithm skills content referred to as the algorithm by easy tendon.

No matter what language you start training an algorithm in, you always have to master the basics. I’ll just use Java as an example here, other languages are similar. LeetCode type platforms are the main ones.

Java arrays and lists intersect

Sometimes the input is an array, and in the middle of that process we want to convert to a list and use some API for a list. But it’s not that simple.

Open your own compiler and see if the following problems work without a for loop. (It is also true to use the for loop, if you forget it during the exam, choose the method you can use)

  • Please change the array of strings to list
  • Please convert the string list into an array
  • Please change list

    to int[].
  • Please convert int[] to List

Make a class of your own and test it to see if you can write it in seconds like this:

Public class ListArrayUtil {public list <String> arrtoListStr (String[] arr) {return null; } public String[] listToArrStr(List<String> List) {return null; } public list <Integer> arrtoListInt (list <Integer> num) {return null; } public list <Integer> arrtoListInt (int[] num) {return null; }}

Some people may think that int[] and Integer[] are autoconvertible, and then the compiler is unable to recognize the array and will report an error. So when it comes to converting array lists of this basic type, remember to either use a stream right away, or just write through the for loop, and don’t get stuck with this compilation error.

My answer:

Public ArrayUtil {public ArrayUtil <String ArrayUtil (String[] ArrayUtil) {return ArrayUtil (ArrayArrayUtil);  Public String[] listtoArray (List<String> List) {return list.toArray(new String[list.size())]); } public int[] public int[] ArrayToListInt (List<Integer> List) {// ArrayToListInt (List<Integer> List) {// ArrayToListInt (List<Integer> List); //return list.toArray(new int[list.size())]); return list.stream() .mapToInt(Integer::valueOf) .toArray(); } public list <Integer> arrtoListInt (int[] num) {// ArrayList (int[] num) {// ArrayList (int[] num); // return Arrays.<Integer>asList(num); return Arrays .stream(num) .boxed() .collect(Collectors.toList()); }}

Initialize lists and arrays

Initialization isn’t that easy, especially when it comes to returning a List[] as a result. People often forget to initialize every List in the array before using it. Please complete the following:

  • Initialize list

    for 5 ones
  • Initializes int[] to five ones
  • ALTER LIS []; ALTER LIS []; ALTER LIS []; ALTER LIS []; ALTER LIS []

The correct answer is as follows:

Public static list <Integer> fillList() {list <Integer> list = The Collections. NCopies (5, 1); System.out.println(list); return list; Public static int[] fillArr() {int[] arr = new int[5]; public static int[] fillArr() {int[] arr = new int[5]; Arrays.fill(arr, 1); return arr; Public static list [] fillListArray() {list [] lists = new List[5]; public static list [] fillListArray() {list [] lists = new List[5]; IntStream.rangeClosed(0, 4).boxed() .forEach(i->lists[i] = new ArrayList()); return lists; }}

The sorting

About how to quickly sort lists and arrays, answer the following questions yourself at IDEA:

  • Arrays.sort() Collections.sort() Collections.sort() Collections.sort() Collections.sort()
  • How do you do a custom sort for a new object? As follows. Defining a Comparable
public class Student implements Comparable<Student> { private int stuId; private String stuName; private int score; @Override public int compareTo(Student o) { return stuId-o.stuId; }}

Reference topic: https://leetcode-cn.com/probl…

The map related

  • If the value in a map is a list, and every time we put data into a list in a key, we must initialize the list first. How can we reduce the initialization code?

GetOrDefault: Map.getOrDefault (Key, new ArrayList<>()).add(XXX

Do not repeat put

if (! map.containsKey(key)) { map.put(key, value); }

“Putifabsent (key, value)” is not absent from the school. Without, the teacher is absent from the school.

The values in the map are updated

For example, there are two ways to increment the value of key each time:

map.put(key, map.getOrDefault(key, 0)+1);

map.compute(key, (k,v)->(v==null?1:v+1));

In a more complex case, compute is useful.

ComputeIfAbsent (key, (k,v)->f(k,v)) only compute the LabMDA formula if the key does not exist. If it exists, it is ignored and the old value is returned.

ComputeIfPresent (key, (k,v)->f(k,v)) is updated only if it exists, but not if it does not exist.

Common Queue Usage

  • General Queue:
  • PriorityQueue: This ensures that the number of elements that are queued is always the largest or smallest that you have set. Is very useful.

PriorityQueue

Queue = new PriorityQueue<>((a, b) -> a[1] -b [1]); A [1] -B [1] is a small top heap A [1] -B [1] >0.
[]>

How to remember? When the heap is updated, it is updated from the top down, so that A is the top point, and B is the bottom point (the child node). When the return is greater than 0, swap A and B.

So that makes sense for the big top heap: if a minus b is less than 0, you have to swap, so the father is smaller than the son, so you have to swap, so the father is bigger than the son, so the father is smaller.

  • Delayed deletion is used when an element in the priority queue is deleted, but not at the top of the heap. Delayed deletion is used until it reaches the top of the heap. Therefore, use the extra realCount to count the actual number of queues, and use specific delete flags (do not set the delete flags in a way that would interfere with the queue compare method).

Simple binary search

  • Find the element in a list that is closest to x and greater than or equal to x

If you don’t want to write dichotomies by hand, use this method:

  • FloorKey (key) is used to locate the largest key less than or equal to a given key. If no such key exists, null is returned.

CeilingKey (Key) finds the smallest key greater than or equal to the given key and does not exist, which returns null

Memory:

Rounding up the ceiling, that’s the top of the key. To look up to the ceiling is to look up to the ceiling.

Floor down, that’s looking for the bottom of the key. Go down, go down, go down, go down, go down

  • Find the element in the list closest to x that is greater than (does not contain or equal to) x

Example: https://leetcode-cn.com/probl…

Click on the attention, the first time to understand Huawei cloud fresh technology ~