algorithm-base/animation-simulation/Leetcode常用类和函数.md

604 lines
12 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Leetcode
##
### ListNode
```java
ListNode list=new ListNode(0)
```
0
### HashSet
HashSet HashMap nullHashSet HashSet 线 线 HashSet 线访 HashSet 访
```java
HashSet<String> sites = new HashSet<String>();
```
#### add()
HashSet
```
sites.add("我是袁厨,大家快快关注我吧");
```
#### size()
#### remove()
remover()size()ArrayList
#### contains()
```
System.out.println(sites.contains("我是袁厨,大家快快关注我吧"));
```
> true
##
### length
length
### sort()
### Arrays.fill()
```
Arrays.fill(nums, 0, 2, 0);
for(int x:nums){
System.out.println(x);
}
```
> 0,0
### Arrays.sort()
,
```
int[] array = {1,6,3,4};
Arrays.sort(array);
return array;
```
> array : 1,3,4,6;
### Arrays.copyOfRange()
02
```
int[] array = {1,2,3,4};
int[] ar= Arrays.copyOfRange(intersection, 0, 2);
return ar;
```
> array2: 1 , 2 ;
### System.arraycopy();
```java
System.arraycopy(targetnums,beganindex, newnums, newindex, length);
```
targetnums:
beganindex:
newsnums:
newindex:
length
```java
int[] array = {1,2,3,4};
int[] newarray = new int[2];
System.arraycopy(array,0,newarray,0,2);
for(int x : newarray){
System.out.println(x)
}
```
> 12
###
#### x | 0
1001|0000=1001110
```java
public static void main(String[] args) {
int x =10 ;
System.out.println(x|0);
}
```
> 10
#### x & 0
0
1001&0000=0000;11
```
public static void main(String[] args) {
int x =10 ;
System.out.println(x&0);
}
```
> 0
#### x ^ 0
01
0111^0000=0111
```java
public static void main(String[] args) {
int x =-10 ;
System.out.println(x^0);
}
```
> -10
#### x | 1
1
```java
int x =-9 ;
int y = -10;
System.out.println(x|1);
System.out.println(y|1);
```
> -9-9
#### x ^ 1
11
```java
int x =-9 ;
int y = -10;
System.out.println(x^1);
System.out.println(y^1);
```
> -10-9
#### x & 1
01
```java
int x =-9 ;
int y = -10;
System.out.println(x&1);
System.out.println(y&1);
```
> 10
#### 1<<3
130001 ---->10002^38
```java
System.out.println(1<<3);
```
> 8
#### HashMap
HashMap,
```
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
```
hashmap
```java
for (int num : arr){
map.put(num, map.getOrDefault(num, 0) + 1);//如果没有则添加如果有则加1
}
```
Hashmap,k
```
for (int k : hashmap.keySet())
if (hashmap.get(k) == 1) return k;
```
HashSet
```
set.iterator().next();//迭代器
```
##
### ArrayList<List<>>
```java
List<List<Integer>> arr = new ArrayList<List<Integer>>();
```
### ArrayList
```java
List<Integer> array = new ArrayList<>();
```
ArrayList <>
#### add(),
```java
public class Test {
public static void main(String[] args) {
List<String> array = new ArrayList<>();
array.add("大家好我是袁厨");
System.out.println(array);
}
}
```
>
#### get()
get()
```java
public class Test {
public static void main(String[] args) {
List<String> array = new ArrayList<>();
array.add("大家好我是袁厨");
System.out.println(array.get(0));//获取第一个元素
}
}
```
>
#### set()
set()
```
public class Test {
public static void main(String[] args) {
List<String> array = new ArrayList<>();
array.add("大家好我是袁厨");
array.set(0,"祝大家天天开心")
System.out.println(array.get(0));//获取第一个元素
}
}
```
>
#### remove()
```
public class Test {
public static void main(String[] args) {
List<String> array = new ArrayList<>();
array.add("大家好我是袁厨");
array.add("祝大家天天开心");
array.remove(0);
System.out.println(array);//获取第一个元素
}
}
```
>
#### isEmpty()
isEmpty()while使
while(!queue.isEmpty()){
//用来判断队列是否为空的情况
}
```
public class Test {
public static void main(String[] args) {
List<String> array = new ArrayList<>();
array.add("大家好我是袁厨");
array.add("祝大家天天开心");
array.remove(0);
System.out.println(array.isEmpty());//获取第一个元素
}
}
```
false
#### clear()
```
ArrayList<String> sites = new ArrayList<>();
sites.add("袁厨不帅");
sites.add("袁厨不帅");
sites.add("袁厨不帅");
System.out.println(sites);
// 删除所有元素
sites.clear();
System.out.println(sites);
```
> ```
> [][]
> ```
#### sort()
```
public class leetcode {
public static void main(String[] args) {
int[] arr = {4,5,1,3,6};
Arrays.sort(arr);
for(int x : arr){
System.out.println(x);
}
}
}
```
> 13456
##
### StringBuffer
StringBuilder Java 5 StringBuffer StringBuilder 线访 StringBuilder StringBuffer 使 StringBuilder 线使 StringBuffer
```Java
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("我的");
sBuffer.append("名字");
sBuffer.append("是");
sBuffer.append("袁厨");
sBuffer.append("大家点个关注吧");
System.out.println(sBuffer);
}
}
```
>
StringStringBuffer
### charAt(i)
charAt() 0 length() - 1
```java
public class Test {
public static void main(String[] args) {
String s = "大家好我是袁厨,点个关注哦";
char result = s.charAt(6);
System.out.println(result);
}
}
```
>
### s.charAt(index)-'0'
int
```java
public class Test {
public static String getType(Object test) {
return test.getClass().getName();
}
public static void main(String[] args) {
String s = "祝大家永不脱发,点个关注哦";
System.out.println(getType(s.charAt(6)-'0'));
}
}
```
> java.lang.Integer
### Integer.toString()
intstring**9**x
```java
class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
//将int型变成string型然后遍历字符串不再需要使用额外数组进行存储
String t = Integer.toString(x);
int i = 0;
int j = t.length()-1;
//双指针遍历
while(i<j){
if(t.charAt(i)!=t.charAt(j)){
return false;
}
else{
i++;
j--;
}
}
return true;
}
}
```
### substring()
substring()
```java
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);
```
beginIndexbeginIndex ->endIndex;
```
String Str = new String("程序员爱做饭");
System.out.println(Str.substring(3) );
System.out.println(Str.substring(4, 5) );
```
>
### equals()
equals() Number
```
public static void main(String args[]){
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;
System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}
```
> false,true,false
### toCharArray()
```java
String num = "12345";
char[] s = num.toCharArray();//将字符串变为char数组
System.out.println(s);
```
> 12345
### charString
```java
String newstr = new String (arr2,start,end);
```
### indexOf
- **int indexOf(String str):** -1
- **int indexOf(String str, int fromIndex):** fromIndex -1
```
String s = "LkLLAAAOOO";
return s.indexOf("LLL");
```
> -1
## Stack
####
```
Stack<TreeNode> stack = new Stack<TreeNode>();//创建栈
```
TreeNode,
#### push()
```
stack.push(root);//将根节点入栈
```
#### pop()
```java
TreeNode temp = stack.pop();//将栈顶元素出栈赋值TreeNode变量temp
```
peek()
```java
stack.push(root);
TreeNode temp2 = stack.peek();
System.out.println(temp2.val);
```
####
```
StringBuilder str = new StringBuilder();
//遍历栈
while(!stack.isEmpty()){
str.append(stack.pop());
}
//反转并变为字符串
return str.reverse().toString();
```