![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 76
LJDaisy
这个作者很懒,什么都没留下…
展开
-
笔试题:去除字符串两头空格,合并中间空格
例子: 原 " asd asda123 as ",返回字符串为“asd asda123 as”;解题思路:我们用另外一个字符串p去存储掉无效空格后的字符串,由原字符串开始往后遍历,首先去除开头的空字符,之后令一变量cnt等于空格出现的次数,一旦cnt>0并且不属于字符串尾端,就在p后面加上空格。代码如下:#include#includeusing nam原创 2015-09-07 15:40:51 · 524 阅读 · 0 评论 -
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2015-07-25 19:06:13 · 255 阅读 · 0 评论 -
Implement Stack using Queues 用队列实现栈
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Retur原创 2015-07-05 15:44:35 · 701 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2015-07-11 14:54:55 · 515 阅读 · 0 评论 -
Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, i原创 2015-06-14 15:18:09 · 447 阅读 · 0 评论 -
Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such thatoutput[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(原创 2015-07-25 16:39:44 · 525 阅读 · 0 评论 -
Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3原创 2015-07-26 21:49:43 · 425 阅读 · 0 评论 -
leetcode-Minimum Depth of Binary Tree &&Maximum Depth of Binary Tree
1.Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.解题思路:求一棵二叉树的最短路径(从根节点到叶子节点)。原创 2015-06-03 23:26:51 · 443 阅读 · 0 评论 -
Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers wi原创 2015-07-25 19:19:51 · 352 阅读 · 0 评论 -
Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2015-07-25 18:48:56 · 667 阅读 · 0 评论 -
Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.题目解析:将一个矩阵中0所在行,以及所在列都置为0.方法一:将矩阵中,0的行和列的下标保存下来,并且分别去除里面重复的下标,之后遍历矩阵,将记录的这些行和列均置为0.代码如下:cla原创 2015-06-12 23:31:55 · 394 阅读 · 0 评论 -
Subsets
Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For原创 2015-07-10 21:25:10 · 475 阅读 · 0 评论 -
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with val原创 2015-07-25 12:01:19 · 605 阅读 · 0 评论 -
Implement Queue using Stacks(用栈实现队列)
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2015-07-11 15:38:01 · 603 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4原创 2015-07-10 22:09:34 · 494 阅读 · 0 评论 -
八皇后问题
八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。计算机发明后,有多种计算机语言可以解决此问题。原创 2015-07-05 11:53:09 · 724 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2015-07-05 18:51:24 · 336 阅读 · 0 评论 -
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原创 2015-06-12 21:52:48 · 376 阅读 · 0 评论 -
Search in Rotated Sorted Array && Search in Rotated Sorted ArrayII
Search in Rotated Sorted ArraySuppose a sorted array 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 s原创 2015-06-12 22:34:58 · 332 阅读 · 0 评论 -
Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallestto find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.解题思路:求BST(二叉排序树)中第k小的数原创 2015-07-05 14:55:16 · 539 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2015-07-05 22:30:47 · 356 阅读 · 0 评论 -
Triangle
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 example, given the following triangle[ [2], [3,4], [原创 2015-08-01 13:14:24 · 482 阅读 · 0 评论 -
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原创 2015-08-02 18:20:09 · 410 阅读 · 0 评论 -
Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.原创 2015-08-02 22:20:27 · 478 阅读 · 0 评论 -
Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may a原创 2015-08-03 20:34:15 · 1713 阅读 · 0 评论 -
Jump Game &Jump Game II
Jump GameGiven 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.原创 2015-08-16 14:55:42 · 479 阅读 · 0 评论 -
实现字符串转为int
题目:自己实现一个函数strToint,不能使用atoi或者其他类似的库函数。解题思路:这个题不难,主要是靠考虑到各种情况,1、判断字符串是否为空2、保存正负号3、字符串中是否有无效字符(非数字)-->有该字符串无效4、是否会溢出!!!我们令出现上述情况时转换的int型数字为0,那么如何区分正常0和异常情况下的0,我们用一个标志位flag即可。代码如下:#i原创 2015-09-08 11:06:25 · 612 阅读 · 0 评论 -
01背包问题分析
“背包题目”的基本描述是:有一个背包,能盛放的物品总重量为S,设有N件物品,其重量分别为w1,w2,…,wn,希望从N件物品中选择若干物品,所选物品的重量之和恰能放进该背包,即所选物品的重量之和即是S。递归和非递归解法都能求得“背包题目”的一组解,试写出“背包题目”的非递归解法。解法一:最初思路就是动态规划,动态规划求一组解,我们用f[i][j]用来表示前i件物品随意选择一些物品能原创 2015-09-03 19:59:17 · 535 阅读 · 0 评论 -
找一个字符串中重复的最长子序列
例如:abcdabcaabcd最长子序列为:abcd。下面我们介绍两种方法:第一种方法:倍增算法。也就是在序列中先以步长为1查看两个相邻的元素是否相等,都不相等,将步长加1,查看两个相邻为2的元素是否相等,不相等,步长再增加1,依次进行,直到步长为k时,两个元素相等,之后以这两个元素为起始端我们同时向后移动,遇到相同元素则在相同子序列上+1.之后和最长子序列长度比较大小,判断是原创 2015-09-05 20:24:40 · 1236 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2015-07-31 22:45:26 · 424 阅读 · 0 评论 -
Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-07-29 20:53:32 · 448 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题目解析:已知一个单链表,其中元素都是升序排列的,现在将链表转成平衡二叉树。方法一:我们用一个数组存储链表中元素,这样就可以利用下标访问元素,之后根据二分查找法找树的原创 2015-07-27 21:42:40 · 476 阅读 · 0 评论 -
Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2015-07-07 20:30:59 · 623 阅读 · 0 评论 -
Unique path ii
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2015-07-26 23:05:02 · 839 阅读 · 0 评论 -
Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain du原创 2015-07-28 22:40:06 · 421 阅读 · 0 评论 -
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.题目解析:判断一个整数是否是回文数,“回文”是指正读反读都能读通的句子,它是古今中外都有的一种修辞方式和文字游戏,如“我为人人,人人为我”等。在数学中也有这样一类数字有这样的特征,成为回文数(palindrome number)。原创 2015-08-09 19:38:42 · 644 阅读 · 0 评论 -
丑数
丑数:只包含因子2、3、5的数称作丑数。一、leetcode原题: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. For example原创 2015-08-24 11:26:50 · 515 阅读 · 0 评论 -
树根 Digital root
数根(又称数字根Digital root)是自然数的一种性质,换句话说,每个自然数都有一个数根。数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于十为止,或是,将一数字重复做数字和,直到其值小于十为止,则所得的值为该数的数根。例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于十,原创 2015-08-24 10:47:23 · 1200 阅读 · 0 评论 -
为什么要引进多线程?
进程也可以完成任务,为什么要在一个进程中再有一类进程(也就是迷你进程,称为线程)? 有很多原因,其中主要原因是进一步提高系统的并发性。在许多应用中同时发生着多种活动,某些活动随着时间的推移会被阻塞。通过将这些应用程序分解成可并行运行的多个顺序程序,程序设计会变得更简单。 下面列出几个促使多线程产生的主要原因: 1)线程是并行实体,共享同一个地址空间和所有数据原创 2015-07-29 21:22:39 · 863 阅读 · 0 评论 -
求M的N次方最后三位
正整数M 的N次方有可能是一个非常大的数字,我们只求该数字的最后三位例1:比如输入5和3 ,5的3次方为125,则输出为125 例2:比如输入2和10 2的10次方为1024 ,则输出结果为24例3:比如输入111和5 111的5次方为116850581551,则输出结果为551方法一:因为m是比较大的数,n次方之后有可能会超出范原创 2015-07-02 22:43:25 · 2423 阅读 · 0 评论