206. 反转链表

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode reverseList(ListNode head) {
  13. if(head==null){
  14. return null;
  15. }
  16. ListNode cur = head;
  17. ListNode pre = null;
  18. while (cur!=null){
  19. ListNode temp = cur.next;
  20. cur.next = pre;
  21. pre = cur;
  22. cur = temp;
  23. }
  24. return pre;
  25. }
  26. }

3. 无重复字符的最长子串

  1. class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. if(s==null || s.length()==0){
  4. return 0;
  5. }
  6. HashMap<Character,Integer> map = new HashMap<>();
  7. int start = 0;
  8. int maxLength = 0;
  9. for(int end = 0; end<s.length();end++){
  10. if(map.containsKey(s.charAt(end))){
  11. start = Math.max(start,map.get(s.charAt(end))+1);
  12. }
  13. map.put(s.charAt(end),end);
  14. maxLength = Math.max(maxLength,end-start+1);
  15. }
  16. return maxLength;
  17. }
  18. }

146. LRU 缓存

  1. public class LRUCache {
  2. private Map<Integer,Node> map = new HashMap<>();
  3. // 定义双向链表的节点
  4. static class Node{
  5. private Integer key;
  6. private Integer value;
  7. private Node pre;
  8. private Node next;
  9. public Node(){}
  10. public Node(Integer key,Integer value){
  11. this.key = key;
  12. this.value = value;
  13. }
  14. }
  15. private int size;
  16. private int capacity;
  17. private Node head;
  18. private Node tail;
  19. public LRUCache(int capacity) {
  20. this.capacity = capacity;
  21. this.size = 0;
  22. this.head = new Node();
  23. this.tail = new Node();
  24. head.next = tail;
  25. tail.pre = head;
  26. }
  27. // 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
  28. public int get(int key) {
  29. Node node = map.get(key);
  30. if(node==null){
  31. return -1;
  32. }
  33. // 将该节点移动到链表的头部
  34. // 1. 删除该节点
  35. removeNode(node);
  36. // 2. 将链表移动到头部
  37. moveToHead(node);
  38. return node.value;
  39. }
  40. private void moveToHead(Node node) {
  41. node.next = head.next;
  42. head.next.pre = node;
  43. head.next = node;
  44. node.pre = head;
  45. }
  46. private void removeNode(Node node) {
  47. node.pre.next = node.next;
  48. node.next.pre = node.pre;
  49. }
  50. // 如果关键字 key 已经存在,则变更其数据值 value ;
  51. // 如果不存在,则向缓存中插入该组 key-value 。
  52. // 如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
  53. public void put(int key, int value) {
  54. Node node = map.get(key);
  55. if(node!=null){
  56. node.value = value;
  57. // 将该节点移动到链表头部
  58. removeNode(node);
  59. moveToHead(node);
  60. }else{
  61. Node newNode = new Node(key,value);
  62. moveToHead(newNode);
  63. map.put(key,newNode);
  64. size++;
  65. if(size>capacity){
  66. // 则应该 逐出 最久未使用的关键字。
  67. Node delteNode = tail.pre;
  68. removeNode(delteNode);
  69. map.remove(delteNode.key);
  70. size--;
  71. }
  72. }
  73. }
  74. }

215. 数组中的第K个最大元素

  1. class Solution {
  2. public int findKthLargest(int[] nums, int k) {
  3. if(nums==null || nums.length==0){
  4. return -1;
  5. }
  6. PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
  7. for(int i=0;i<nums.length;i++){
  8. if(i<k){
  9. priorityQueue.add(nums[i]);
  10. }else{
  11. if(!priorityQueue.isEmpty() && nums[i]>priorityQueue.peek()){
  12. priorityQueue.poll();
  13. priorityQueue.add(nums[i]);
  14. }
  15. }
  16. }
  17. return priorityQueue.peek();
  18. }
  19. }

25. K 个一组翻转链表

  1. class Solution {
  2. public ListNode reverseKGroup(ListNode head, int k) {
  3. ListNode dummy = new ListNode(-1);
  4. ListNode pre = dummy;
  5. dummy.next = head;
  6. ListNode end = pre;
  7. ListNode start = pre.next;
  8. while (end.next!=null){
  9. for(int i=0;i<k && end!=null;i++){
  10. end = end.next;
  11. }
  12. if(end==null){
  13. break;
  14. }
  15. ListNode temp = end.next;
  16. end.next = null;
  17. pre.next = reverse(start);
  18. start.next = temp;
  19. pre = start;
  20. end = pre;
  21. start = temp;
  22. }
  23. return dummy.next;
  24. }
  25. private ListNode reverse(ListNode cur) {
  26. ListNode pre = null;
  27. while (cur!=null){
  28. ListNode temp = cur.next;
  29. cur.next = pre;
  30. pre = cur;
  31. cur = temp;
  32. }
  33. return pre;
  34. }
  35. }