leetcode刷题笔记

人的天性:避难趋易,急于求成

数组

链表

哈希表

字符串

双指针

栈与队列

二叉树

回溯

贪心

动态规划

图论

数据库

预备知识

输入输出
  • 输入:Scanner: next, nextLine, nextByte, nextShort(), nextInt, nextLong, nextFloat, nextDouble
    Scanner scan = new Scanner(System.in);
    while(scan.hasNextXxx()){
        scan.nextXxx();
    }
    
  • 输出
    System.out.print();
    System.out.println();
    System.out.printf("%b %d %x %.2f %.2e %s", true, 10, 10, 1.1f, 2.2, "hello");
    
数据结构
  • 简单数据类型
    • char, byte, short, int, long, float, double
    • 数据类型转换
      • 简单数据类型转换
        • 低转高:自动类型提升
          • byte/short -> int -> long -> float -> double
        • 高转低:强制类型转换(损失精度)
          • double -> float -> long -> int -> byte/short
      • 字符串与其他数据类型的相互转换
        • 字符串转其他
          • Integer.parseInt, Float.parseFloat, Double.parseDouble
          • Integer.valueOf, Float.valueOf, Double.valueOf, String.valueOf
        • 其他转字符串
          • Integer.toString, Float.toString, Double.toString
  • 数组
    • char[], byte[], short[], int[], long[], float[], double[]
    • 常用属性:length
    • 常用方法:equals, toString, Arrays.binarySearch, Arrays.sort, Arrays.copyOfRange, Arrays.asList
  • 字符串
    • String
      • 常用方法:length, charAt, substring, isEmpty
    • StringBuilder
      • 常用方法:length, charAt, substring, toString, append, insert, delete, deleteCharAt, reverse
  • 链表节点
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { 
        	this.val = val; 
        	this.next = next; 
        }
    }
    
  • 二叉树节点
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    
    class Utils {
    	public TreeNode buildTree(Object[] arrays) {
    		TreeNode root = new TreeNode();
    		Queue<TreeNode> nodeQueue = new LinkedList<>();
    		int idx = 0;
    		if (idx < arrays.length && arrays[idx] != null) {
    			root.val = (int)arrays[idx];
    			nodeQueue.offer(root);
    		}
    		while (idx < arrays.length && !nodeQueue.isEmpty()) {
    			int count = nodeQueue.size();
    			while (count > 0) {
    				TreeNode node = nodeQueue.poll();
    				int leftIdx = 2 * idx + 1;
    				int rightIdx = 2 * idx + 2;
    				if (leftIdx < arrays.length && arrays[leftIdx] != null) {
    					node.left = new TreeNode((int)arrays[leftIdx]);
    					nodeQueue.offer(node.left);
    				}
    				else {
    					node.left = null;
    				}
    				if (rightIdx < arrays.length && arrays[rightIdx] != null) {
    					node.right = new TreeNode((int)arrays[rightIdx]);
    					nodeQueue.offer(node.right);
    				}
    				else {
    					node.right = null;
    				}
    				idx++;
    				count--;
    			}
    		}
    		return root;
    	}
    }
    
Java集合
  • Collection 接口
    • Set 接口
      • 无序性,不允许重复
      • TreeSet:基于红黑树实现,查找复杂度 O ( l o g N ) O(logN) O(logN),插入删除复杂度 O ( l o g N ) O(logN) O(logN),支持范围查找
      • HashSet:基于哈希表实现,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( 1 ) O(1) O(1)
        • LinkedHashSet:使用双向链表维护元素的插入顺序,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( 1 ) O(1) O(1)
      • 常用操作:add, remove, contains
        Set<Integer> tset = new TreeSet<>();
        Set<Integer> hset = new HashSet<>();
        Set<Integer> lhset = new LinkedHashSet<>();
        
    • List 接口
      • 有序性,允许重复
      • ArrayList:基于动态数组实现,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( N ) O(N) O(N),自动扩容(1.5)
      • LinkedList:基于双向链表实现,查找复杂度 O ( N ) O(N) O(N),插入删除复杂度 O ( 1 ) O(1) O(1)
      • Vector:基于动态数组实现,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( N ) O(N) O(N),并且线程安全(关键方法前加synchronized)
        • Stack 类:push, pop, peek, isEmpty
      • 常用方法:add, remove, get, set, size, isEmpty, sort, toArray
        List<Integer> alist = new ArrayList<>();
        List<Integer> llist = new LinkedList<>();
        Stack<Integer> stack = new Stack<>();
        
    • Queue 接口
      • 先进先出
      • 普通队列:Queue 接口
      • 双端队列:Deque 接口
        • offerFirst, offerLast, pollFirst, pollLast, peekFirst, peekLast, size, isEmpty
      • 优先队列:PriorityQueue 类
      • 常用方法:offer, poll, peek, size, isEmpty
        Queue<Integer> queue = new LinkedList<>();
        Deque<Integer> dq = new LinkedList<>();
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
            @Override
            public int compare(Integer a, Integer b){
                return a-b;
            }
        });
        
    • Iterator
      • 常用方法:hasNext, next
        List<T> list = new ArrayList<>();
        Iterator<T> it = list.iterator();
        while(it.hasNext()){
            it.next();
        }
        
  • Map 接口
    • 键值对,键唯一,不允许重复
    • TreeMap:基于红黑树实现,查找复杂度 O ( l o g N ) O(logN) O(logN),插入删除复杂度 O ( l o g N ) O(logN) O(logN)
    • HashMap:基于哈希表(在1.8中, 数组+链表/红黑树,8,64)实现,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( 1 ) O(1) O(1)
      • LinkedHashMap:使用双向队列维护元素的顺序,查找复杂度 O ( 1 ) O(1) O(1),插入删除复杂度 O ( 1 ) O(1) O(1)
    • 常用方法:put, remove, get, containsKey, containsValue, entrySet, keySet, values
      Map<String, Integer> tmap = new TreeMap<>();
      Map<String, Integer> hmap = new HashMap<>();
      Map<String, Integer> lhmap = new LinkedHashMap<>();
      

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值