LeetCode
记录伪·ACM退役队员刷面试题的SX经历
MissZhou要努力
不晓日月,不辩兰艾,终日碌碌,安与燕雀相随乎
展开
-
leetcode 71. Simplify Path【文件路径表达式】
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"path = "/a/../../b/../c//.//", => "/c"pa原创 2018-10-29 11:52:16 · 313 阅读 · 0 评论 -
leetcode 73. Set Matrix Zeroes【设置行列为0】
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]E...原创 2018-10-31 21:25:38 · 196 阅读 · 0 评论 -
leetcode 75. Sort Colors【荷兰旗问题】
基于快排的思想回忆一下快排的思路:把第一个位置的作为pivot,从右遍历直到发现有比pivot小的,交换;从左遍历,直到发现有比pivot大的,交换注意这里交换的都是当前的low和high,直到low,high相遇,本轮停止如果数字里面只有0,1,2那么显然只需要一次while就可以结束思路依旧是l r currentcurrent==0 需要和low交换,使得0都在左侧...原创 2018-11-01 13:56:37 · 200 阅读 · 0 评论 -
leetcode 79. Word Search【dfs找方格中出现字符串】
https://leetcode.com/problems/word-search/description/Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where...原创 2018-11-02 11:11:31 · 214 阅读 · 0 评论 -
leetcode 80. Remove Duplicates from Sorted Array II【c++for循环】
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by mod...原创 2018-11-03 10:54:01 · 188 阅读 · 0 评论 -
leetcode61Rotate List【将列表后k个元素移动到前面】
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplan...原创 2018-10-28 12:10:10 · 318 阅读 · 0 评论 -
leetcode 62 Unique Paths 62 Unique Paths 62 Unique Paths 【走格子 简单dp】
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 bo...原创 2018-10-28 12:15:41 · 227 阅读 · 0 评论 -
leetcode 59. Spiral Matrix II【蛇形填数】
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.Example:Input: 3Output:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]就是leetcode 54Spiral ...原创 2018-10-27 12:26:25 · 268 阅读 · 0 评论 -
面试算法——双指针的应用leetcode 42. Trapping Rain Water
.给定一个非负数的数组,代表一个容器。例如数组[0,1,0,2,1,0,1,3,2,1,2,1],就是以下图形中黑色的部分。如果用这个容器接水的话,请问可以接多少水?还以这个数组为例, 可以接6格水,就是以下图形中蓝色的部分。 要求:实现时间复杂度O(N),额外空间复杂度O(1)的解法注:因为没地方提交,所以只写思路了QAQ做法:首先要注意的是,围出水的体积(面积)是总体的和,而不原创 2016-08-10 14:11:16 · 2420 阅读 · 0 评论 -
leetcode gas-station【最大序列变形】
There are N gas stations along a circular route, where the amount of gas at stationi isgas[i]. You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationi to its原创 2017-04-12 11:40:24 · 435 阅读 · 0 评论 -
leetcode 55. Jump Game [贪心]
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if原创 2017-04-12 13:37:47 · 469 阅读 · 0 评论 -
LeetCode 45 Jump Game II【贪心】
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to原创 2017-04-17 20:11:14 · 396 阅读 · 0 评论 -
#python练习#leetcode392. Is Subsequence【贪心】
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2017-07-06 13:00:49 · 626 阅读 · 0 评论 -
[leetcode]435. Non-overlapping Intervals
讲真,一点都不想写这个的题解被自己蠢哭了,告诉我这个题和看电视节目那个贪心入门有什么区别???怎么还能第一关键字是start???后一个和前一个比较的是end,当然是第一关键字是end排序啊!!!大二下开学还是老陈讲的啊………………这都能写错,还搜题解…………赶紧老实儿的一天刷一个题吧/** * Definition for an interval. * struct原创 2017-09-18 13:29:20 · 472 阅读 · 0 评论 -
[leetcode]376. Wiggle Subsequence 贪心
遇到间距=0的直接减去,连续是正数、负数的情况少减去一个我怎么12行的代码还要搜题解 mdzzclass Solution {public: int wiggleMaxLength(vector& nums) { int leftnum=nums.size(); if(leftnum==0||leftnum==1)return leftnu原创 2017-09-20 09:24:51 · 469 阅读 · 0 评论 -
面试经典题【两个栈模拟队列 两个队列模拟栈】
由于随便投的G社简历居然过初筛了,本着有那么一丢丢希望还是要拼一把的心理还是要准备一下,死皮赖脸让对象晚上看我编程暴露了一堆问题,写之前不沟通 代码逻辑 代码风格 变量名…………而且第一道题出现的错误第二个还会出现……两个栈模拟队列,牛客网上都做过……class Solution{public: void push(int node) { stack1.push(nod...原创 2018-03-13 19:39:37 · 796 阅读 · 0 评论 -
[leetcode]394. Decode String
s = "3[a]2[bc]", return "aaabcbc".s = "3[a2[c]]", return "accaccacc".s = "2[abc]3[cd]ef", return "abcabccdcdcdef".读错题……放弃治疗了以为第一个要输出bbcc来着发现自己写一个dfs真优雅啊class Solution {public: string dfs(st原创 2018-03-13 20:06:59 · 245 阅读 · 0 评论 -
leetcode2 Add Two Numbers【链表模拟大数加法】
给定的链表是倒序的 MDZZ………………我还自己写了半天反转链表,才发现不对…………然后后来又是各种报错,发现自己写麻烦了,总共先定义两个指针,一个作为头,一个往后走就可以了…………AC1.0/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *...原创 2018-03-23 16:21:50 · 267 阅读 · 0 评论 -
leetcode 91. Decode Ways
简单dp感觉自己越来越差了==可能本来就是这个水平吧orz能想到dp[i]=dp[i-1]+dp[i-2]但是写不明白,20+行的代码还要找标称其实如果想着判断两位字符是用函数封装一下,问题就是so easy单独一位的不合法的情况只有为'0'两位的是大于>27如果遍历到某一位发现dp==0 直接return 0还是要好好练习呀class Solution {public: bool...原创 2018-04-10 18:06:36 · 181 阅读 · 0 评论 -
leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee【贪心??水题】
Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.You may complete as many tr...原创 2018-06-13 10:08:10 · 352 阅读 · 0 评论 -
leetcode11 Container With Most Water【挡板存水,双指针】【Python刷题】
因为是搜单调队列搜到这个题这个就是误导我的博客http://www.cnblogs.com/Phantom01/p/5871977.html想着单调队列想了两天,又综合了单调栈优化dp:hdu1506Largest Rectangle in a Histogram &hdu1505city game dp想了一下是否是单调栈优化,发现了单调队列和单调栈对数组末尾的处理是一样的,区别只是在于...原创 2018-06-27 11:50:16 · 505 阅读 · 0 评论 -
leetcode3 Longest Substring Without Repeating Characters【Python刷题】【哈希】
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the l...原创 2018-06-29 10:02:21 · 204 阅读 · 0 评论 -
leetcode 8. String to Integer (atoi)【Python 正则表达式】
规则好多哦……Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting...原创 2018-07-30 08:20:17 · 319 阅读 · 0 评论 -
leetcode 12. Integer to Roman【Python】模拟
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-07-30 09:30:53 · 273 阅读 · 0 评论 -
leetcode 15 3-sum【双指针】
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not cont...原创 2018-08-01 10:31:32 · 321 阅读 · 0 评论 -
leetcode 16 3Sum Closest【双指针 变形】
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would ...原创 2018-09-05 20:51:25 · 167 阅读 · 0 评论 -
leetcode17Letter Combinations of a Phone Number【队列】
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is giv...原创 2018-09-12 17:36:15 · 341 阅读 · 0 评论 -
leetcode 18. 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of tar...原创 2018-09-20 18:28:53 · 147 阅读 · 0 评论 -
leetcode 19. Remove Nth Node From End of List【Python刷题】
Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, t...原创 2018-09-27 16:53:00 · 179 阅读 · 0 评论 -
leetcode 22. Generate Parentheses
22. Generate ParenthesesDescriptionHintsSubmissionsDiscussSolutionPick OneGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example,...原创 2018-09-28 17:57:17 · 137 阅读 · 0 评论 -
leetcode24. Swap Nodes in Pairs【Python刷题】链表交换相邻位置
Given a linked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only...原创 2018-10-08 20:32:12 · 542 阅读 · 0 评论 -
leetcode 29. Divide Two Integers【二进制】不用除法做整除
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.Return the quotient after dividing dividend by divisor.The integer division sho...原创 2018-10-10 17:16:30 · 487 阅读 · 0 评论 -
leetcode33. Search in Rotated Sorted Array【旋转数组找指定值 二分】
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value to search. If found ...原创 2018-10-12 18:42:02 · 163 阅读 · 0 评论 -
LeetCode 36. Valid Sudoku【九宫格判断合法】
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition. Each column must conta...原创 2018-10-15 12:32:42 · 447 阅读 · 0 评论 -
leetcode39. Combination Sum【回溯】
https://leetcode.com/problems/combination-sum/description/Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates ...原创 2018-10-16 17:18:18 · 229 阅读 · 0 评论 -
LeetCode40. Combination Sum II
https://leetcode.com/problems/combination-sum-ii/description/Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the c...原创 2018-10-17 11:18:59 · 204 阅读 · 0 评论 -
leetcode 43. Multiply Strings【大数乘法】
https://leetcode.com/problems/multiply-strings/description/Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.E...原创 2018-10-18 11:13:28 · 202 阅读 · 0 评论 -
leetcode 48. Rotate Image【旋转90】
https://leetcode.com/problems/rotate-image/description/You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-pl...原创 2018-10-22 10:20:24 · 167 阅读 · 0 评论 -
leetcode 49. Group Anagrams【素数相乘处理字符串哈希】
https://leetcode.com/problems/group-anagrams/description/Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate原创 2018-10-23 12:37:25 · 344 阅读 · 2 评论 -
leetcode 50. Pow(x, n)【快速幂】
https://leetcode.com/problems/powx-n/description/Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000...原创 2018-10-24 18:11:34 · 384 阅读 · 0 评论