![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode刷题
风可。
机器视觉、深度学习、数据挖掘
展开
-
485. Max Consecutive Ones -- array、栈
485. Max Consecutive OnesGiven a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digi原创 2017-05-10 23:17:46 · 154 阅读 · 0 评论 -
Leetcode刷题前奏
被笔试题虐过之后奋发图强,立志增强编程能力,不被笔试题虐。刷题计划:按难度刷easy–>hard,增加题型多样性每天刷个一题多学多看原创 2017-05-08 11:17:11 · 183 阅读 · 0 评论 -
561. Array Partition I
561. Array Partition IGiven an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1原创 2017-05-08 18:38:48 · 180 阅读 · 0 评论 -
461. Hamming Distance
461. Hamming Distanceclass Solution(object): def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ x_bin = bin(x).replace('0原创 2017-05-08 19:36:41 · 156 阅读 · 0 评论 -
350. Intersection of Two Arrays II -- 双指针、哈希表、排序、二分搜索
350. Intersection of Two Arrays IIGiven two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]解1:双指针,并把结果添加到栈中。class Solution(obje原创 2017-05-17 09:52:55 · 349 阅读 · 0 评论 -
575. Distribute Candies
575. Distribute CandiesGiven an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You ne原创 2017-05-09 10:54:54 · 303 阅读 · 0 评论 -
557. Reverse Words in a String III
Reverse Words in a String IIIGiven a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Input: "Let's原创 2017-05-09 13:33:37 · 234 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree--二叉查找数
108. Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.# Definition for a binary tree node.# class TreeNode(obj原创 2017-05-18 15:01:10 · 195 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one an原创 2017-05-18 15:21:07 · 199 阅读 · 0 评论 -
500. Keyboard Row
500. Keyboard Row其实这种问题的通用解法是动态规划,但是我的水平不够所以还写不出来。Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.Example原创 2017-05-09 21:24:09 · 202 阅读 · 0 评论 -
412. Fizz Buzz
412. Fizz Buzz这是一题使用判断条件的题目,还考察了求余数,我使用了map()方法。Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number an原创 2017-05-09 21:49:12 · 180 阅读 · 0 评论 -
344. Reverse String
344. Reverse StringWrite a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".解: 可以直接用python的字符串处理,也可以用交换字符的位置来完成,交换位置的话需要先把字符串拆分成一个个字符,因为原创 2017-05-09 21:58:55 · 132 阅读 · 0 评论 -
496. Next Greater Element I -- 栈
496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corres原创 2017-05-10 10:27:03 · 199 阅读 · 0 评论 -
206. Reverse Linked List--链表翻转
链表翻转该开始想了很久,看代码也是乱,后来发现其实就是从前到后依次翻转箭头的指向,首先把第一个表指向第二个表的箭头翻转为指向None的箭头,head指向第二个表。 current相当于交换两杯水的中间的水杯。 用于存储中间变量。# Definition for singly-linked list.# class ListNode(object):# def __init__(sel原创 2017-05-16 14:53:43 · 319 阅读 · 0 评论 -
动态规划
动态规划的描述动态规划通常基于一个递推公式及一个或多个初始状态。当前子问题将由上一子问题的解推出。状态与状态转移方程“状态”用来描述该问题的子问题的解。 注意,大部分情况下,某个状态只与它前面出现的状态有关, 而独立于后面的状态。例题1一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度。 让我们沿用“入门”一节里那道简单题的思路来一步步找到“状态”和“状态转移方程”。原创 2017-05-26 11:24:07 · 268 阅读 · 0 评论 -
136. Single Number--字典、找单身狗
136. Single Number在情侣中找出单身狗,对单身狗暴击。Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you i原创 2017-05-10 23:39:33 · 230 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array -- set
448. Find All Numbers Disappeared in an ArrayTotal Accepted: 40636Total Submissions: 77545Difficulty: EasyContributors:yuhaowang001Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array)原创 2017-05-11 10:09:26 · 180 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree -- 二叉数、递归
104. Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.解1: 经典的二叉原创 2017-05-11 10:45:03 · 246 阅读 · 0 评论 -
389. Find the Difference --字符串的共同字符
389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the l原创 2017-05-11 23:42:47 · 211 阅读 · 0 评论 -
371. Sum of Two Integers
371. Sum of Two IntegersCalculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.解1: 厉害了,不使用加减,计算两个整数的和。 那么是否可以使用operato原创 2017-05-12 00:12:45 · 209 阅读 · 0 评论 -
226. Invert Binary Tree -- 二叉树、递归、交换、栈
226. Invert Binary TreeInvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1解一: 又是一个二叉树问题,这次直接想到了递归,并且确定停止条件和执行的命令,返回的参数。# Definitio原创 2017-05-12 00:19:14 · 288 阅读 · 0 评论 -
258. Add Digits -- 递归、字符串分解
258. Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2017-05-14 19:59:55 · 240 阅读 · 0 评论 -
455. Assign Cookies -- 贪心算法
455. 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原创 2017-05-14 20:13:36 · 351 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted -- 双指针、字典、二分搜索
167. Two Sum II - Input array is sortedGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum shoul原创 2017-05-14 21:21:16 · 218 阅读 · 0 评论 -
349. Intersection of Two Arrays --双指针、字典、二分查找、集合
349. Intersection of Two Arrays很经典的题目,可以使用双指针、字典、二分查找、集合来实现Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Solution 1:us原创 2017-05-15 00:31:49 · 327 阅读 · 0 评论 -
387. First Unique Character in a String -- 哈希表、字典
387. First Unique Character in a String – 哈希表、字典注:涉及到统计个数之类的题目,大部分都可以使用字典(hashmap)来解。Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.E原创 2017-05-15 23:36:13 · 190 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II -- greedy
122. Best Time to Buy and Sell Stock II很有意思的一道题目 怎么利益最大化? 贪婪算法就是要把问题分解成小问题,把全局最优集问题分为一个个局部最优解的小问题。 那么把全局利益最大化,转换为当前的局部利益最大,当第二天比第一天价格高的时候就第一天买和第二天卖,当第二天比第一天价格低的时候就不动。Say you have an array for which原创 2017-05-16 00:06:58 · 229 阅读 · 0 评论 -
169. Majority Element -- 字典
169. Majority ElementGiven 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 maj原创 2017-05-16 11:03:26 · 196 阅读 · 0 评论 -
202. Happy Number
202. Happy NumberWrite an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of原创 2017-05-18 23:57:57 · 206 阅读 · 0 评论