LeetCode
落霞与孤鹜亓飞
与其感慨路难行,不如马上出发
展开
-
力扣——两数之和
两数之和来自力扣上面的一道简单题,我比较轻松的做出来了,按照道理,这道题目不应该放到博客上来,不过今天我做完这道题之后,我突然发现我在2年前,10个月前都做过这道题目,我比较好奇从2年前,再到10个月前,再到今天,我的做题水平是否真的有进步?我分别看了我2年前,10个月前以及今天的题解。2年前我做这道题目,我的题解如下:class Solution {public: string addBinary(string a, string b) { string rslt ="";原创 2020-06-23 01:10:40 · 931 阅读 · 0 评论 -
Valid Parentheses
1.问题描述Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}"原创 2016-07-02 20:09:51 · 272 阅读 · 0 评论 -
Merge Two Sorted Lists
1.问题描述Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.2.解题思路合并两个有序的链表,只需要逐一进行比较即可。1).刚开始通过比较l1和原创 2016-07-03 15:07:30 · 301 阅读 · 0 评论 -
Roman to Integer
罗马数字转数字,非常简单的语句,直接上代码:class Solution {public: int romanToInt(string s) { auto it = s.begin(); int rslt = 0; while(it!=s.end()) { switch (*it) { case 'I':原创 2016-06-19 20:42:32 · 267 阅读 · 0 评论 -
Int to Roman
阿拉伯数字转罗马数字(1~3999)思路:直接从个位数开始转起,分别用10,100,1000,10000取余数,不同的余数采用不同的解决办法,最后返回字符串代码class Solution {public: string intToRoman(int num){ int d = 10; int n = num; string rslt(""); while原创 2016-06-19 23:10:31 · 230 阅读 · 0 评论 -
25. Reverse Nodes in k-Group
1.问题描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should rem原创 2016-07-18 19:12:27 · 368 阅读 · 0 评论 -
27. Remove Element
1.问题描述Given an array and a value, 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 in place with constant mem原创 2016-07-18 20:00:34 · 248 阅读 · 0 评论 -
29. Divide Two Integers
1.问题描述Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.2.解题思路采用二分法,每次让除数以2倍的形式递增,只要递增结果小于被除数,则继续递增,如果递增时超过了,则记住递增前的数,然后用被除数减去原创 2016-07-20 19:33:51 · 372 阅读 · 0 评论 -
Knight Probability in Chessboard
Knight Probability in Chessboard题目描述:在NxN的棋盘上,骑士走“日”字,经过K次行走之后,骑士还留在棋盘上的概率是多少?骑士在棋盘上行走时,每次都有8个方向可选,走K次之后的路径树目8K8K8^K,而假设留在棋盘上的路径数目为x,那么总共的概率为x/8kx/8kx / 8^k。现在问题就是如何求解x。设矩阵F[K,R,C]表示骑士在以坐标(R,C...原创 2018-07-18 15:27:25 · 226 阅读 · 0 评论 -
450.Delete Node in a BST
Delete Node in a BST在二分查找树中删除一个给定值所在的节点,随后返回删除后二叉树的根节点。解题思路:利用二分查找树查找该节点key,key和当前根节点的value进行比较: (1) 如果key<<>>>value,说明要删除的节点在根节点的右子树上。 (3) 如果key == value,表明要删除的节点就是当前根节点,按照如下情况讨...原创 2018-07-19 15:34:47 · 182 阅读 · 0 评论 -
Search in Rotated Sorted Array
Search in Rotated Sorted Array对一个有序排列的数组,升序排列,每个数各不相同。将其中前面一部分数据挪到后面变成一个新的数组,给定一个数,求这个数在新数组中的下标。 该数存在则返回下标,否则返回-1。思路:首先使用二分查找找到数组中最小的那个数的位置small_index,然后以最小的这个数为轴,可以将该数组划分为两部分,small_index之前的数为升...原创 2018-07-14 16:22:14 · 232 阅读 · 0 评论 -
Search in Rotated Sorted Array II
题目描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If...原创 2018-09-15 12:00:47 · 147 阅读 · 0 评论 -
23. Merge k Sorted Lists
1.问题描述Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.2.解题思路类似于2路归并法,将K路归并划为2路归并,直到最后得到一个链表,采用递归的方法3.代码实现class Solution {public:ListNo原创 2016-07-13 11:51:09 · 364 阅读 · 0 评论 -
22.Generate Parentheses
1.问题描述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:[ "((()))", "(()())", "(())()原创 2016-07-12 14:23:41 · 293 阅读 · 0 评论 -
Add Two Numbers
Add two numbes You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit.原创 2016-01-13 15:01:51 · 438 阅读 · 0 评论 -
Longest Palindromic Substring
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic subst原创 2016-02-14 14:21:27 · 353 阅读 · 0 评论 -
ZigZag Conversion
ZigZag Conversion The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P原创 2016-02-14 15:16:34 · 405 阅读 · 0 评论 -
Reverse Integer
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321算法思路 先求出整数有多少位,然后将其反转,但是需要注意反转后的数是否越界,如果越界,直接返回0代码实现class Solution {public:原创 2016-02-14 15:53:52 · 392 阅读 · 0 评论 -
String to Integer (atoi)
String to Integer (atoi) String to Integer (atoi) The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this原创 2016-02-14 16:14:07 · 305 阅读 · 0 评论 -
Median of Two Sorted Arrays
Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).原创 2016-02-03 19:06:35 · 549 阅读 · 0 评论 -
Palindrome Number
判断一个数字是否为回文数最简单的方法就是先把这个整数反转,然后将反转后的整数和原来的整数相减,然后如果结果为0,则为回文数,否则不是代码:class Solution {public://num是0~9的数字,time>=1,num为最高位的数,time为位数int getRslt(int num, unsigned time){ while (1<time--) {原创 2016-03-18 21:35:59 · 356 阅读 · 0 评论 -
Remove Nth Node From End of List
删除链表的倒数第n个元素算法:用三个指针p,q,r,p指针作为前移指针,q指针指向倒数第n个元素的指针,r为q指针的前面一个元素的指针。起始时刻p指向head,让p和q之间的距离为n,这样当p指向链表末尾的时候,q自然指向的是倒数第n个元素。代码:/** * Definition for singly-linked list. * struct ListNode { * int val原创 2016-03-20 21:52:47 · 276 阅读 · 0 评论 -
11.Container With Most Water
Container With Most WaterGiven n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai)原创 2016-03-24 21:44:15 · 690 阅读 · 0 评论 -
THREE SUM
3Sum问题Given an array S of n integers, are there elements a, b, c in S 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 contain原创 2016-06-27 22:21:12 · 359 阅读 · 0 评论 -
3Sum Closest
1.问题描述Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would ha原创 2016-06-27 23:28:34 · 353 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters一、题目说明Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters f原创 2016-01-29 19:53:11 · 452 阅读 · 0 评论