273. Integer to English Words
摘要:public class Solution { private String[] lessThanTwenty = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven
阅读全文
posted @
2022-01-28 06:20
阳光明媚的菲越
阅读(28)
推荐(0)
953. Verifying an Alien Dictionary
摘要:To solve this problem, we need two steps: 1. Convert "order" string to a Hashmap, the key is the characor in the "order" string, value is its index. 2
阅读全文
posted @
2022-01-28 04:54
阳光明媚的菲越
阅读(41)
推荐(0)
921. Minimum Add to Make Parentheses Valid
摘要:这道题是典型的括号题,一般来说,括号题用stack做,但是这道题因为太简单了,连stack都不用, 就用int就好了,时间复杂度O(n)。 public int minAddToMakeValid(String s) { int count = 0; int res = 0; for(int i=0
阅读全文
posted @
2022-01-28 04:31
阳光明媚的菲越
阅读(28)
推荐(0)
1143. Longest Common Subsequence
摘要:找两组数据的最大数值,可以自顶向下写个递归,把可能重复的部分记录下来,用一个二维数组来存储: class Solution { private Integer[][] dp; private int m, n; public int longestCommonSubsequence(String t
阅读全文
posted @
2022-01-25 07:40
阳光明媚的菲越
阅读(35)
推荐(0)
14. Longest Common Prefix
摘要:第一种算法,不需要任何排序的暴力算法,一个字符一个字符,一个字符串一个字符串去比较,时间复杂度O(m*n), m是数组长度,n是最短字符串的长度。 public String longestCommonPrefix(String[] strs) { StringBuilder res = new S
阅读全文
posted @
2022-01-25 06:58
阳光明媚的菲越
阅读(38)
推荐(0)
277. Find the Celebrity (k2)
摘要:这道题是一道Graph题目,关于这种人际关系网,谁认识不认识谁的题目,用indegree,outdegree是没问题的,时间复杂度是O(n2): /* The knows API is defined in the parent class Relation. boolean knows(int a
阅读全文
posted @
2022-01-25 03:22
阳光明媚的菲越
阅读(41)
推荐(0)
720. Longest Word in Dictionary(k1)
摘要:这道题有两种解法,第一解法,很好理解,利用HashSet,时间复杂度O(n). public String longestWord(String[] words) { Arrays.sort(words); Set<String> set = new HashSet<>(); String res
阅读全文
posted @
2022-01-24 14:11
阳光明媚的菲越
阅读(59)
推荐(0)
696. Count Binary Substrings
摘要:这道题,最重要的是要能观察出,连续的0和连续的1之间的关系——每一组连续的0和连续的1可以贡献出:Math.min(连续0,连续1) 下面的两个算法都可以beat 100%,时间复杂度O(n). public int countBinarySubstrings(String s) { int res
阅读全文
posted @
2022-01-20 04:57
阳光明媚的菲越
阅读(44)
推荐(0)
1351. Count Negative Numbers in a Sorted Matrix
摘要:这道题的最简单的算法如下,时间复杂度O(m*n) public int countNegatives(int[][] grid) { int res = 0; for(int i=0;i<grid.length;i++){ for ( int j=0;j<grid[0].length;j++){ i
阅读全文
posted @
2022-01-20 04:23
阳光明媚的菲越
阅读(48)
推荐(0)
1213. Intersection of Three Sorted Arrays
摘要:这道题最简单的思路是用三个set,把三个数组的数放在set中,然后检查set1中的每个数是不是在set2和set3中,但是这样做的缺点是,set不是sorted的,最后要对结果排序,时间复杂度最坏情况是O(nlogn) (n是三个数组中的最小长度). public List<Integer> arr
阅读全文
posted @
2022-01-19 08:48
阳光明媚的菲越
阅读(52)
推荐(0)
2089. Find Target Indices After Sorting Array
摘要:这道题的最简单算法,时间复杂度O(nlogn): public List<Integer> targetIndices(int[] nums, int target) { Arrays.sort(nums); List<Integer> res = new ArrayList<>(); for(in
阅读全文
posted @
2022-01-19 05:16
阳光明媚的菲越
阅读(105)
推荐(0)
Binary Search的一般规律
摘要:如果能明确的确定要返回的值是什么,用三个判断,用下面这套模版,例如 704. Binary Search: public int search(int[] nums, int target) { int l=0, r = nums.length-1, mid=0, res = -1;while(l<
阅读全文
posted @
2022-01-18 15:44
阳光明媚的菲越
阅读(59)
推荐(0)
540. Single Element in a Sorted Array
摘要:这道题如果用暴力解法做非常简单,但是题目有要求:Your solution must run in O(log n) time and O(1) space. 如果看到时间复杂度O(logn),那么就一定要想到binary search。需要注意的是,在做这类找数题的时候,一定要看清楚是让返回ind
阅读全文
posted @
2022-01-18 15:29
阳光明媚的菲越
阅读(31)
推荐(0)
704. Binary Search
摘要:这道理一点没有弯弯绕绕,直接告诉你考binary search,那咱也不客气了,直接上算法,beat 100%,时间复杂度就不用啰嗦了。 public int search(int[] nums, int target) { int l=0, r = nums.length-1, mid=0; wh
阅读全文
posted @
2022-01-18 14:32
阳光明媚的菲越
阅读(29)
推荐(0)
4. Median of Two Sorted Arrays
摘要:这道题最简单的思路是把两个数组合并起来,再排序,如果数组长度是奇数,return 中间的那个数,如果数组长度是偶数,return 中间两个数的平均值。时间复杂度 O(nlogn)用于数组排序。 public double findMedianSortedArrays(int[] nums1, int
阅读全文
posted @
2022-01-18 06:49
阳光明媚的菲越
阅读(49)
推荐(0)
1891. Cutting Ribbons
摘要:这道题,要求cut ribbons,怎么cut都行,只要能cut出k条丝带就行,不是每条丝带都需要被cut,cut完没用的可以丢弃,求能cut出k条丝带的情况下,丝带的最大长度。你可以想象这样的场景,客户给你一堆丝带,让你帮忙cut出几条丝带,希望丝带越长越好,剩下的丝带就不要了。 暴力求解方法很简
阅读全文
posted @
2022-01-16 15:56
阳光明媚的菲越
阅读(272)
推荐(0)
138. Copy List with Random Pointer
摘要:这道题咋看挺复杂,又要clone node还要clone random link,但其实只要用一个HashMap就可以轻松解决,以下是我的算法,先clone node和node的next link,然后clone node的random link,时间复杂度O(n): public Node cop
阅读全文
posted @
2022-01-14 03:47
阳光明媚的菲越
阅读(36)
推荐(0)
791. Custom Sort String
摘要:一拿到这道题,我首先想的是用binary search做,但是因为结果String的长度不是固定的,用binary search很难实现。所以我写了第一个brute force的算法,时间复杂度是O(n2), 效率很低,beat 5%,而且很容易考虑不到edge case而出错。 Map<Chara
阅读全文
posted @
2022-01-14 02:47
阳光明媚的菲越
阅读(49)
推荐(0)
162. Find Peak Element
摘要:这道题里面有个隐藏条件:nums[-1] = nums[n] = -∞, 这就意味着数组最边上的两个数也有可能符合条件被返回,算法如下: public int findPeakElement(int[] nums) { int i=0; for(;i<nums.length-1;i++){ if(n
阅读全文
posted @
2022-01-04 05:53
阳光明媚的菲越
阅读(39)
推荐(0)
1047. Remove All Adjacent Duplicates In String
摘要:这道题很简单,其实是一个Stack的问题, 但是不用Stack,用一个StringBuilder就可以解决,时间复杂度和空间复杂度都是O(n). public String removeDuplicates(String s) { StringBuilder sb = new StringBuild
阅读全文
posted @
2022-01-03 11:50
阳光明媚的菲越
阅读(30)
推荐(0)
636. Exclusive Time of Functions
摘要:这道题很明显,需要用到stack,我一开始的想法是用两个stack,一个存functions,一个存start times,算法如下: package stack; import java.util.List; import java.util.Stack; public class Exclusi
阅读全文
posted @
2022-01-03 09:33
阳光明媚的菲越
阅读(44)
推荐(0)
680. Valid Palindrome II
摘要:这道题的暴力解法很简单,先check如果不删除任何字符,是否字符串是回文,如果不是,再挨个删除每个字符,check删除字符之后是否是回文。 时间复杂度O(n2), n是字符串s的长度,字符串很长的情况下会TLE,算法如下: class Solution { public boolean validP
阅读全文
posted @
2022-01-03 05:25
阳光明媚的菲越
阅读(79)
推荐(0)
348. Design Tic-Tac-Toe
摘要:Assume the following rules are for the tic-tac-toe game on an n x n board between two players: A move is guaranteed to be valid and is placed on an em
阅读全文
posted @
2022-01-02 15:47
阳光明媚的菲越
阅读(67)
推荐(0)
295. Find Median from Data Stream
摘要:当我拿到这道题的时候,第一时间想到的就是如下的暴力解法,时间复杂度:O(nlogn)+O(1)≃O(nlogn),空间复杂度:O(n). 因为暴力解法每次都要对整个list做sort,时间复杂度是nlog(n), 对于大数据量的test case,会TLE. class MedianFinder {
阅读全文
posted @
2022-01-01 07:19
阳光明媚的菲越
阅读(58)
推荐(0)