
leetcode
寂寞灵魂
这个作者很懒,什么都没留下…
展开
-
Network Delay Time
宽度搜索 bfs。此题所求为时间而非距离,故最终结果为到某个节点的最大值。此题的输入里,会有如下情况。环;多路时间比单路时间短;多路时间与单路时间相同。class Solution { public int networkDelayTime(int[][] times, int N, int K) { if (times == null) { ...原创 2020-05-01 12:07:01 · 221 阅读 · 0 评论 -
Remove Comments
this problem needs to be dealed character by character each line, as there’re multi-cases.I just copy one solution here.Steps:Iterate each line from the input document.Inside the line, iterate ea...原创 2020-05-01 10:15:49 · 270 阅读 · 0 评论 -
Map Sum Pairs
things like prefix should use Trie. I draw a picture to display this solution./**Runtime: 12 ms, faster than 56.55% of Java online submissions for Map Sum Pairs.Memory Usage: 40 MB, less than 14....原创 2020-04-28 16:11:36 · 165 阅读 · 0 评论 -
Maximum Length of Repeated Subarray.
dp.class Solution { public int findLength(int[] A, int[] B) { if (A == null || A.length == 0 || B == null || B.length == 0) { return 0; } int[][] dp = new i...原创 2020-04-28 15:39:13 · 127 阅读 · 0 评论 -
Check If N and Its Double Exist
i need to be even when checking set.contains(i / 2).class Solution { public boolean checkIfExist(int[] arr) { if (arr == null || arr.length == 0) { return false; } ...原创 2020-04-28 15:24:16 · 274 阅读 · 0 评论 -
Can Place Flowers
可以种植的情况(下标 i):i == 0 && arr[i+1] == o && arr[i] = 0;i == arr.length - 1 && arr[i] == 0 && arr[i-1] == 0;arr[i] == 0 && arr[i-1] == 0 && arr[i+1] == ...原创 2020-04-27 20:49:51 · 185 阅读 · 0 评论 -
Number of Steps toreduce a number to zero
/**Runtime: 45 ms, faster than 7.47% of Java online submissions for Number of Steps to Reduce a Number to Zero.Memory Usage: 40.2 MB, less than 100.00% of Java online submissions for Number of Step...原创 2020-04-27 15:22:42 · 231 阅读 · 0 评论 -
Sort Integers by the Number of 1 Bits
k快排,注意比较的方法,因为 bitCount 相同时也要排序。class Solution { public int[] sortByBits(int[] arr) { if (arr == null || arr.length == 0) { return new int[0]; } quickSort(arr,...原创 2020-04-27 14:32:13 · 176 阅读 · 0 评论 -
String matching in an array
class Solution { public List<String> stringMatching(String[] words) { if (words == null || words.length == 0) { return new ArrayList<>(); } HashSet&...原创 2020-04-27 13:59:28 · 150 阅读 · 0 评论 -
Maximum Score After Splitting a String
可以先统计所有 1 的数量。因为肯定要分隔的,所以 i < s.length() - 1 。/**Runtime: 2 ms, faster than 100.00% of Java online submissions for Maximum Score After Splitting a String.Memory Usage: 38.8 MB, less than 100.0...原创 2020-04-26 16:55:09 · 335 阅读 · 0 评论 -
Number of days
import java.time.LocalDate;import java.time.temporal.ChronoUnit;class Solution { public int daysBetweenDates(String date1, String date2) { if (date1 == null || date1.length() == 0 || da...原创 2020-04-26 16:08:31 · 200 阅读 · 0 评论 -
[LeetCode][easy]Create Target Array
读题读了好几遍才看明白,是往指定下标进行插入操作,而不是更新。/*Runtime: 3 ms, faster than 14.63% of Java online submissions for Create Target Array in the Given Order.Memory Usage: 39.7 MB, less than 100.00% of Java online sub...原创 2020-04-26 15:57:23 · 278 阅读 · 0 评论 -
[LeetCode][easy]Rank Transform of an Array
排序加 map 可以做。/**Runtime: 24 ms, faster than 79.61% of Java online submissions for Rank Transform of an Array.Memory Usage: 56.1 MB, less than 100.00% of Java online submissions for Rank Transform o...原创 2020-04-25 19:22:46 · 225 阅读 · 0 评论 -
[LeetCode][easy]Reformat The String
如果字母的数量和数字的数量之差为 0 或 1,则可以 reformat。注意如果之差为 1,那么多的那种字符应该先写。/**Runtime: 2 ms, faster than 98.95% of Java online submissions for Reformat The String.Memory Usage: 39.5 MB, less than 100.00% of Java ...原创 2020-04-25 18:36:32 · 244 阅读 · 0 评论 -
[LeetCode][easy]Longest Commong Prefix
分治法,l, mid, r 分为 (l, mid) 和 (mid+1, r)class Solution { public String longestCommonPrefix(String[] strs) { String result = ""; if (strs == null || strs.length == 0) { r...原创 2020-04-25 18:08:44 · 129 阅读 · 0 评论 -
[LeetCode][easy]Roman to Integer
从低位到高位,如果上一位代表的数值比该位小,则减,否则加。class Solution { private static HashMap<Character, Integer> map = new HashMap<>(); static { map.put('I', 1); map.put('V', 5)...原创 2020-04-25 17:39:07 · 108 阅读 · 0 评论 -
[leetcode][easy]Two Sum
class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; if (nums == null || nums.length == 0) { return result; } HashM...原创 2020-04-25 17:21:28 · 124 阅读 · 0 评论 -
leetcode53
求总和最大的子数组。 动态规划dp。 Suppose we’ve solved the problem for A[1 .. i - 1]; how can we extend that to A[1 .. i]? The maximum sum in the first I elements is either the maximum sum in the first i - 1 eleme原创 2017-03-03 19:59:41 · 518 阅读 · 0 评论 -
leetcode88
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hol原创 2017-03-03 20:31:08 · 359 阅读 · 0 评论 -
leetcode485、448、414
例如414,用java做的话好多坑,以后就用java刷leetcode了。//414public class Solution { public int thirdMax(int[] nums) { Integer max1 = null; //不能使用Integer.MIN_VALUE,因为题目里会有Integer.MIN_VALUE作输入 Integer原创 2017-02-25 14:03:14 · 531 阅读 · 0 评论 -
、118
//283public class Solution { public void moveZeroes(int[] nums) { int flag = 0; for(int i=0; i<nums.length; i++) { if(nums[i] != 0) { nums[flag++] = num原创 2017-02-26 22:04:54 · 357 阅读 · 0 评论 -
bfs和dfs:poj2386和leetcode130
poj2386#include <iostream>using namespace std;const int MAX_NM = 105;char ch[MAX_NM][MAX_NM];int m, n;void dfs(int x, int y) { ch[x][y] = '.'; for(int i=-1; i<=1; i++) { for(int j=-1;原创 2017-04-22 14:54:40 · 480 阅读 · 0 评论 -
leetcode121、122、123
今天发现leetcode比别的都好。。。因为面试面到121题。 121:class Solution {public: int maxProfit(vector<int>& prices) { size_t size = prices.size(); if(size <= 1) return 0; int min = INT_MAX,原创 2017-02-22 19:20:30 · 1156 阅读 · 0 评论