leetcode

LeetCode 945. 使数组唯一的最小增量

余生长醉 提交于 2020-03-23 01:57:44
我的LeetCode刷题源码[GitHub]: https://github.com/izhoujie/Algorithmcii LeetCode 945. 使数组唯一的最小增量 题目 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1。 返回使 A 中的每个值都是唯一的最少操作次数。 示例 1: 输入:[1,2,2] 输出:1 解释:经过一次 move 操作,数组将变为 [1, 2, 3]。 示例 2: 输入:[3,2,1,2,1,7] 输出:6 解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。 可以看出 5 次或 5 次以下的 move 操作是不能让数组的每个值唯一的。 提示: 0 <= A.length <= 40000 0 <= A[i] < 40000 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解题思路 思路1-先排序,再从左向右累加每两个临近数需要的+1操作数; Arrays.sort(A)排序; 顺次比较并记录将后一个数变为前一个数+1数所需要的操作数; 例子

LeetCode All in One 题目讲解汇总(持续更新中...)

混江龙づ霸主 提交于 2020-03-21 06:32:49
Given a non-empty string s , you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. Note: The string will only contain lowercase characters a-z. The maximum length of the string is 50000. 这道题是之前那道 Valid Palindrome 的拓展,还是让我们验证回复字符串,但是区别是这道题的字符串中只含有小写字母,而且这道题允许删除一个字符,那么当遇到不匹配的时候,我们到底是删除左边的字符,还是右边的字符呢,我们的做法是两种情况都要算一遍,只要有一种能返回true,那么结果就返回true。我们可以写一个子函数来判断字符串中的某一个范围内的子字符串是否为回文串,参见代码如下: 解法一: class Solution { public: bool validPalindrome(string s) {

leetcode1371

我们两清 提交于 2020-03-19 11:01:34
1 class Solution { 2 public int findTheLongestSubstring(String s) { 3 int n = s.length(); 4 Map<Character, Integer> map = new HashMap<>(); 5 map.put('a', 0); 6 map.put('e', 0); 7 map.put('i', 0); 8 map.put('o', 0); 9 map.put('u', 0); 10 int l = 0, maxlen = 0; 11 for (int r = 0; r < n; r++) { 12 char curr = s.charAt(r); 13 if (map.containsKey(curr)) { 14 map.put(curr, map.get(curr) + 1); 15 } 16 Map<Character, Integer> temp = new HashMap<>(); 17 18 temp.put('a', map.get('a')); 19 temp.put('e', map.get('e')); 20 temp.put('i', map.get('i')); 21 temp.put('o', map.get('o')); 22 temp.put('u', map

Leetcode 佛系刷题 每天动动脑

旧巷老猫 提交于 2020-03-17 17:44:59
Leetcode 佛系刷题 Java代码 11. Container With Most Water 7. Reserver Integer https://leetcode.com/problemset/all/ 没有特殊标明的话,贴上来的代码都是在leetcode上跑成功,速度100%的。 11. Container With Most Water 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, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. The above vertical lines are represented by array [1,8,6,2,5,4

LeetCode:最接近的三数之和

旧城冷巷雨未停 提交于 2020-03-17 10:03:06
刷题神器:LeetCode官方网站 一、题目还原 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 二、解题思路 ①三数之和的思路和上一题一样 Solution 2 双指针 ① EdgeCase:数组长度小于3或者等于三但是相加不等于0的,返回空 ② 将数组进行排序 ③ 定义左右两个下标,记为left = 0和right = nums.length - 1 ④ 以第一个数num为基准,left和right分别从第二个数和最后一个数开始计算,如果num+nums[left]+nums[right] == 0则left ++; right --;注意,如果left的下一个与之前数字相同继续left++,right同理 ⑤ 注意每次循环的基准数如果和上一次相同直接跳过进行下一次循环 ② 定义一个diff表示三数之和与target的差,如果差越小,就取当前的和 三、代码展示 ① main函数 public static void main ( String [ ] args ) { int

LeetCode:Integer to English Words

試著忘記壹切 提交于 2020-03-16 14:11:29
某厂面试归来,发现自己落伍了!>>> 1、题目名称 Integer to English Words(按英语读法输出数字对应单词) 2、题目地址 https://leetcode.com/problems/integer-to-english-words/ 3、题目内容 英文:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 中文:给出一个非负整数,输出该数字在英语对应的单词,数字小于2^31-1 例如:下面是三个转换的例子 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 4、解题方法 其实一开始我把题目想复杂了。这道题不用考虑英语中隔位要写“And”的情况。如101,在英语中的念法为“One Hundred And One”,在这道题中,只需要输出“One Hundred One”就可以了。因此

LeetCode 17. Letter Combinations of a Phone Number (电话号码的字母组合)

纵饮孤独 提交于 2020-03-16 07:43:42
题目标签:Backtracking   建立一个hashmap 把数字 对应 字母 存入 map;   利用dfs,每次存入一个 char,当 chars 达到 digtis 的size 返回,具体看code。    Java Solution: Runtime: 0 ms, faster than 100.00 % Memory Usage: 38.7 MB, less than 6.16 % 完成日期:12/15/2019 关键点:HashMap class Solution { List<String> res; HashMap<Character, String> map; public List<String> letterCombinations(String digits) { res = new ArrayList<>(); map = new HashMap<>(); StringBuilder comb = new StringBuilder(); if(digits.length() == 0) return res; map.put('2', "abc"); map.put('3', "def"); map.put('4', "ghi"); map.put('5', "jkl"); map.put('6', "mno"); map.put('7',

LeetCode 784. Letter Case Permutation (字母大小写全排列 )

╄→гoц情女王★ 提交于 2020-03-16 07:38:32
题目标签:Backtracking   用dfs,只对字母分别递归小写 和 大写,具体看code。    Java Solution: Runtime: 1 ms, faster than 100.00 % Memory Usage: 42.7 MB, less than 8.00 % 完成日期:12/15/2019 关键点:dfs class Solution { public List<String> letterCasePermutation(String S) { List<String> result = new ArrayList<>(); DFS(result, S.toCharArray(), 0); return result; } private void DFS(List<String> res, char [] cArr, int pos) { if(pos == cArr.length) { res.add(new String(cArr)); return; } if(Character.isLetter(cArr[pos])) { // lower case cArr[pos] = Character.toLowerCase(cArr[pos]); DFS(res, cArr, pos+1); // upper case cArr[pos] =

LeetCode:Remove Nth Node From End of List 移除链表倒第n项

感情迁移 提交于 2020-03-14 19:11:44
1、题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2、题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3、题目内容 英文:Given a linked list, remove the n th node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如: 给出链表:1->2->3->4->5,给出 n = 2,返回的链表为:1->2->3->5 4、解题方法1 删去链表的倒数第n项,有两种办法,一是将链表翻转,把正数第n项删去,再将链表翻转回去。 Java代码如下: /** * 功能说明:LeetCode 19 - Remove Nth Node From End * 开发人员:Tsybius2014 * 开发时间:2015年8月6日 */ public class Solution { /** * 删除链表中倒数第N项(从1开始计数) * @param head 链表首节点 * @param n 正整数 * @return 删除后链表首节点 */ public ListNode removeNthFromEnd(ListNode head

leetcode 599. Minimum Index Sum of Two Lists

◇◆丶佛笑我妖孽 提交于 2020-03-12 05:44:05
目录 一、问题描述 二、代码实现 1、暴力法 2、使用HashMap https://leetcode.com/problems/minimum-index-sum-of-two-lists/ 从两个人喜欢的餐厅列表中找到共同喜欢并下标之和最小的餐厅(如果有多个则将它们都返回) 一、问题描述 测试用例: Example 1: Input: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] Output: ["Shogun"] Explanation: The only restaurant they both like is "Shogun". Example 2: Input: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["KFC", "Shogun", "Burger King"] Output: ["Shogun"] Explanation: The restaurant they both like and have the least index sum is "Shogun" with index