Java常用函数LeetCode

这篇博客详细介绍了Java编程中常见的数据结构操作,包括数组、字符串、有序列表、链表、哈希集合、哈希表、栈、队列和双端队列的使用方法。讲解了如Arrays.sort()、equals()、substring()、indexOf()等关键函数,并提到了递归和Integer封装类的注意事项。此外,还涵盖了集合类的转换、元素的增删查改以及堆的概念和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组

Arrays.sort(arr);//注意Arrays后的s,且A大写

Arrays.equals(str1,str2)//注意equal后的s

字符串

  1. str.length();
  2. StringBuilder ans = new StringBuilder(); ans.append("/"); ans.toString();//字符串拼接
  3. buffer.reverse();   //StringBuffer对象的值反转
  4. s = s.trim();    //删除首尾空格
  5. String arr[] = s.split("\\s+");//括号内的界定符要用正则表达式写,split()方法分割完字符串返回的是一个字符串数组,可以用循环输出结果
  6. str.toUpperCase()与toLowerCase()     //大小写转换函数
  7. str.indexOf( " ")    //查找的是特定字符串第一次出现位置的索引,返回的是int类型
  8. str.replace(), replaceFirst(), replaceAll()   //替换函数
  9.  str1.compareTo(str2) 比较str1与str2的ASCII值
  10. String.valueOf(char[])   //字符数组转为字符串
  11. str.toCharArray() //字符串转为字符数组
  12. String.substring(开始位置索引,结束位置索引[可省略])  //字符串取子串
  13. Integer.parseInt(String);Double.parseDouble(String);//字符串转为int,double
  14. String.valueOf(int);//int转为String
  15. Integer.toString(int);

有序列表

List<Integer> list = new ArrayList();//List为抽象类,初始化时要new为ArrayList类
Collections.reverse(list);//翻转列表顺序
list.toArray(new int[list.size()];//List转为数组

//一般函数名第二个单词开始大写

链表

LinkedList<Integer> list = new LinkedList();
list.addFirst(item);在列表开头插入元素
list.addLast(item);在列表结尾插入元素
list.contains(item);列表是否包含指定元素
list.get(index);获取列表中指定位置处的元素
list.getFirst();获取列表第一个元素
list.getLast();获取列表最后一个元素
list.size();获取列表中元素个数

哈希集合

HashSet<Integer> set = new HashSet();
set.contains()
set.add()

 哈希表

Map<Integer,Integer> count = new HashMap();
count.put(key,value);//加入键值对
count.get(key);//根据键获取对应值
count.containsKey(key);//是否包含key,返回true/false
count.containsValue(value);//是否包含value,返回true/false
count.size();//map中键值对的数量
count.isEmpty();//map是否为空,返回true/false

Deque<TreeNode> stack = new LinkedList();
//也可以用 Stack<Integer> stack = new Stack();
stack.push();//进栈
stack.pop();//出栈
stack.peek();//查看栈顶对象
stack.empty();//判断栈是否为空

队列

//普通队列
Queue<String> queue = new LinkedList<String>();
queue.offer();//入队列
queue.poll();//出队列
queue.peek();//获取队头元素

双端队列

Deque<Integer> deque = new LinkedList<>();
deque.isEmpty();//判断空
deque.peekLast();//访问队尾数据
deque.removeLast();//移除队尾数据
deque.addLast(int k);//在队尾插入数据
deque.removeFirst();//移除队头数据
deque.peekFirst();//访问队头数据

PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>()//小顶堆
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11,new Comparator<Integer>(){ 
   @Override
   public int compare(Integer i1,Integer i2){
        return i2-i1;
   }
});// 大顶堆,容量11,需要重写Comparator函数
heap.add();/heap.offer()//插入元素
heap.size();//堆的大小
heap.poll();//弹出元素
heap.isEmpty();
heap.peek();//获取堆顶元素 

递归:关键在于找到重复的子问题确定的终止条件

Integer封装类

在使用Integer封装类时,涉及两个Integer对象中值的比较必须使用.equals()方法,==在两个对象的情况下会直接比较地址。如果一边是int,则会自动拆箱,进行值的比较。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pistachiout

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值