LeetCode 中等题解(1)】的更多相关文章

34 在排序数组中查找元素的第一个和最后一个位置 Question 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [-1, -1]. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: [3,4] 示例 2: 输入: nums = [5,7,7,8,8,10], target = 6 输出: [-1,-…
16 最接近的三数之和 Question 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). Answer # # @lc app=leetcode.cn id=16 lang=python3 #…
40 组合总和 II Question 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1…
31 下一个排列 Question 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. 以下是一些例子,输入位于左侧列,其相应输出位于右侧列. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 Answer # # @lc app=leetcode.cn id=31 lang=python3 # # [31] 下一个…
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-Solution/ ———————————————————————————————————————— ———————————————————————————————————————— LeetCode OJ 题解 LeetCode OJ is a platform for preparing tech…
Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的无偿付出连认同都得不到,很是失望.明白人都知道这是潜水伸手党的锅.但同时,截止写这段话时,我发现这篇Leetcode简略(得不能再简略的)题解得到了一千多赞,三千多收藏.所以,在我不向任何人做任何广告.收任何费用的前提下,麻烦在收藏的同时,顺手点个赞(对只收藏不点赞的行为表示明确鄙视).如果连点一个…
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://leetcode-cn.com/problems/two-sum/submissions/ 1. 暴力解法 Time complexity: O(n**2) Space complexity: O(n) "use strict"; /** * @author xgqfrms * @descrip…
子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [1,2,3],   [1,3],   [2,3],   [1,2],   [] ] 本题做过好几遍了, 之前用的是回溯法, 本次使用的是位运算的思想. 位运算解法: 将 \(nums\) 的每一个位置看作一个 \(bit\),如果某个 \(bit\) 为1,表示 \(nums[i]\) 需要加入子集…
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度. 如: 给出[100, 4, 200, 1, 3, 2], 最长的连续元素序列是[1, 2, 3, 4].返回它的长度:4. 你的算法必须有O(n)的时间复杂度 . 解法: 初始思路 要找连续的元素,第一反应一般是先把数组排序.但悲剧的是题目中明确要求了O(n)的时间复杂度,要做一次排序,是不能达…
深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给定一堆数,每个数可以用无限多次,问凑出目标数target的结果集,要求不能重复. 题解:爆搜,设计为dfs(cur, target),cur为当前用第i个数,这个数用0到target / candidates[cur]个. LeetCode 216 题目:给定数字1到9,从中选 k 个数,返回所有方…
50. Pow(x, n) (中等) double myPow(double x, int n) { ; unsigned long long p; ) { p = -n; x = / x; } else { p = n; } while (p) { ) ans *= x; x *= x; p >>= ; } return ans; } 96. Unique Binary Search Trees(很快) class Solution { public: int numTrees(int n)…
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode…
这是leetcode第一题,通过较为简单. 第一题用来测试的,用的c,直接暴力法过, /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target) { ]={}; ;i<numsSize-;i++) ;j<numsSize;j++) if(nums[i]+nums[j]==target){ a[]=i…
https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:  DFS AC代码: 1.递归 /…
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly o…
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-des…
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car"…
https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the to…
https://leetcode.com/problems/merge-sorted-array/ 题目: 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 hold additi…
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->…
https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->…
https://leetcode.com/problems/climbing-stairs/ 题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 1)递归:  超时 class Solution { public…
https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 数组模拟加法操做,注意首位进一的情况. class Solution { public: string addBinary(strin…
https://leetcode.com/problems/plus-one/ 题目: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 思路: 设置进1位,逢9进1.注意999这种情况要首位inser…
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is de…
https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as…
https://leetcode.com/problems/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)…
https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路: 我的思路是遍…
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 判断一个string是否另一个string的子序列并返回位置. naive法:遍历查找,复杂度O(mn). advance法还有Rabi…
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: 因为可以改变元…