
leetcode
文章平均质量分 80
AXIMI
这个作者很懒,什么都没留下…
展开
-
【leetcode】841. Keys and Rooms
提交代码class Solution { public boolean canVisitAllRooms(List<List<Integer>> rooms) { boolean[] access=new boolean[rooms.size()]; Arrays.fill(access, false); access[0] = true; Queue<Integer> next=new Linke原创 2020-06-18 09:21:53 · 262 阅读 · 0 评论 -
【leetcode】785. Is Graph Bipartite?
创建两个集合用于保存两组节点首先0放在集合1,0的邻节点放在集合21的邻节点是0和2,0已经在集合1里面了,把2也放到集合1里面去3的邻节点是0和2,都在集合1里面。所有节点都放在了集合1和2中,返回true(如果存放过程中出现矛盾返回false)提交代码class Solution { public boolean isBipartite(int[][] graph) { Set<Integer> g1=new HashSet<>(); S.原创 2020-06-16 22:38:58 · 225 阅读 · 0 评论 -
【leetcode】1482. Minimum Number of Days to Make m Bouquets
提交代码class Solution { public int minDays(int[] bloomDay, int m, int k) { int[] bd=bloomDay.clone(); Arrays.sort(bloomDay); int i=0,j=bloomDay.length-1,mid=(i+j)/2; while(i<j) { if(i==j-1) { if(hasBouquet(原创 2020-06-15 13:16:24 · 360 阅读 · 0 评论 -
【算法分析与设计】图:bellman-ford算法
介绍Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(SSSP:Single-Source Shortest Path)的算法。该算法由 Richard Bellman 和 Lester Ford 分别发表于 1958 年和 1956 年,而实际上 Edward F. Moore 也在 1957 年发布了相同的算法,因此,此算法也常被称为 Bellman-Ford-Moore 算法。Bellman-Ford 算法和 Dijkstra 算法同为解决单源最短路径的算法。对于带权有向图 G原创 2020-06-09 21:21:06 · 783 阅读 · 0 评论 -
【leetcode】743. Network Delay Time
bellman-ford算法提交代码class Solution { public int networkDelayTime(int[][] times, int N, int K) { int[][] graph = new int[N+1][N+1]; for(int i=0;i<N+1;i++) Arrays.fill(graph[i], -1); for(int[] t: times) graph[原创 2020-06-09 19:06:27 · 250 阅读 · 0 评论 -
【leetcode】684. Redundant Connection
提交代码class Solution { Map<Integer, List<Integer>> graph=new HashMap<>(); Set<Integer> allNodes = new HashSet<>(); public int[] findRedundantConnection(int[][] edges) { for(int[] e: edges) { if(findPath(e[0], e原创 2020-06-02 22:28:12 · 306 阅读 · 0 评论 -
【leetcode】332. Reconstruct Itinerary
欧拉通路提交代码class Solution { Map<String, PriorityQueue<String>> graph =new HashMap<>(); List<String> path=new LinkedList<>(); public List<String> findItinerary(List<List<String>> tickets) { for(L原创 2020-06-01 21:11:50 · 228 阅读 · 0 评论 -
【leetcode】399. Evaluate Division
提交代码class Solution { public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { String start, end; Map<String, Map<String, Double>> graph=new HashMap<>(原创 2020-05-29 13:24:05 · 194 阅读 · 0 评论 -
【Leetcode】378. Kth Smallest Element in a Sorted Matrix
二分法提交代码class Solution { public int kthSmallest(int[][] matrix, int k) { int low = matrix[0][0], high = matrix[matrix.length-1][matrix[0].length-1]; while(low<high) { int mid = low+(high-low)/2; int curRank = 0, j=matrix[0].le原创 2020-05-27 12:31:55 · 167 阅读 · 0 评论 -
【leetcode】310. Minimum Height Trees
解决思路以这棵树为例,首先MHT的根节点不会是叶子节点,所以删除0,1,2,5剩下的3,4就是MHT的根节点。即一层一层的删掉叶子节点,直到只剩下一个或者两个节点。提交代码class Solution{ public List<Integer> findMinHeightTrees(int n, int[][] edges) { List<Integer> res= new ArrayList<>(); List<Node> tre原创 2020-05-13 22:36:55 · 299 阅读 · 0 评论 -
【leetcode】387. First Unique Character in a String
提交代码class Solution { public int firstUniqChar(String s) { HashMap<Character, Integer> map=new HashMap<>(); for(int i=0;i<s.length();i++) map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0)+1); for(int原创 2020-05-11 13:18:01 · 184 阅读 · 0 评论 -
【leetcode】1443. Minimum Time to Collect All Apples in a Tree
用递归提交代码class Solution { public int minTime(int n, int[][] edges, List<Boolean> hasApple) { TreeNode[] nodes=new TreeNode[n]; for(int i=0;i<n;i++) nodes[i]=new TreeNode(i); for(int[] e:edges原创 2020-05-11 13:07:31 · 333 阅读 · 0 评论 -
【leetcode】1399. Count Largest Group
提交代码class Solution { public int countLargestGroup(int n) { int digitSum, maxSize=0,res=0; Map<Integer, Integer> map=new HashMap<>(); for(int i=1;i<=n;i++) { ...原创 2020-05-06 13:06:48 · 260 阅读 · 0 评论 -
【leetcode】208. Implement Trie (Prefix Tree)
提交代码class Trie { ArrayList<String> trie; /** Initialize your data structure here. */ public Trie() { trie=new ArrayList<>(); } /** Inserts a word into the tr...原创 2020-04-26 13:09:26 · 269 阅读 · 0 评论 -
【leetcode】395. Longest Substring with At Least K Repeating Characters
提交代码class Solution { public int longestSubstring(String s, int k) { if(s==null) return 0; int res=0, curMax=0; for(int i=0;i<=s.length()-k;i++) { curMax ...原创 2020-04-22 13:11:24 · 185 阅读 · 0 评论 -
【leetcode】1419. Minimum Number of Frogs Croaking
提交代码class Solution { public int minNumberOfFrogs(String croakOfFrogs) { ArrayList<Integer> allCroaks=new ArrayList<Integer>(); char[] chars=croakOfFrogs.toCharArray(); ...原创 2020-04-21 13:29:43 · 299 阅读 · 0 评论 -
【leetcode】295. Find Median from Data Stream
提交代码class MedianFinder { private ArrayList<Integer> nums; /** initialize your data structure here. */ public MedianFinder() { nums=new ArrayList<>(); } public...原创 2020-04-14 22:47:27 · 179 阅读 · 0 评论 -
【leetcode】347. Top K Frequent Elements
思路我的想法是用一张表来记录出现的数字及对应出现的次数。然后对所有数字的出现次数排名。然后出现排名前k的数字就是目标数字。提交代码class Solution { public List<Integer> topKFrequent(int[] nums, int k) { HashMap<Integer, Integer> map=new H...原创 2020-04-06 19:01:59 · 148 阅读 · 0 评论 -
【Leetcode】412. Fizz Buzz
提交代码class Solution { public List<String> fizzBuzz(int n) { List<String> res=new ArrayList<>(); for(int i=1;i<=n;i++) { if(i%3==0&&i%5==0) res...原创 2020-03-08 20:33:48 · 226 阅读 · 0 评论 -
【leetcode】454. 4Sum II
解题思路四个数组中,找出组合之和为0的个数。一般想到的是4个for循环,时间复杂度O(n^4),耗时太久了。可以考虑牺牲空间换取时间。将A B两个数组中数字的组合值用map保存起来,然后再遍历C D数组。综合时间复杂度O(N^2).提交代码class Solution { public int fourSumCount(int[] A, int[] B, int[] C, int[]...原创 2020-03-06 14:13:22 · 183 阅读 · 0 评论 -
【leetcode高频问题-DP】95.(Medium)Unique Binary Search Trees II
题目描述给出数字n,要求返回所有1-n能能构成的二叉搜索树的根节点数组。用递归当输入为0时,输出是空数组提交代码class Solution { public List<TreeNode> generateTrees(int n) { if(n==0) return new ArrayList<TreeNode>(); re...原创 2019-12-03 20:43:22 · 164 阅读 · 0 评论 -
【leetcode高频问题-DP】53.(Easy) Maximum Subarray
解题思路其实不用DP,用最简单的滑动窗口即可。提交代码class Solution { public int maxSubArray(int[] nums) { int curSum=nums[0],res=nums[0]; for(int i=1,len=nums.length;i<len;i++) { if(curSum+nums...原创 2019-10-31 19:55:49 · 150 阅读 · 0 评论 -
【leetcode】331.(Medium)Verify Preorder Serialization of a Binary Tree
解题思路这道题的意思是,检查一串数字是不是一棵树先序遍历的结果。做法是使用递归代码class Solution { public boolean isValidSerialization(String preorder) { if(preorder == null || preorder.length()==0) return true; Array...原创 2019-10-17 19:26:48 · 204 阅读 · 0 评论 -
【Leetcode】328.(Medium)Odd Even Linked List
解题思路设置两个链表头,一个串接单数节点,一个串接双数节点。最后将两个链表的数据连接起来。时间复杂度为O(n),是原始链表的长度空间复杂度为O(1),用于保存两个新创建的链表头。代码class Solution { public ListNode oddEvenList(ListNode head) { ListNode trueHead = new ListN...原创 2019-10-16 21:56:12 · 154 阅读 · 0 评论 -
【leetcode】292(Easy)Nim Game
解题思路这道题就是找规律,找到规律以后就比较简单提交代码class Solution {public boolean canWinNim(int n) { return n%4==0?false:true; }}运行结果原创 2019-10-13 20:23:51 · 187 阅读 · 0 评论 -
【leetcode】290 (Easy) Word Pattern
提交代码class Solution { public boolean wordPattern(String pattern, String str) { char[] chars = pattern.toCharArray(); String[] words = str.split(" "); if(chars.length!=words.length) ...原创 2019-10-13 20:08:00 · 123 阅读 · 0 评论 -
【leetcode】3.Longest Substring Without Repeating Characters(c语言)
Description:Given a string, find the length of the longest substring without repeating characters.Example1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Ex...原创 2018-09-24 09:51:36 · 386 阅读 · 0 评论 -
【leetcode】4.Median of Two Sorted Arrays(c语言)
Description:There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).You may assume...原创 2018-09-24 10:47:03 · 328 阅读 · 0 评论 -
【leetcode】5.Longest Palindromic Substring
Description:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example1:Input: “babad”Output: “bab”Note: “aba” is also a valid a...原创 2018-09-25 10:46:44 · 547 阅读 · 0 评论 -
【leetcode】2.Add Two Numbers(c语言)
Description:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two number...原创 2018-09-20 11:00:20 · 331 阅读 · 0 评论 -
【leetcode】11.Container With Most Water(c语言)
z原创 2018-10-11 09:54:59 · 154 阅读 · 0 评论 -
【leetcode】16. 3Sum Closest(Medium)(JAVA)
Description:Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that eac...原创 2018-10-11 09:53:22 · 183 阅读 · 0 评论 -
【leetcode】8.String to Integer(atoi)(Medium)(C)
Description:Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, star...原创 2018-10-11 10:09:25 · 121 阅读 · 0 评论 -
【leetcode】6.ZigZag Conversion(c语言)
Description:The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N...原创 2018-09-27 10:26:25 · 642 阅读 · 0 评论 -
【leetcode】11.Container With Most Water(c语言)
Description:Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, ...原创 2018-10-05 10:18:20 · 371 阅读 · 0 评论 -
【leetcode】21. Merge Two Sorted Lists(JAVA)
Description:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->...原创 2018-10-18 09:52:20 · 477 阅读 · 0 评论 -
【leetcode】13. Roman to Integer(C)
题目链接提交代码:int romanToInt(char* s) { int i,p = 0,numslen=0; int sum = 0; int* nums = (int*)malloc(30 * sizeof(int)); while (s[p] != '\0') { if (s[p] == 'M') nums[p] = 1000; else if (s...原创 2018-10-18 09:55:54 · 210 阅读 · 0 评论 -
【leetcode】18. 4Sum(C)
Description:Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives ...原创 2018-10-16 10:16:34 · 266 阅读 · 0 评论 -
【leetcode】9. Palindrome Number
Description:Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example1:Input: 121Output: trueExample2:Input: -121Output: f...原创 2018-10-16 10:21:24 · 247 阅读 · 0 评论 -
【leetcode】22. Generate Parentheses
[ 题目链接 ]提交代码(递归):class Solution { List<String> generateParenthesis(int n) { List<String> list=new ArrayList<>(); append(n,n,"",list); return list;} void append(in...原创 2018-10-19 09:04:57 · 168 阅读 · 0 评论