leetcode题解
N3verL4nd
中年老男人~
展开
-
今日头条面试经验
今日头条面试经验今日头条面试经验一共四轮面试 前三轮是技术面试,最后一轮是hr面试。(我选的python语言)第一轮:问了简历上的两个项目,简单的介绍了一下项目,问了一些项目里的技术问题。然后直接开始出题,在一张A4纸上写代码。(一个半小时)第一题:给函数传递一个正整数的列表alist和一个正整数T,假装它等于[1,3,6,4,2,7],给出alist里所有相加等于T的元素的l...原创 2018-03-29 12:38:28 · 30991 阅读 · 3 评论 -
LeetCode-19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list be原创 2017-10-31 11:46:42 · 374 阅读 · 0 评论 -
LeetCode-22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())", "(原创 2017-10-31 11:37:37 · 268 阅读 · 0 评论 -
LeetCode-49. Group Anagrams
Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: All inputs will b原创 2017-11-08 13:06:20 · 352 阅读 · 0 评论 -
LeetCode-38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 111221 1 is read off as “one 1” or 11. 11 is read off原创 2017-11-08 10:28:36 · 346 阅读 · 0 评论 -
LeetCode-448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you原创 2017-10-22 10:27:19 · 461 阅读 · 0 评论 -
LeetCode-442. Find All Duplicates in an Array
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fin原创 2017-10-22 10:12:03 · 295 阅读 · 0 评论 -
LeetCode-Excel Sheet Column Number/Title
171. Excel Sheet Column NumberRelated to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z原创 2017-10-10 22:04:35 · 328 阅读 · 0 评论 -
LeetCode-155. 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 t原创 2017-09-28 21:17:51 · 315 阅读 · 0 评论 -
LeetCode-79. Word Search
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 “adjacent” cells are those horizontally or vertically neig原创 2017-09-28 17:30:33 · 279 阅读 · 0 评论 -
LeetCode-90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,2], a solution is:原创 2017-09-28 16:11:35 · 297 阅读 · 0 评论 -
LeetCode-151. Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.输入一个句子然后把每个单词倒序输出。利用String里的 trim、split函数可以轻松解决。/** * 返回字符串的副本,忽略前导空白和尾部空白。原创 2017-11-09 10:36:01 · 450 阅读 · 0 评论 -
LeetCode-12. Integer to Roman-13. Roman to Integer
12. Integer to RomanGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.将一个int型的数字转化为罗马数字,范围在1-3999。下面是罗马数字的介绍及基本规则: 罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C原创 2017-11-09 16:31:13 · 299 阅读 · 0 评论 -
LeetCode-Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another arr原创 2017-11-02 14:19:18 · 303 阅读 · 0 评论 -
LeetCode-147. Insertion Sort List
Sort a linked list using insertion sort.言简意赅,使用插入排序对链表排序。 与顺序表不同的是,针对链表的插入排序需要考虑到表头和表尾的插入。(每次在表尾插入都需要遍历到末尾链表吗?) 为提高效率可以分别设置指向表头和表尾的指针。也就是考察头插法和尾插法。 在中间位置插入就是简单的指针移动了。package solutions._147;...原创 2018-03-26 20:24:57 · 170 阅读 · 0 评论 -
LeetCode-260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums =...原创 2018-03-22 09:20:25 · 289 阅读 · 0 评论 -
LeetCode-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 bo...原创 2018-03-25 19:30:03 · 358 阅读 · 0 评论 -
LeetCode-8. String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input case...原创 2018-03-25 12:50:53 · 413 阅读 · 0 评论 -
求1+2+3+...+n
题目描述 求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。思路1:(1 + n) * n / 2;–>不满足 思路2:循环–>不满足 思路3:递归对于递归,我们必须得设计出来不依赖与条件判断的退出语句。public static int plus(int sum, int...原创 2018-03-30 16:57:39 · 254 阅读 · 0 评论 -
LeetCode-113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example: Given the below binary tree and sum = 22, 5 / \ ...原创 2018-03-20 19:17:04 · 238 阅读 · 0 评论 -
LeetCode- 层次遍历
637. Average of Levels in Binary TreeGiven a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1:Input: 3 / \ 9 20 / \ ...原创 2018-03-20 14:39:03 · 344 阅读 · 0 评论 -
LeetCode-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.For...原创 2018-03-20 12:13:20 · 261 阅读 · 0 评论 -
LeetCode-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 element alwa...原创 2018-03-19 19:20:55 · 239 阅读 · 0 评论 -
如何判断链表是否有环
141. Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?使用一个数组、Map记录访问过的结点,可以解决但是消耗了空间。package solutions._141;import utils.Li原创 2017-10-14 12:47:17 · 313 阅读 · 0 评论 -
LeetCode-74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is原创 2017-09-28 15:16:27 · 289 阅读 · 0 评论 -
LeetCode-78. Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:[ [3], [1], [2], [1,原创 2017-09-20 14:41:47 · 327 阅读 · 0 评论 -
LeetCode-全排列
46. PermutationsGiven a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2],原创 2017-09-19 19:43:39 · 517 阅读 · 0 评论 -
191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000000000...原创 2017-05-27 16:57:02 · 407 阅读 · 0 评论 -
461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x,原创 2017-05-27 17:28:30 · 571 阅读 · 0 评论 -
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 implement it without using e原创 2016-10-25 14:40:22 · 406 阅读 · 0 评论 -
34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found原创 2016-10-30 22:48:37 · 513 阅读 · 1 评论 -
leetcode水题题解
344. Reverse StringWrite a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".class Solution {public: string reverseString(stri原创 2016-10-25 09:52:04 · 575 阅读 · 0 评论 -
70. Climbing Stairs QuestionEditorial Solution
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?DFS#include #incl原创 2016-10-27 23:04:38 · 421 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
Given 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 should return indices of the two number原创 2016-10-26 23:41:48 · 549 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2016-10-25 18:29:33 · 423 阅读 · 0 评论 -
20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2016-11-06 00:25:12 · 373 阅读 · 0 评论 -
338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5原创 2016-10-25 09:47:19 · 501 阅读 · 0 评论 -
9. Palindrome Number QuestionEditorial Solution
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin原创 2016-11-05 23:55:34 · 400 阅读 · 0 评论 -
371. Sum of Two Integers
Calculate 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.不使用加号,减号,来实现求两个整数的和。利用到:①异或:完成相加但不进位。原创 2017-05-31 16:56:37 · 505 阅读 · 0 评论 -
345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.Example 1:Given s = "hello", return "holle".Example 2:Given s = "leetcode", return "leotcede".Note原创 2017-06-01 16:44:27 · 530 阅读 · 0 评论