Java-20180419

1. leetcode second question

Given two linked lists, the reverse order is converted to numerical addition, and the new linked list is output in reverse order.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Idea: The algorithm is the most simple idea at the beginning. First convert the linked list to an integer, then add it and then convert it into a linked list

  while(it1.hasNext())
  {
   p=it1.next(); 

   s1=s1+p*t1;

   t1*=10;
  } //The linked list is transformed into a shape, p is a single value from low to high, and t represents the power of 10.

while(s!=0)
  {
   leave=s%10;
    s=s/10;

  } //Integer number decomposition, leave represents each single value from low to high.

Later, it was found that the data in the test set exceeded the int type, and the data in the long type still exceeded two data types. It was changed to an object of the BigInteger class, and the last data was super-large N bits, so I began to change my thinking.

It can be calculated by adding each bit separately, and then keep the carry, and finally the 9th Acceptance.

Code:

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag=0;
  int s;
  s=(l1.val+l2.val+flag)%10;
  flag=(l1.val+l2.val+flag)/10;
  ListNode re=new ListNode(s);
  l1=l1.next;
  l2=l2.next;   //这一段的作用详见第3点
  while(l1!=null || l2!=null)
  {
   if(l1==null)
   {
    s=(l2.val+flag)%10;
    flag=(l2.val+flag)/10;
                ListNode newl=new ListNode(s);
    add(re,newl);
    l2=l2.next;
   }
   else if(l2==null)
   {
    s=(l1.val+flag)%10;
    flag=(l1.val+flag)/10;
                ListNode newl=new ListNode(s);
    add(re,newl);
    l1=l1.next;
   }
   else
   {
    s=(l1.val+l2.val+flag)%10;
    flag=(l1.val+l2.val+flag)/10;
    ListNode newl=new ListNode(s);
    add(re,newl);
    l1=l1.next;
    l2=l2.next;
   }
  }
  if(flag==1)
  {
   ListNode newl=new ListNode(1);
   add(re,newl);
  }
  return re;
       
    }
    void add(ListNode l,ListNode ll)
    {
     if(l==null)
     {
      l=ll;
     }
     while(l.next!=null)
     {
      l=l.next;
     }
     l.next=ll;
    }

2. Linked list review and first contact with BigInteger

Differences between linked lists in JAVA and C

There are no pointers in JAVA, so the class is directly used as the next node reference:

class ListNode

{

   int val;

      ListNode next;
      ListNode(int x) { val = x; }
}

C:

typedef struct ListNode

{

   int val;

      struct ListNode *next;
}ListNode,*LinkList; //Structures in C language have semicolons regardless of whether they have typedef or not.

Traversing a linked list (simple)

while(l!=null)

{

// handle l.val

l=l.next;

}

Reverse order traversal of linked list

if(l!=null)

{

if(l.next!=null)

{

//recursive call

}

// handle l.val

} //Note that you start with if, while will fall into an infinite loop.

Generally speaking, using long is enough, but sometimes we encounter larger numbers, then we can use the BigInteger object to initialize BigInteger s=BigInteger.valueOf(10)

add(//BigInteger object), subtract(), multiply(), divide() operations of addition, subtraction, multiplication and division.

divideAndRemainder() returns an array, the first quotient, and the second remainder. BigDecimal is introduced next time.

3. Problems encountered

Small problem 1: The file name (class name) cannot be the same as the imported class name.

Small problem 2: Long exceeds the boundary value of int and then coerced to int will become a random value.

Comparison of Java's formal parameter changing actual parameter problem and C++:

Java can only change the value of the reference, only class objects and arrays (except String and references of various basic types (Integer, Double, etc.)) can be used as formal parameters to change the actual parameters.

C++ can use references and pointers.

You can declare the form void swap(int &a,int &b) and call the form swap(a,b) by simply adding the "e.

Declare void swap(int *a, int *b) {function body uses *a, *b}, calls swap(&a, &b), and the form remains unchanged in Java.

4. Java Chapter 3 Object-Oriented Programming (1)

(1) Foundation: There is at most one public class in a Java file, and the file name must be the same as the public class name. If there is no public class, the file name can be the same as any class name.

Garbage collection mechanism: If an instance object does not have any reference points, the memory it occupies will become garbage, but the JVM generally does not recycle it immediately, and will recycle garbage at a suitable time. Guaranteed to be recycled first. If you want to manually recycle, you can use the System.gc() method, which will call the finalize() method of the class, but the system will not call it after the program is executed.

(2) Inheritance: one of the three major characteristics, a class inherits at most one direct parent class.

super() and this() methods: super() is called in the constructor of the subclass to initialize the class members and must be written in the first sentence of the constructor. If it does not contain any parameters, it can be omitted. It is worth noting that if the parent class has no constructor, a default no-argument constructor will be created; if the parent class has only a parameterized constructor, the default constructor will not be generated, then the subclass must explicitly call super and must contain parameters;

  Implicit call to /super() without arguments super() has parameters
parameterless constructor / no constructor correct mistake
parameterized constructor mistake correct
have correct correct

The this() method also appears in the constructor and must be written in the first line, so it cannot be used at the same time with super() to call other overloaded constructors.

Upcasting implicit call: when the subclass object is copied to the superclass object, it becomes upcasting. For example, the subclass Square inherits the superclass Shape, and the reference of the superclass can be pointed to the subclass object, for example: Shape sh=new Square();

Downcasting explicit call: Following the above statement, Square sq=(Square)sh; is called downcasting (dwoncasting), but this sentence cannot be used directly, because only sh can be downcasted when it points to a subclass object, otherwise the compilation will not error, but running will fail.

instanceof: Determines whether the object is an instance of a certain class. For no transformation, the subclass is an instance of all superclasses and itself, and returns true, such as Square sq=new Square(); then sq instanceof Square, sq instanceof Shape and even sq instanceof Object returns true; and the parent class will not be an instance of the subclass, such as Shape sh=new Shape(); sh instanceof Shape returns true, but sh instanceof Square returns false;

For upcasting, the parent class is the object of the subclass, for example Shape sh=new Square(); then sh instanceof Square returns true; or Square sq=new Square(); Shape sh=sq; then sh instanceof Square, sh instanceof Shape, sq instanceof Square, sq instanceof Shape all return true;

For downcasting, since the parent class is definitely upcasting, it is not discussed, and the subclass is the same as without conversion.

(3) Polymorphism: one of the three major characteristics of object-oriented, divided into static polymorphism, dynamic polymorphism (ie static binding and dynamic binding in C++)

Static polymorphism: overloading, a method with the same name but different parameters, such as individual constructors. Note that the same parameter type but different parameter names are not overloaded, they are still the same function.

Dynamic polymorphism: that is, override, which means that the parent class and subclass method parameter types and method names are exactly the same, but the JVM will identify which call it is. The subclass object calls the subclass method, the parent class object calls the parent class method, the parent class of the upward transformation calls the subclass method, and the subclass of the downward transformation also calls the subclass method.

super and this keywords: Note that it is not the same as the previous super() and this() methods. super can be used in non-static member methods in subclasses to access members of the supertype. The usage of this is the same as super, both are super. a, this.b, super.A(), this.B() form, but to call members of this class, it is mainly used to distinguish local variables and member fields (with the same name), for example, in many set methods, there will be this .a=a similar statement.

Also static methods do not have dynamic polymorphism.

(4) Package: The package keyword package must be the first sentence of the file;

There are three types of import package statements:

import wym.*; Import the interfaces, classes, and enumerations in the entire wym package. Types of subpackages are not imported.

import wym.Eat; Import the Eat type (one of interface, class, enumeration) in the wym package.

import static wym.Eat.main; import the main static method.

The first one will increase program overhead and reduce compilation efficiency, so try to use the latter two.

(5) Package: one of the three major characteristics

Class access control methods: public and default (that is, no modifiers are added), public classes can be used by all packages (different packages need to be imported with import, and visible in the same package), and default classes can only be used in packages use (visible in the same package).

Class member access control methods: four types of public, protected, default, private.

For a public class member, all type definitions that can access the class can access the member.

For protected class members, if the class is in the default mode, members of other packages cannot be accessed. If the class is public, more subclass methods of other packages can access it than default.

For default class members, the same package can access, but different packages must not access.

For private class members, only member methods of this class can be accessed.

Permissions when the class is public:

access control mode within the same class In the same package Subclass All classes
public allow allow allow allow
Protect allow allow allow  
default allow allow    
private allow      

Permissions when the class is the default:

 

access control mode within the same class In the same package Subclass All classes
public allow allow    
Protect allow allow    
default allow allow    
private allow      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606254&siteId=291194637