![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 72
Mike_CSU
这个作者很懒,什么都没留下…
展开
-
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路:杨辉三角 很简单的逻辑判断 但看了某大神的代码 还原创 2017-06-19 12:41:49 · 331 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the原创 2017-07-07 15:44:56 · 286 阅读 · 0 评论 -
189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].题意:一个简单的数组旋转,注意当k大于数组长度时,相当于对数组旋转k%len(数组长度)次原创 2017-07-07 16:46:16 · 270 阅读 · 0 评论 -
88. 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 add原创 2017-07-09 20:15:56 · 305 阅读 · 0 评论 -
Combination Sum系列(I,II,III) dfs做的
39. Combination SumGiven a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same rep原创 2017-07-19 13:11:19 · 497 阅读 · 0 评论 -
231,338,326,Power of Two(Three,Four)Counting Bits 类似的技巧题
231. Power of TwoGiven an integer, write a function to determine if it is a power of two.题意:一个数是否 2的n次方的判断思想: 把该数n转换为2进制,与该数n-1进行&操作 可以判断出是否为2^nC++ AC代码:Time O(1) Space O(1)class原创 2017-07-23 14:49:48 · 402 阅读 · 0 评论 -
565. Array Nesting(dfs 血的教训)
为什么说是血的教训呢,因为明明自己想的思路与那些AC的人思路一样而自己偏偏超时,又爆内存,觉得很奇怪,偏偏又找不出问题所在这道题,看完题意就知道dfs的思路了,却因为很小的细节卡了我2个小时!!!!! 泪崩原创 2017-07-16 22:26:24 · 927 阅读 · 0 评论 -
大数相加,相减,相乘算法(JAVA代码)
415. Add StringsGiven two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 con原创 2017-07-26 15:49:06 · 2148 阅读 · 0 评论 -
LeetCode:Subsets I II
求集合的所有子集问题LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain转载 2017-09-28 15:03:02 · 378 阅读 · 0 评论 -
LeetCode Maximum Subarray Maximum Product Subarray DP问题
Maximum Subarray题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the cont原创 2017-10-10 15:46:12 · 388 阅读 · 0 评论 -
LeetCode:Majority Element I II 投票算法
投票算法因为这两道题涉及到投票算法的思想,所以先从什么是投票算法入手。Boyer-Moore majority vote algorithm(摩尔投票算法)是一种在线性时间O(n)和空间复杂度的情况下,在一个元素序列中查找包含最多的元素。在它最简单的形式就是,查找最多的元素,也就是在输入中重复出现超过一半以上(n/2)的元素。如果序列中没有最多的元素,算法不能检测到正确结果原创 2017-10-22 19:04:13 · 822 阅读 · 0 评论 -
leetCode Single Number I II III
Single Number系列Single Number IGiven an array of integers, every element appears twice except for one. Find that single one.题目描述:给出一个数组arr,数组中每个元素都仅出现两次,除了其中一个数只出现一次,找出那个数。思想:主要的思想是用异或来原创 2017-12-08 15:11:29 · 339 阅读 · 0 评论 -
算法:n&(n-1)的总结
基本原理: n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子: n = 10100(二进制),则(n-1) = 10011 ==》n&(n-1) = 10000 可以看到原本最低位为1的那位变为0。以下几个应用:1. 求某一个数的二进制表示中1的个数(leetcode 191)class Solution {public...原创 2018-02-09 14:48:35 · 1534 阅读 · 0 评论 -
63. Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2017-07-05 14:16:55 · 256 阅读 · 0 评论 -
62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2017-07-05 14:07:22 · 309 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O原创 2017-06-07 20:52:12 · 499 阅读 · 0 评论 -
119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?Subscrib原创 2017-06-20 16:02:20 · 283 阅读 · 0 评论 -
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] ha原创 2017-06-08 21:47:29 · 260 阅读 · 0 评论 -
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority elemen原创 2017-06-08 20:59:55 · 480 阅读 · 0 评论 -
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2017-06-07 20:48:32 · 418 阅读 · 0 评论 -
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2017-06-09 18:24:38 · 300 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
题目:Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24原创 2017-07-02 23:27:05 · 1320 阅读 · 0 评论 -
27. Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memo原创 2017-07-02 23:39:50 · 381 阅读 · 0 评论 -
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at原创 2017-06-23 17:43:49 · 246 阅读 · 0 评论 -
540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.Example 1:Input: [原创 2017-06-23 17:45:53 · 349 阅读 · 0 评论 -
66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digi原创 2017-06-26 14:11:12 · 410 阅读 · 0 评论 -
162. Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in原创 2017-06-26 14:18:19 · 361 阅读 · 0 评论 -
Best Time to Buy and Sell Stock(股票的价值系列 整理总结)
股票的最大价值I (leetcode 121)题目:假设把某股票的价格按照时间先后顺序存储再数组中,请问买卖该股票一次可能获得的最大利润是多少?例如,一只股票在某些时间节点的价格为{9,11,8,5,7,12,16,14}。如果5买入,16迈出,则利润为11.思路:定义一个函数diff(i) 表示为卖出价格为数组下标为i的元素时获得的最大利润。显然只要找到前i-1个数中最小的元素,就能算出当前的最...原创 2018-07-15 21:34:18 · 356 阅读 · 0 评论