leetcode

LeetCode 001. Two Sum

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:07:54
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. You may assume that each input would have exactly one solution. Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 好像在《编程之美》里看到过,用hashset.时间复杂度O(N)。 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) {

LeetCode:Sum of Two Integers

允我心安 提交于 2019-12-11 16:00:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、题目名称 Sum of Two Integers(不使用加减法运算符的整数加法) 2、题目地址 https://leetcode.com/problems/sum-of-two-integers/ 3、题目内容 英文: Calculate the sum of two integers a and b , but you are not allowed to use the operator + and - . 中文: 计算两个整数的和,但不允许使用运算符+和- 4、解题方法1 虽然题目中明确表示不让用户使用加法运算符与减法运算符,不过下面两种写法还是能获得Accept: 1)Java代码:直接使用a+b作为返回值 public class Solution { public int getSum(int a, int b) { return a + b; } } 2)Java代码:使用a-(-b)作为返回值 public class Solution { public int getSum(int a, int b) { return a - (-b); } } 5、解题方法2 解题方法1中列举的两种方法虽然能获得Accept,但以这种方式解题明显不是命题人的本意。既然不让用加减法解决两数求和的问题

LeetCode:Add Two Numbers

谁都会走 提交于 2019-12-11 15:56:28
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2、题目地址 https://leetcode.com/problems/add-two-numbers/ 3、题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 中文:给定两个链表,其中的元素都是非负整数。将两个链表的对应位置相加,如果两数相加需要进位,则将进位带到下一组加法。返回相加后的新链表。 例如:给出的链表为(2 -> 4 -> 3)和(5 -> 6 -> 4),则返回的结果为:7 -> 0 -> 8 4、解题方法1 我最开始的思路是,设两个链表为l1和l2,则依次向后遍历两链表。每次遍历都需要考虑以下三点: 1、考虑l1和l2有一个链表里对应位置为空的情况 2、考虑新链表根节点和非根节点的情况 3、考虑进位的情况

LeetCode 102. Binary Tree Level Order Traversal

喜欢而已 提交于 2019-12-11 15:06:19
文章目录 版权声明 1. LeetCode 102 2. 带有层数参数的递归 3. 区分层数的迭代 References 版权声明 LeetCode 系列笔记来源于 LeetCode 题库 1 ,在个人思考的基础之上博采众长,受益匪浅;故今记此文,感怀于心,更多题解及程序,参见 Github 2 ; 该系列笔记不以盈利为目的,仅用于个人学习、课后复习及交流讨论; 如有侵权,请与本人联系(hqpan@foxmail.com),经核实后即刻删除; 本文采用 署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 协议发布; 1. LeetCode 102 递归: 时间复杂度: O ( n ) O(n) O ( n ) ; 空间复杂度: O ( n ) O(n) O ( n ) ; 迭代: 时间复杂度: O ( n ) O(n) O ( n ) ; 空间复杂度: O ( n ) O(n) O ( n ) ; 2. 带有层数参数的递归 解题思路: 为将各节点的值分层存入二维 List 中,应使用 BFS 进行层次遍历; 难点:遍历过程中需要判断某个节点位于哪一层; 解决方案:执行广度优先搜索的函数应具备两个形参,即节点和层数; // Approach 1: Recursion class Solution { List < List < Integer > >

LeetCode:合并K个排序链表

风流意气都作罢 提交于 2019-12-11 00:54:32
题目: 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1->1->2->3->4->4->5->6 我的答案: 解法一:暴力法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeKLists(ListNode[] lists) { //ListNode node = new ListNode(0); if(lists==null||lists.length==0){ return null; } //node.next = lists.get(0); ListNode temp = lists[0]; for(int i=1;i<lists.length;i++){ temp = mergeTwoLists(temp,lists[i]); } return temp; } public ListNode mergeTwoLists(ListNode l1,ListNode l2){

leetcode滑动窗口题集

╄→尐↘猪︶ㄣ 提交于 2019-12-10 14:28:48
滑动窗口是一类题目,虽然其中有的题目可以有其他的或者更简单的解法,但是作为一个通用解法,我们在有限的时间里还是可以用的,具体在leetcode中有几题,有人已经总结了,而且说的很好 leetcode滑动窗口解决 我这几题都是用java来写的,在用java写的过程中呢,遇到一些问题,比如非常容易忽视的问题,Integer类型的,要用equals方法比较大小,否则当数值大于127的时候会出错。 以最小覆盖字串为例,模板中必有的成分是left,right,needs,windows,match,另外注意两个while循环,一个right++,left++; 注意match-- 处的注解 public String minWindow(String s, String t) { String result =""; int left = 0; int right = 0; int minLength = Integer.MAX_VALUE; int resultLeft= 0; int resultRight = 0; int match = 0; HashMap<Character,Integer>needs = new HashMap<>(); HashMap<Character,Integer>windows = new HashMap<>(); for(int i=0;i<t

Baozi Leetcode solution 54: Sprial Matrix

流过昼夜 提交于 2019-12-10 12:48:18
Problem Statement Given a matrix of m x n elements ( m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Problem link Video Tutorial You can find the detailed video tutorial here Youtube B站 Thought Process It's a straight forward implementation question, we can simply just simulate the spiral order and print. You can choose to do it either iteratively (see reference to download the official

【LeetCode】455. Assign Cookies (java实现)

瘦欲@ 提交于 2019-12-10 08:50:19
原题链接 https://leetcode.com/problems/assign-cookies/ 原题 Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Note: You may assume the greed factor is always positive. You cannot assign more than one

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

梦想的初衷 提交于 2019-12-10 05:31:53
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects. You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. When you finish a

leetcode阶段总结——求众数

半世苍凉 提交于 2019-12-10 05:30:08
相关题目 169. 多数元素 229. 求众数 II 摩尔投票法 摩尔投票法基于这样一个事实,当一个数的重复次数超过数组长度的一半,每次将两个不相同的数删除,最终剩下的就是要找的数。 为了解释清楚这个问题,首先来看leetcode的第169题。 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 解决这个题的思路是这样的: 我们维护一个计数器,如果遇到一个我们目前的候选众数,就将计数器加一,否则减一。只要计数器等于 0 ,我们就将 nums 中之前访问的数字全部 忘记 ,并把下一个数字当做候选的众数。这样到最后,总能找到符合要求的数。 为什么呢?考虑这个问题:当我们“减到0重新换个数开始计数”,实际上我们从原数组中忽略了2n个数,其中有n个是减到零之前那个候选的数,另外n个数不知道是什么数,反正不是那个候选的数。也就是说,可以看作删掉了n组元素,每组元素包括两个元素,这两个元素是不同的。 这一过程可以看作“抵消”。什么意思呢,最差情况下,每一对中都有一个众数,那么一个众数抵消一个非众数,由于本题中的众数是大于出现次数一半的,所以抵消之后,剩下的数组中