[First time to write a program using java] Limited shyness

Insert picture description here

class Solution {
    
    
    public int rob(int[] nums) {
    
    
		int[] dp = new int[nums.length];
        if (nums.length==0){
    
    
            return 0;
        }else if(nums.length==1){
    
    
            return nums[0];
        }
        dp[0]=nums[0];
        dp[1]=Math.max(dp[0],nums[1]);
		int max=Math.max(dp[0],dp[1]);
		for(int i=2;i<nums.length;i++){
    
    
			dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
			max=Math.max(max,dp[i]);
			}
		return max;
    }
}

Insert picture description here

import java.util.Stack;
class MinStack {
    
    

    /** initialize your data structure here. */
    private Stack<Integer> stack; //定义一个栈
    private Stack<Integer> ministack;
    public MinStack() {
    
    
        stack = new Stack<>();
        ministack = new Stack<>();
    }
    
    public void push(int x) {
    
    
        stack.add(x);
        if (ministack.isEmpty() || ministack.peek() >= x) {
    
    
            ministack.add(x);
        }else{
    
    
            ministack.add(ministack.peek());
        }
    }
    
    public void pop() {
    
    
        if (!stack.isEmpty()) {
    
    
            stack.pop();
            ministack.pop();
        }
    }
    
    public int top() {
    
    
        return stack.peek();
    }
    
    public int getMin() {
    
    
        return ministack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

Introduction to java.util.Stack

Stack is a last-in-first-out (LIFO) structure, which inherits from Vector and extends 5 methods push(), pop(), peek(), empty(), search().
1. push(E ): Push the item into the stack
2. pop(): Push the top item in the stack and return the pushed item
3. peek(): Return the top item in the stack, but don’t do it Any operation
4. empty(): judge whether the stack is empty
5. search(Object): search for the position of an item in the stack [position is the distance between the item closest to the top of the stack and the top of the stack]

PS: Although Java provides this type of data structure, it is officially recommended to use Deque [double-ended queue]. Deque provides better integrity and consistency and should be used first.

import java.util.Stack;

import org.junit.Before;
import org.junit.Test;

/**
 * Stack(栈)继承了Vector类,底层实现是数组。 
 * 此处只介绍了Stack自己定义的方法,父类中的方法不再一一介绍。
 */
public class TestStack {
    
    

    // 定义一个栈
    Stack<String> stack;

    @Before
    public void before() {
    
    
        // 实例化栈变量
        stack = new Stack<String>();

        // add方法向栈中添加元素,添加成功返回true
        stack.add("1");
        stack.add("2");
        stack.add("3");
        stack.add("4");
        stack.add("5");

        // push方法向栈中添加元素,返回结果是当前添加的元素
        stack.push("a");
        stack.push("b");
        stack.push("c");
        stack.push("d");
        stack.push("e");

        // push和add都是向栈中添加元素,底层实现也是一样的,都是先将Vector扩容,再添加

    }

    // pop方法移除并返回栈顶元素,如果是空栈,会抛出异常:EmptyStackException
    @Test
    public void test1() {
    
    
        String pop = stack.pop();
        System.out.println(pop); // e
        System.out.println(stack); // [1, 2, 3, 4, 5, a, b, c, d]
    }

    // peek方法获取栈顶元素,但并不移除,如果是空栈,会抛出异常:EmptyStackException
    @Test
    public void test2() {
    
    
        String peek = stack.peek();
        System.out.println(peek); // e
        System.out.println(stack); // [1, 2, 3, 4, 5, a, b, c, d, e]
    }

    // empty方法检查栈是否是空栈
    @Test
    public void test3() {
    
    
        boolean isEmpty = stack.empty();
        System.out.println(isEmpty); // false
    }

    // search方法查看某元素在栈中的位置,计数从1开始
    @Test
    public void test4() {
    
    
        int index = stack.search("1");
        System.out.println(index); // 10
    }

}
public class Test {
    
    

public static void main(String[] args){
    
    
Stack<String> stack = new Stack<>();
System.out.println("数量:"+stack.size());
stack.push("100");
stack.push("200");
stack.push("300");
for(int i = 0; i <stack.size(); i++){
    
    
System.out.println("stack值:"+stack.get(i));
}
System.out.println("数量:"+stack.size());
List<String> stack_list = stack.subList(0,2);
for(String str : stack_list){
    
    
System.out.println("stack_list截取值:"+str);
}
System.out.println("搜索100结果:"+stack.search("300"));
}
}

4 methods of ArrayList initialization

In the last post we discussed about class ArrayList in Java and it’s important methods. Here we are sharing multiple ways to initialize an ArrayList with examples.

Method 1: Initialization using Arrays.asList

 
ArrayList<Type> obj = new ArrayList<Type>(
        Arrays.asList(Object o1, Object o2, Object o3, ....so on));
Example:
 
import java.util.*;
public class InitializationExample1 {
    
    
   public static void main(String args[]) {
    
    
       ArrayList<String> obj = new ArrayList<String>(
        Arrays.asList("Pratap", "Peter", "Harsh"));
      System.out.println("Elements are:"+obj);
   }
}

Output:

Elements are:[Pratap, Peter, Harsh]

Method 2: Anonymous inner class method to initialize ArrayList

Copy the code
Syntax:

ArrayList<T> obj = new ArrayList<T>(){
    
    {
    
    
           add(Object o1);
           add(Object o2);
           add(Object o3);
                   ...
                   ...
           }};
Example:

import java.util.*;
public class InitializationExample2 {
    
    
   public static void main(String args[]) {
    
    
       ArrayList<String> cities = new ArrayList<String>(){
    
    {
    
    
           add("Delhi");
           add("Agra");
           add("Chennai");
           }};
      System.out.println("Content of Array list cities:"+cities);
   }
}

Copy code

Output:

Content of Array list cities:[Delhi, Agra, Chennai]

Method3: Normal way of ArrayList initialization

Copy the code
Syntax:

ArrayList<T> obj = new ArrayList<T>();
       obj.add("Object o1");
       obj.add("Object o2");
       obj.add("Object o3");
                        ...
                        ...
Example:

import java.util.*;

public class Details {
    
    
   public static void main(String args[]) {
    
    
       ArrayList<String> books = new ArrayList<String>();
       books.add("Java Book1");
       books.add("Java Book2");
       books.add("Java Book3");
      System.out.println("Books stored in array list are: "+books);
   }
}

Output:

Books stored in array list are: [Java Book1, Java Book2, Java Book3]

Method 4: Use Collections.ncopies

Collections.ncopies method can be used when we need to initialize the ArrayList with the same value for all of its elements. Syntax: count is number of elements and element is the item value

ArrayList<T> obj = new ArrayList<T>(Collections.nCopies(count, element));
Example:

import java.util.*;

public class Details {
    
    
   public static void main(String args[]) {
    
    
       ArrayList<Integer> intlist = new ArrayList<Integer>(Collections.nCopies(10, 5));
      System.out.println("ArrayList items: "+intlist);
   }
}

Output:

ArrayList items: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

Refer to the original link: https://beginnersbook.com/2013/12/how-to-initialize-an-arraylist/

The purpose of blogging is to remember things that are easy to forget. It is also a summary of your work. Articles can be reprinted without copyright. I hope to do my best to do better, and everyone works hard to make progress together!

If you have any questions, you are welcome to discuss them together. If there are any problems with the code, please correct me!

Add a pair of wings to your dream, so that it can fly freely in the sky!

Guess you like

Origin blog.csdn.net/weixin_42462804/article/details/106420572