
Leetcode
T2777
这个作者很懒,什么都没留下…
展开
-
[leetcode] 88. Merge Sorted Array 合并有序数组
题目描述:Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may as...原创 2019-09-13 19:30:23 · 151 阅读 · 0 评论 -
[Leetcode] 392. Is Subsequence
看起来应该是一个简单的求子串的问题。Given a stringsand a stringt, check ifsis subsequence oft.You may assume that there is only lower case English letters in bothsandt.tis potentially a very long (lengt...原创 2019-08-12 00:11:46 · 168 阅读 · 0 评论 -
[leetcode] 122. Best Time to Buy and Sell Stock II
这是一个非常简单贪心问题,贪心问题的解决方法一般都非常简单,但是要想到这个方法真的挺不容易的:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may compl...原创 2019-08-11 22:39:23 · 153 阅读 · 0 评论 -
【leetcode】169. Majority Element 求众数
要求求出给定数组中出现频率大于 n / 2的数,那么这个数显然只可能有1个。题目为:Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.You may assume that the a...原创 2019-08-14 23:46:07 · 136 阅读 · 0 评论 -
【leetcode】100. Same Tree
判断两棵树是否相同,可以根据先序遍历和中序遍历或中序遍历及后序遍历是否一致来判断,也可以根据简单的递归方式来解决。Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structu...原创 2019-08-14 21:09:19 · 138 阅读 · 0 评论 -
215. Kth Largest Element in an Array 求数组中第k大的数
问题看起来比较简单,只要进行简单的排序即可,因为标注了堆,就使用了堆进行一下求解:Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:...原创 2019-08-19 13:15:05 · 132 阅读 · 0 评论 -
【leetcode】53. Maximum Subarray 连续子序列最大值问题:动态规划
前面已经遇到了这一题目,就是简单的求给定数组的连续子序列的最大和。各元素必须直接相连。Given an integer arraynums, find the contiguous subarray(containing at least one number) which has the largest sum and return its sum.Example:Input...原创 2019-08-19 12:59:26 · 229 阅读 · 0 评论 -
【leetcode】242. Valid Anagram
暴力解决问题是很简单的,这里主要学习了map的使用。Given two stringssandt, write a function to determine iftis an anagram ofs.Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s =...原创 2019-08-18 21:33:17 · 131 阅读 · 0 评论 -
【leetcode】496. Next Greater Element I
问题比较简单,给出两个数组,求第二个数组中与第一个数组中某元素a相同的右边的第一个某一元素b,使得b大于a,数组1是数组2的一个子集。You are given two arrays(without duplicates)nums1andnums2wherenums1’s elements are subset ofnums2. Find all the next greate...原创 2019-08-18 21:07:59 · 143 阅读 · 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. 1112211is read off ...原创 2019-08-18 20:08:43 · 120 阅读 · 0 评论 -
【leetcode】【c++】617. 合并二叉树
617.Merge Two Binary TreesGiven 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 mer...原创 2019-07-07 21:06:40 · 163 阅读 · 0 评论 -
【leetcode】120. Triangle
问题是求一个直角三角形从上到下的和最小的路径,要求每一行的路径要么是上一行对应列而来,要么是上一行对应列-1即斜对角线而来。问题描述:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For ...原创 2019-08-15 23:24:27 · 130 阅读 · 0 评论 -
[leetcode] 58. Length of Last Word (字符串的末尾单词的长度)
简单题,求给出的字符串的最后一个单词的长度,要注意的是字符串的末尾可能是空格,要跳过空格。Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word ...原创 2019-08-25 16:22:54 · 145 阅读 · 0 评论 -
[pat乙级] 1054 求平均值
问题很简单,但是要判断的东西很多:本题的基本要求非常简单:给定N个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是 [−1000,1000] 区间内的实数,并且最多精确到小数点后 2 位。当你计算平均值的时候,不能把那些非法的数据算在内。输入格式:输入第一行给出正整数N(≤100)。随后一行给出N个实数,数字间以一个空格分隔。输出格式:...原创 2019-09-05 10:40:40 · 164 阅读 · 0 评论 -
[leetcode] 312. Burst Balloons
与矩阵连乘问题很相似:Givennballoons, indexed from0ton-1. Each balloon is painted with a number on it represented by arraynums. You are asked to burst all the balloons. If the you burst ballooniyou wi...原创 2019-09-05 09:44:40 · 169 阅读 · 0 评论 -
[leetcode] 206. Reverse Linked List
问题描述:Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or...原创 2019-09-04 09:31:34 · 218 阅读 · 0 评论 -
[leetcode] 70. Climbing Stairs 爬楼梯问题
斐波那契额数列问题。You are climbing a stair case. It takesnsteps 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?Note:Givennwill ...原创 2019-08-31 21:21:49 · 177 阅读 · 0 评论 -
[leetcode] 287. Find the Duplicate Number (查找数组中重复的数)
找出重复的数,已知数组中重复的数只有1个,要求不能使用额外的空间。Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that th...原创 2019-08-30 20:02:25 · 190 阅读 · 0 评论 -
[leetcode]344. Reverse String
题目比较简单,翻转char[]字符数组:问题描述:Write a function that reverses a string. The input string is given as an array of characterschar[].Do not allocate extra space for another array, you must do this bym...原创 2019-08-26 17:54:09 · 127 阅读 · 0 评论 -
【leetcode】73. Set Matrix Zeroes矩阵置0问题
问题是将矩阵中所有0所在的行与列全部置0.Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do itin-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], ...原创 2019-08-17 15:30:35 · 139 阅读 · 0 评论 -
【leetcode】102. Binary Tree Level Order Traversal 层序遍历递归形式(全局变量)
二叉树层序遍历递归形式的解决,一般问题都是直接打印遍历的结果,这里要将每一层的结果按照层进行打印,用队列的方式可能存在一些问题。这里采用了递归的方式进行解决。Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level)....原创 2019-08-17 14:35:24 · 175 阅读 · 0 评论 -
[leetcode]1038. Binary Search Tree to Greater Sum Tree
问题描述:Given the root of a binarysearchtree with distinct values, modify it so that everynodehas a new value equal to the sum of the values of the original tree that are greater than or equal to...原创 2019-08-21 19:42:02 · 165 阅读 · 0 评论 -
【LeetCode】2. Add Two Numbers 链表相加(new)
这题还是比较有难度的。题目的意思是两个链表相加得到一个新的链表,链表的长度可能不相等,将每个相应结点的值存储在一个链表上,当该值大于10时,将该值进位到结果链表的下一个结点上,要注意的几点是最后的值可能会超出 l1 和 l2的长度,也要注意如何处理 l1 和 l2的长度不相等的情况。You are given twonon-emptylinked lists representing t...原创 2019-08-21 15:47:27 · 158 阅读 · 0 评论 -
【leetcode】【c++】378. Kth Smallest Element in a Sorted Matrix 以及 迭代器iterator的理解
首先是题目的介绍:Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted ...原创 2019-02-24 17:09:46 · 208 阅读 · 0 评论 -
【leetcode】326. power of three 以及 log 问题
首先介绍 c++ 中 math.h 中的 log 函数的用法, log 函数只支持 double log10(double x) 以及double log(double x) 两种形式,其中 log10(x)以 10 为底,log(x)则以 e 为底,想要求其它的对数,要用 log(y) / log(x) 的形式,这在数学上很好理解,不再详细解释。题目描述:Given an inte...原创 2019-02-13 15:05:13 · 204 阅读 · 0 评论 -
【leetcode】【c++】69. Sqrt(x)
万能的c++ 头文件,以后在进行oj 时可以用,但是 vs 不支持#include<bits/stdc++.h>它包含了<iostream><cmath><algorithm>等各种头文件的集合。INT_MAX = 2147483647 ; INT_MIN = -2147483648 ; 都是有符号数。题目描述:Implem...原创 2019-02-23 09:07:50 · 377 阅读 · 0 评论 -
[leetcode][c++] 28. strstr()
这是一道比较简单的问题,就是找出母字符串中是否有与子字符串相匹配的字符串。要注意的就是几点,题目中要求当needle串为空时,要返回0,没有匹配的则返回 -1.下面是题目的具体描述,haystack代表长的,needle则代表短的。class Solution {public: int strStr(string haystack, string needle) {...原创 2019-01-31 22:47:00 · 183 阅读 · 0 评论 -
[leetcode] 27. remove element
c++11 支持auto自动变量: 已知nums是vector类型用auto it = nums.begin()代替vector<int>::iterator it = nums.begin();这样可以减少代码量。题目为:简单说就是在一个vector中将与给定的数val不同的数的个数n返回,同时此时的vector的前面的n个数是不同的数(顺序不必和原来的相同...转载 2019-01-31 20:22:56 · 127 阅读 · 0 评论 -
[leetcode] 26 Remove duplicates from sorted array 去除有序素组中重复的元素
题目描述: 主要就是计算出所给的数组中不同的元素个数,比较简单,利用一般方法即可。class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size() < 2) return nums.size(); ...转载 2018-10-09 00:06:25 · 194 阅读 · 0 评论 -
【leetcode】21. Merge two sorted lists 合并两个排序列表
这个题目用到了链表,实质就是将两个有序链表进行合并,合并后的链表应该也是有序的。具体代码为:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {...原创 2018-10-06 21:22:50 · 243 阅读 · 0 评论 -
[leetcode][c++] 20. Valid Parentheses 有效的括号
题目描述:这是大二上数据结构课时的一个比较简单的问题,就是括号的匹配,要注意的几点就是:()[]正确,{() } 也是正确的,主要计时按照顺序的匹配,用到的就是栈的知识,具体的代码为:class Solution {public: bool isValid(string s) { vector<char> stack; for(i...转载 2018-10-05 20:53:46 · 227 阅读 · 0 评论 -
[leetcode] 1. c++ :two sum 以及 map问题
题目描述,给定一个整数数组,返回2个数组元素的下标使2个数组元素之和等于某个给定的整数。如:数组 : [2, 7, 11, 15], 目标 : 9, 由于 nums[0] + nums[1] = 2 + 7 = 9, 返回 [0, 1].1.利用暴力搜索的方法:class Solution {public: vector<int> twoSum(vector&l...原创 2018-09-09 16:56:55 · 401 阅读 · 0 评论 -
[Leetcode][C++] 7. reverse integer
太菜了,好多基础的东西都不会 :(一,1个字节共8位,int 类型共有4个字节,共32位,根据二进制编码的规则,INT_MAX = 2^31-1,INT_MIN= -2^31(-INT_MAX-1).C/C++中,所有超过该限值的数,都会出现溢出,出现warning,但是并不会出现error。如果想表示的整数超过了该限值,可以使用长整型long long 占8字节64位。关于INT_M...转载 2018-10-03 17:43:28 · 366 阅读 · 0 评论 -
[leetcode] 9. Palindrome number 回文数
题目描述:首先负数一定不是回文数,0是回文数,10的倍数一定不是回文数10000方法1 ,跟前面的求数的逆序一样,求出逆序数,与原数比较即可,需要借助temp:class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; ...转载 2018-10-03 20:02:55 · 132 阅读 · 0 评论 -
【leetcode】12. integer to roman 整数向罗马字符的转换
题目描述:罗马字符真是这样转换的吗,太可怕了。由基本的1000,500,100,50,10,5,1组成,然后 IV(1,5)就表示4,XL(10,50)就表示40,把所有的这种组成排列出来啊,能想到的太厉害了吧,下面是转载得到的一个比较厉害的贪心算法,beat 50%。范围只到3999,例如3541,先减去3次1000,再减去1次500,再减去1次40,再减去一次1.得到的字符串...转载 2018-10-03 20:40:10 · 171 阅读 · 0 评论 -
[Leetcode] 13. Roman to integer 罗马数转换为整数
与上一题相对应,方法不同,这里运用到了map的方法,从后往前开始加,前面的罗马字符比后面的字符小,那就减去这个小字符,大的话就加上这个字符,注意T[s[i]]的写法,s.back()是最后的字符,s.length-2是因为倒数倒数第二个和倒数第一个进行对比。具体代码如下,击败40%,非常巧妙的方法,学习到了:int romanToInt(string s) { unordere...转载 2018-10-03 21:13:26 · 166 阅读 · 0 评论 -
[leetcode][c++] 35. search insert position 以及vector的简单归纳
首先对容器vector(向量), vector 可以理解为一个动态数组,这点可以很方便的在以后使用,不用像数组那样预先分配指定大小空间,下面例子中的vector<int> v(2,2) 进行了一个初始化,即它的元素由2 个 2组成,实际上可以继续push_back(), 再打印就可以看出v的实际内容得到了增加,元素也得到了增长,满足了动态数组的条件。 做一个简单的归纳如下图的程序:...原创 2019-02-01 19:50:08 · 195 阅读 · 0 评论 -
【Leetcode】【c++】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 r...原创 2019-02-04 11:35:44 · 253 阅读 · 0 评论 -
【leetcode】【c++】292. Nim Game
Nim Game 是一种回合制游戏,要求游戏双方轮流从一堆棋子中取棋子,当不能再取时所轮到的一方为输家,即取到最后一颗棋子的玩家获胜。题目描述:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take ...原创 2019-02-22 21:19:50 · 244 阅读 · 0 评论 -
【leetcode】【c++】268. Missing Number
题目:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input: [9,6,4,2,3,5,7,...原创 2019-02-22 20:35:25 · 146 阅读 · 0 评论