![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
我是叫帽帽啊
这个作者很懒,什么都没留下…
展开
-
LeetCode 977. Squares of a Sorted Array 有序数组的平方 JAVA实现
题目内容:给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。 示例 1:输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100]示例 2:输入:[-7,-3,2,3,11] 输出:[4,9,9,49,121] 提示:1 <= A.length <= 10000 -10000 <= A...原创 2019-02-16 17:35:49 · 623 阅读 · 0 评论 -
LeetCode 89. Gray Code(格雷编码) JAVA实现
题目描述:格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个二进制的差异。给定一个非负整数 n ,表示该代码中所有二进制的总数,请找出其格雷编码顺序。一个格雷编码顺序必须以 0 开始,并覆盖所有的 2n 个整数。注意事项:对于给定的 n,其格雷编码顺序并不唯一。根据以上定义, [0,2,3,1] 也是一个有效的格雷编码顺序。样例:给定 n = 2, 返回 [0,1,3,2]。...原创 2018-12-18 15:02:40 · 838 阅读 · 0 评论 -
LeetCode 617. Merge Two Binary Trees(合并二叉树)JAVA实现
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree...原创 2018-11-30 15:23:34 · 350 阅读 · 0 评论 -
LeetCode 52. N-Queens II(N皇后2)C++实现
52. N-Queens IIThe n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return the number of distinct solutions t...原创 2018-11-30 15:21:28 · 279 阅读 · 0 评论 -
590. N-ary Tree Postorder Traversal(N叉树的后序遍历)JAVA实现
Given an n-ary tree, return the postorder traversal of its nodes' values.For example, given a 3-ary tree: Return its postorder traversal as: [5,6,3,2,4,1].解题思路一:递归实现。/*// Definition f...原创 2018-11-30 15:18:25 · 268 阅读 · 0 评论 -
LeetCode589. N-ary Tree Preorder Traversal(N叉树的前序遍历)JAVA实现
Given an n-ary tree, return the preorder traversal of its nodes' values.For example, given a 3-ary tree:Return its preorder traversal as: [1,3,5,6,2,4].Note:Recursive solution is trivial, co...原创 2018-11-30 15:16:10 · 263 阅读 · 0 评论 -
LeetCode458. Poor Pigs(可怜的小猪)JAVA实现
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum...原创 2018-11-25 10:57:39 · 312 阅读 · 0 评论 -
leetcode383. Ransom Note(赎金信)JAVA实现
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot...原创 2018-11-25 10:43:08 · 347 阅读 · 0 评论 -
leetcode263. Ugly Number(丑数)JAVA实现
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.Example 1:Input: 6Output: trueExplanation: 6 = 2 ×...原创 2018-11-25 10:38:32 · 385 阅读 · 0 评论 -
剑指offer 数组中重复的数字
题目描述在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。测试用例:长度为n的数组里包含一个或多个重复的数字 数组中不包含重复的数字 无效输入测试(数组为空; 出现0-...原创 2019-02-18 15:27:28 · 1000 阅读 · 0 评论 -
剑指Offer面试题4(Java版):二维数组中的查找
题目描述在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 解题思路一:暴力求解,代码如下:public class Solution { public boolean Find(int target, int [][] arr...原创 2019-02-18 21:20:03 · 559 阅读 · 0 评论 -
剑指offer面试题 数组中只出现一次的数字 JAVA实现
题目描述一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。解题思路一:使用hashMap,能够保证在O(n)的时间复杂度内找到。空间复杂度O(n)。//num1,num2分别为长度为1的数组。传出参数//将num1[0],num2[0]设置为返回结果import java.util.HashMap;public class S...原创 2019-05-10 10:18:49 · 406 阅读 · 0 评论 -
剑指offer面试题 和为s的连续正数序列 JAVA实现
题目描述小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!输出描述:输出所有和为S的连续正数序...原创 2019-05-10 10:16:53 · 393 阅读 · 0 评论 -
剑指offer面试题 扑克牌顺子 JAVA实现
题目描述LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K...原创 2019-05-10 10:12:29 · 496 阅读 · 0 评论 -
剑指offer面试题 孩子们的游戏(圆圈中最后剩下的数)
题目描述每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去.....原创 2019-05-10 10:11:18 · 570 阅读 · 0 评论 -
LeetCode 279. Perfect Squares JAVA解法
279.Perfect SquaresMediumGiven a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton.Example 1:Input: n = 12 Output: 3 Explanati...原创 2019-05-10 10:03:18 · 904 阅读 · 0 评论 -
剑指offer39 数组中出现次数超过一半的数字
题目描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。解题思路一:时间复杂度O(n),空间复杂度O(1),不需要改变数组采用阵地攻守的思想:第一个数字作为第一个士兵,守阵地;count=1;遇到相同元素,coun...原创 2019-04-07 16:28:19 · 606 阅读 · 0 评论 -
LeetCode746. Min Cost Climbing Stairs 使用最小花费爬楼梯
题目描述:数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。示例 1:输入: cost = [10, 15, 20] 输出: 15 解释: ...原创 2019-02-14 13:16:12 · 4983 阅读 · 0 评论 -
LeetCode18. 四数之和 4Sum JAVA实现
给定一个包含n个整数的数组nums和一个目标值target,判断nums中是否存在四个元素a,b,c和d,使得a+b+c+d的值与target相等?找出所有满足条件且不重复的四元组。注意:答案中不可以包含重复的四元组。示例:给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集合为...原创 2019-02-28 17:03:18 · 2681 阅读 · 0 评论 -
LeetCode16 3Sum Closest(最接近的三数之和) JAVA实现
给定一个包括n个整数的数组nums和 一个目标值target。找出nums中的三个整数,使得它们的和与target最接近。返回这三个数的和。假定每组输入只存在唯一答案。例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).解题思路:参考: leetcode...原创 2019-02-28 16:57:54 · 2486 阅读 · 0 评论 -
leetcode 15 三数之和 3Sum JAVA实现
给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c ,使得a + b + c =0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]解法一:穷举,时间复杂度...原创 2019-02-28 16:53:26 · 2673 阅读 · 0 评论 -
LeetCode70. Climbing Stairs 爬楼梯 JAVA实现
题目描述:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?注意:给定 n 是一个正整数。示例 1:输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1. 1 阶 + 1 阶 2. 2 阶示例 2:输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1. 1 阶 + 1 阶 + 1 阶 2...原创 2019-02-13 19:28:04 · 2401 阅读 · 0 评论 -
LeetCode 509 Fibonacci Number 斐波那契数 JAVA实现
题目描述: 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1.给定 N,计算 F(N)。示例 1:输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1...原创 2019-02-13 12:00:59 · 2264 阅读 · 0 评论 -
leetcode665. Non-decreasing Array(非递减数列)JAVA实现
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] <= array[i + 1] holds for e...原创 2018-11-25 10:35:18 · 738 阅读 · 0 评论 -
LeetCode77. Combinations(组合)JAVA实现
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]给定两个整数 n 和 ...原创 2018-11-23 16:13:05 · 1120 阅读 · 0 评论 -
LeetCode27. Remove Element(移除元素)JAVA实现
Given an array nums and a value val, 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 by modifying the input arra...原创 2018-11-09 22:15:26 · 853 阅读 · 0 评论 -
LeetCode 258. Add Digits (各位相加)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.Example:Input: 38Output: 2 Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. ...原创 2018-10-21 20:13:03 · 815 阅读 · 0 评论 -
LeetCode561. Array Partition I ( 数组拆分 I )
Given 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 to n as large as poss...原创 2018-10-21 20:08:58 · 508 阅读 · 0 评论 -
leetcode807 Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can...原创 2018-07-31 15:46:54 · 1135 阅读 · 0 评论 -
leetcode861. Score After Flipping Matrix
We have a two dimensional matrix A where each value is 0 or 1.A move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s....原创 2018-07-31 17:14:24 · 744 阅读 · 0 评论 -
LeetCode 476. Number Complement (数字的补数)C++ JAVA实现
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range of a 3...原创 2018-10-22 19:16:17 · 788 阅读 · 0 评论 -
LeetCode 657.Robot Return to Origin( 机器人能否返回原点) C++ JAVA实现
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.The move sequence is repres...原创 2018-10-22 10:27:00 · 569 阅读 · 0 评论 -
LeetCode 905. Sort Array By Parity (C++ C JAVA Python3 实现)
905. Sort Array By ParityGiven an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.You may return any answer array ...原创 2018-10-14 14:41:37 · 785 阅读 · 0 评论 -
POJ1222 EXTENDED LIGHTS OUT
DescriptionIn an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is presse...原创 2018-09-12 23:32:52 · 1407 阅读 · 0 评论 -
HDOJ1482 & POJ1013
Problem DescriptionSally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistin...原创 2018-09-11 17:47:34 · 439 阅读 · 0 评论 -
leetcode728. Self Dividing Numbers
self-dividing number is a number that is divisible by every digit it contains.For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.Also, a self-dividing...原创 2018-08-01 16:15:48 · 1321 阅读 · 0 评论 -
leetcode627. Swap Salary
Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate...原创 2018-08-01 15:44:59 · 426 阅读 · 0 评论 -
LeetCode797. All Paths From Source to Target
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order.The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. ...原创 2018-09-18 12:44:30 · 1461 阅读 · 0 评论 -
LeetCode852. Peak Index in a Mountain Array
Let's call an array A a mountain if the following properties hold:A.length >= 3 There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ....原创 2018-07-31 21:48:54 · 516 阅读 · 0 评论 -
leetcode 709 To Lower Case
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.Example 1:Input: "Hello"Output: "hello"Example 2:Input: "here"Output: "here"E...原创 2018-07-27 20:54:45 · 667 阅读 · 0 评论