CodeTop
。
Charlesjjc
这个作者很懒,什么都没留下…
展开
-
5. 最长回文子串
5. 最长回文子串第一种解法,动态规划class Solution { public String longestPalindrome(String s) { int len = s.length(); if(len <= 1) return s; boolean[][] dp = new boolean[len][len]; for(int j = 0;j < len;j++){ f.原创 2021-09-14 04:14:26 · 176 阅读 · 0 评论 -
46. 全排列
46. 全排列class Solution { List<List<Integer>> res = new LinkedList<>(); List<Integer> list = new LinkedList<>(); Set<Integer> set = new HashSet<>(); public List<List<Integer>> per原创 2021-09-13 08:43:53 · 113 阅读 · 0 评论 -
142. 环形链表 II
142. 环形链表 IIpublic class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head, slow = head; while(fast != null){ if(fast.next == null || fast.next.next == null) return null; fast = fas.原创 2021-09-13 08:20:54 · 109 阅读 · 0 评论 -
33. 搜索旋转排序数组
class Solution { public int search(int[] nums, int target) { int n = nums.length; if (n == 0) { return -1; } if (n == 1) { return nums[0] == target ? 0 : -1; } int l = 0, r = n - .原创 2021-09-13 07:34:02 · 82 阅读 · 0 评论 -
415. 字符串相加
415. 字符串相加class Solution { public String addStrings(String num1, String num2) { int i = num1.length() - 1, j = num2.length() - 1, add = 0; StringBuffer ans = new StringBuffer(); while (i >= 0 || j >= 0 || add != 0) {原创 2021-09-13 03:52:59 · 47 阅读 · 0 评论 -
20. 有效的括号
20. 有效的括号class Solution { public boolean isValid(String s) { Set<Character> left = new HashSet<>(); Set<Character> right = new HashSet<>(); left.add('('); left.add('{'); left.add(原创 2021-09-13 03:31:53 · 45 阅读 · 0 评论 -
236. 二叉树的最近公共祖先
236. 二叉树的最近公共祖先/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private TreeNode ans; publi原创 2021-09-13 02:12:17 · 48 阅读 · 0 评论 -
88. 合并两个有序数组
88. 合并两个有序数组class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] res = new int[m + n]; int i = 0,j = 0; while(i < m && j < n){ if(nums1[i] < nums2[j]){原创 2021-09-12 21:59:15 · 38 阅读 · 0 评论 -
103. 二叉树的锯齿形层序遍历
103. 二叉树的锯齿形层序遍历class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); if(root == null) return res; Deque<TreeNode> st .原创 2021-09-12 21:02:31 · 47 阅读 · 0 评论 -
102. 二叉树的层序遍历
102. 二叉树的层序遍历class Solution { public List<List<Integer>> levelOrder(TreeNode root) { Deque<TreeNode> st = new LinkedList<>(); List<List<Integer>> res = new LinkedList<>(); if(root ==原创 2021-09-12 17:16:25 · 47 阅读 · 0 评论 -
121. 买卖股票的最佳时机
121. 买卖股票的最佳时机class Solution { public int maxProfit(int[] prices) { int profit = 0; int min = prices[0]; for(int i = 1;i < prices.length;i++){ if(prices[i] > min) profit = Math.max(profit,prices[i] - min);.原创 2021-09-10 22:40:30 · 44 阅读 · 0 评论 -
160. 相交链表
160. 相交链表public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { Set<ListNode> visited = new HashSet<ListNode>(); ListNode temp = headA; while (temp != null) { vis原创 2021-09-10 22:29:09 · 76 阅读 · 0 评论 -
21. 合并两个有序链表
21. 合并两个有序链表class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head1 = l1; ListNode head2 = l2; ListNode dummy = new ListNode(0); ListNode res = dummy; while(head1 != nul原创 2021-09-10 21:53:17 · 42 阅读 · 0 评论 -
53. 最大子序和
53. 最大子序和class Solution { public int maxSubArray(int[] nums) { int sum = 0; int max = nums[0]; for(int num:nums){ if(sum > 0){ sum += num; }else{ sum = num; .原创 2021-09-10 21:43:57 · 39 阅读 · 0 评论 -
141. 环形链表
141. 环形链表public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> seen = new HashSet<>(); while(head != null){ if(!seen.add(head)){ return true; }原创 2021-09-10 21:31:22 · 52 阅读 · 0 评论 -
15. 三数之和
15. 三数之和又做一遍啊,双指针class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new LinkedList<>(); Arrays.sort(nums); if(nums.length < 3) return res; f原创 2021-09-07 21:28:55 · 50 阅读 · 0 评论 -
912. 排序数组
912. 排序数组class Solution { public int[] sortArray(int[] nums) { quickSort(nums,0,nums.length - 1); return nums; } public void quickSort(int[] a, int l,int r){ int q = randomizedPartition(a,l,r); if(q > l) qu原创 2021-09-07 21:12:56 · 50 阅读 · 0 评论 -
25. K 个一组翻转链表
25. K 个一组翻转链表/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next原创 2021-09-07 07:32:29 · 37 阅读 · 0 评论 -
215. 数组中的第K个最大元素
215. 数组中的第K个最大元素class Solution { Random random = new Random(); public int findKthLargest(int[] nums, int k) { return quicksort(nums,0,nums.length - 1,nums.length - k); } public int quicksort(int[] nums,int l, int r,int k){原创 2021-09-06 19:23:46 · 41 阅读 · 0 评论 -
146. LRU 缓存机制
146. LRU 缓存机制class LRUCache { public class DoubleListNode{ private int key; private int value; DoubleListNode prev; DoubleListNode next; public DoubleListNode(){}; public DoubleListNode(int key,int val原创 2021-09-06 04:55:11 · 122 阅读 · 0 评论 -
3. 无重复字符的最长子串
3. 无重复字符的最长子串class Solution { public int lengthOfLongestSubstring(String s) { if(s.length() == 0) return 0; Map<Character,Integer> map = new HashMap<>(); int l = 0; int r = 0; int max = 0;原创 2021-09-05 22:24:22 · 56 阅读 · 0 评论