arraylist

LeetCode in Java [11]: 90. Subsets II

允我心安 提交于 2020-01-25 01:38:30
Given a collection of integers that might contain duplicates, nums , return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] Backtrack就完事了,记得先排序一下,结果: Success Runtime: 1 ms, faster than 99.06% of Java online submissions for Subsets II. Memory Usage: 37.5 MB, less than 98.53% of Java online submissions forSubsets II. class Solution { private List<List<Integer>> res; public List<List<Integer>> subsetsWithDup(int[] nums) { res=new ArrayList<>(); ArrayList<Integer> temp=new

数据结构与算法——暴力递归

偶尔善良 提交于 2020-01-25 00:41:15
暴力递归就是尝试 1, 把问题转化为规模缩小了的同类问题的子问题 2, 有明确的不需要继续进行递归的条件(base case) 3, 有当得到了子问题的结果之后的决策过程 4, 不记录每一个子问题的解 一定要学会怎么去尝试,因为这是动态规划的基础,这一内容我们将在提升班讲述 汉诺塔问题 打印n层汉诺塔从最左边移动到最右边的全部过程 public static void hanoi(int n) { if (n > 0) { func(n, "左", "右", "中"); } } // 1~i 圆盘 目标是from -> to, other是另外一个 public static void func(int N, String from, String to, String other) { if (N == 1) { // base System.out.println("Move 1 from " + from + " to " + to); } else { func(N - 1, from, other, to); System.out.println("Move " + N + " from " + from + " to " + to); func(N - 1, other, to, from); } } 打印一个字符串的全部子序列,包括空字符串 public static

Getting IndexOutOfBoundsException only when resume Activity

徘徊边缘 提交于 2020-01-24 21:50:19
问题 My problem is that my App crashes when the MainActivity gets resumed. The error is an IndexOutOfBoundsException , unfortunately without a specific line. And the error says Index: 0, Size: 0 . As I know this means that my ArrayList is null and I call it before I initialize it. But I searched since days after that case. I didn't find anything. So first here is the Error: 08-28 13:10:24.232 18741-18741/com.test.test.test E/AndroidRuntime: FATAL EXCEPTION: main Process: com.test.test.test, PID:

com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1画图

痴心易碎 提交于 2020-01-24 20:33:47
MPAndroidChart画图 导入架包,是3.1.0版本的哦。 implementation 'com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1' xml布局中设置图形显示的位置,加id。 < com . github . mikephil . charting . charts . PieChart android : id = "@+id/pieChart" android : layout_width = "match_parent" android : layout_height = "match_parent" / > Java类中的核心代码!!! public class Paintyuan extends AppCompatActivity { PieChart pieChart ; ImageView imageView ; ArrayList < JSONObject > arrayList ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_yuan )

剑指offer 从尾到头打印链表

情到浓时终转凉″ 提交于 2020-01-24 18:57:41
题目 输入一个链表,按链表从尾到头的顺序返回一个ArrayList。 思路1 最简单的方法,直接按顺序遍历,然后利用Collections函数转置。 import java.util.*; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode node) { ArrayList<Integer> ans=new ArrayList(); if(node==null) return ans; while(node!=null){ ans.add(node.val); node=node.next; } Collections.reverse(ans); return ans; } } 思路2 因为是倒叙输出,所以利用栈的先进后出的特性。 import java.util.*; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode node) { ArrayList<Integer> ans=new ArrayList(); Stack<Integer> stack=new Stack(); while(node!=null) { stack.push(node

Combining Arrays in Java

烈酒焚心 提交于 2020-01-24 13:57:05
问题 In the following code I needed to read in a list of five student names and marks for five quizzes for each student, which loads the names in an ArrayList of type String and the quiz marks in an ArrayList of type Integer. I have broken this problem down by the two different ArrayLists, and I am looking to combine them but not sure. The following code reads five students names and loads the names in an ArrayList of type String import java.util.ArrayList; public class QuizAveragee { public

day07_Scanner类、Random类、ArrayList 类

大城市里の小女人 提交于 2020-01-24 10:24:24
API 概述 API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。 API使用步骤 打开帮助文档。 点击显示,找到索引,看到输入框。 你要找谁?在输入框里输入,然后回车。 看包。java.lang下的类不需要导包,其他需要。 看类的解释和说明。 学习构造方法 使用成员方法/变量 引用类型使用步骤 类属于引用类型中的一种,所以去使用步骤符合引用类型的使用步骤。 1:导包 使用import关键字导包,在类的所有代码之前导包,引入要使用的类型,java.lang包下的所有类无需导入。 格式: 2:创建对象 使用该类的构造方法,创建一个该类的对象。 格式: 3:调用方法 调用该类的成员方法,完成指定功能。 格式: // 接收一个键盘录入的整数 int i = sc.nextInt(); Scanner类 Scanner使用步骤 查看类 java.util.Scanner :该类需要import导入后使用。 查看构造方法 public Scanner(InputStream source) :

Unable to find assembly

折月煮酒 提交于 2020-01-24 07:49:24
问题 I'm Serializing an ArrayList to a binary file in order to send it across TCP/IP. The serialized file is created by the server and I hope to be able to deserialize it with the client that I'm writing at the moment. However, when the client attempts to deserialize it throws a SerializationException because it can't find the assembly (presumably) which serialized the file to begin with. How do I get around this? 回答1: Does your arraylist contain custom data type (i.e. your own classes)? The

把二叉树打印成多行

血红的双手。 提交于 2020-01-24 06:58:26
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。 思路同上题 之字形打印二叉树一样 利用队列(先进先出) 储存如Array list 再存入总arraylist public class Solution { ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(pRoot == null){ return res; } ArrayList<Integer> temp = new ArrayList<Integer>();// Queue <TreeNode> q = new LinkedList<TreeNode>();//创建队列用来添加节点 q.offer(pRoot);//添加根节点 int start = 0, end = 1; while(!q.isEmpty()){ //如果队列不为空, 继续循环 直到为空 TreeNode node = q.poll(); //添加queue第一个排出的元素,并删除 temp.add(node.val); start++; //记录值+1 if(node.left != null){ //判断 是否有左子树,

Sort ArrayList<String> excluding the digits on the first half of the String

痴心易碎 提交于 2020-01-24 05:38:04
问题 I'm trying to sort an ArrayList which a series of String composed as well (XX and YY are numbers): Test: XX Genere: Maschio Eta: YY Protocollo: A I would link to sort them by only considering the YY value. I found this method but it considerers all the digits of the string and I cannot remove n digits before YY because I don't know from how many digits is composed XX: Collections.sort(strings, new Comparator<String>() { public int compare(String o1, String o2) { return extractInt(o1) -