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;
}
}
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();
*/
java.util.Stack的简介
Stack是一种后进先出(LIFO)的结构,其继承了Vector的基础上拓展5个方法push()、pop()、peek()、empty()、search()而来
1、push(E):将item推入到栈中
2、pop() :将栈中的最顶一个item推出,并返回被推出的item
3、peek():返回栈中最顶的一个item,但不对其做任何操作
4、empty():判断该栈是否为空
5、search(Object):搜索某一个item在该栈中的位置【位置为离栈顶最近的item与栈顶间距离】
PS:虽然Java有提供该类型的数据结构,但是官方推荐使用Deque【双端队列】,Deque提供更好的完整性和一致性,应该优先使用。
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"));
}
}
ArrayList初始化的4种方法
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
复制代码
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);
}
}
复制代码
Output:
Content of Array list cities:[Delhi, Agra, Chennai]
Method3: Normal way of ArrayList initialization
复制代码
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]
参考原文链接:https://beginnersbook.com/2013/12/how-to-initialize-an-arraylist/
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
给自己的梦想添加一双翅膀,让它可以在天空中自由自在的飞翔!