• Java常用函数LeetCode


    数组

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

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

    链表

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

    哈希集合

    HashSet set = new HashSet();
    set.contains()
    set.add()

     哈希表

    Map 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 stack = new LinkedList();
    //也可以用 Stack stack = new Stack();
    stack.push();//进栈
    stack.pop();//出栈
    stack.peek();//查看栈顶对象
    stack.empty();//判断栈是否为空

    队列

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

    双端队列

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

    PriorityQueue minHeap = new PriorityQueue()//小顶堆
    PriorityQueue maxHeap = new PriorityQueue(11,new Comparator(){ 
       @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,则会自动拆箱,进行值的比较。

  • 相关阅读:
    NCTF2022 - pwn 部分 wp
    史上最全架构师知识图谱(纯干货)
    基于SSM的动漫商城管理系统
    淘宝开店装修教程 (2023新版)
    集成环信IM时常见问题及解决——包括消息、群组、推送
    WLAN学习笔记
    接口测试文档
    java的面向对象基础(6)——高级泛型
    深入了解JUC并发(一)什么是JUC
    MFC使用system有弹黑窗的解决 用WinExec(szBuffer, SW_HIDE);代替
  • 原文地址:https://blog.csdn.net/qq_45808700/article/details/126861076