数据结构与算法
文章平均质量分 56
qazwyc
这个作者很懒,什么都没留下…
展开
-
421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.Could you do this in O(n) runtime?题解使用trie树记录所有数的01二进制表示 之后遍历每个数,找原创 2017-07-22 21:45:06 · 308 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed int原创 2017-05-15 15:11:28 · 201 阅读 · 0 评论 -
Activity使用技巧
知晓当前是在哪一个活动当我们需要看别人写的源码,但是不知道启动的是哪一个活动,就可以通过下面的方式。新建BaseActivity类继承AppCompatActivity,使所有活动继承BaseActivity类import android.support.v7.app.AppCompatActivity;import android.util.Log;public class BaseActivi原创 2017-05-14 16:17:38 · 309 阅读 · 0 评论 -
序列化二叉树
请实现两个函数,分别用来序列化和反序列化二叉树题解序列化:前序遍历,节点为空用#表示,如{8,3,9,1,4}的表示结果为{8,3,1,#,#,4,#,#,9,#,#,} 反序列化:逗号分隔了每个节点,如果是#则为空,否则算出数值实例节点并递归其左右子树。/*struct TreeNode { int val; struct TreeNode *left; struct原创 2017-04-23 18:31:05 · 237 阅读 · 0 评论 -
二叉树中序遍历的下一个节点
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针next。题解如果没有给出父节点可能真要中序遍历一次了,但既然给出了就不必那么麻烦了。 分两种情况: 1. 该节点存在右子节点,则下一个节点是右子树的最左节点。 2. 该节点不存在右子节点,则下一个节点是该节点的第一个父子关系为左的祖先节点中的父节点, 因为如果遍原创 2017-04-22 23:41:36 · 1754 阅读 · 1 评论 -
KMP算法
参考博客: 1. 经典算法研究系列:六、教你初步了解KMP算法、updated 2. KMP算法的前缀next数组最通俗的解释,如果看不懂我也没辙了字符串的匹配问题假设文本是一个长度为n的数组T[1…n],模式是一个长度为m<=n的数组P[1….m]。 找出所有在文本T=“abcabaabcaabac”中的模式P=“abaa”所有出现。 该模式仅在文本中出现了一次,在位移s=3处。简单的字原创 2017-05-12 21:43:32 · 483 阅读 · 0 评论 -
正则表达式匹配
请实现一个函数用来匹配包括’.’和’‘的正则表达式。模式中的字符’.’表示任意一个字符,而’‘表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配】题解注意两个匹配的特殊样例 1. “” , “.” 2. “”, “.*” 即字符串为’\原创 2017-04-21 12:33:04 · 251 阅读 · 0 评论 -
数组中重复的数
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。题解解法1: set或hashclass Solution {public: // Parameters: //原创 2017-04-20 13:34:54 · 523 阅读 · 0 评论 -
459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Englis原创 2017-04-16 02:34:11 · 346 阅读 · 0 评论 -
二叉树前序、中序、后序遍历非递归写法的透彻解析
本文转载自:http://blog.csdn.net/zhangxiangdavaid/article/details/37115355前言二叉树的三种遍历,递归写法只要理解思想,几行代码。可是非递归写法却很不容易。这里特地总结下,透彻解析它们的非递归写法。其中,中序遍历的非递归写法最简单,后序遍历最难。我们的讨论基础是这样的: //Binary Tree Nodetypedef st转载 2017-05-01 20:40:11 · 126 阅读 · 0 评论 -
孩子们的游戏(圆圈中最后剩下的数)
题目描述首先,让小朋友们围成一个大圈。然后随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数….这样下去….直到剩下最后一个小朋友获得神秘大奖,。请你试着想下,哪个小朋友会得到这份神秘大奖呢呢?(注:小朋友的编号是从0到n-1)题解解法1模拟环形链表 这里通过取模运算直原创 2017-04-19 15:15:37 · 386 阅读 · 0 评论 -
88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional原创 2017-05-06 17:22:21 · 312 阅读 · 0 评论 -
数据流中的中位数
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。题解解法1使用插入排序维护一个排好序的vector数组,Insert时间复杂度O(n),GetMedian()时间复杂度O(1)class Solution {public: void Insert(int原创 2017-04-24 23:44:54 · 482 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists: A: a1 → a2 ↘原创 2017-05-08 00:19:05 · 181 阅读 · 0 评论 -
Leetcode: Combination Sum
已知数组C和目标值T,要求在C中找出所有独特的组合使其和为T,C中所有的数和T都为正整数39. Combination SumNote: 1. C中无重复 2. 同一个数在组合中可以出现任意次比如C=[2, 3, 6, 7] ,T=7, 解为[ [7], [2, 2, 3]]解法简单回溯public class Solution { public List<List<Integ原创 2017-07-30 22:24:06 · 468 阅读 · 0 评论 -
寻找和为定值的多个数
15.3SumGiven 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原创 2017-06-11 20:49:37 · 831 阅读 · 4 评论 -
547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A原创 2017-06-02 21:37:00 · 495 阅读 · 0 评论 -
不用辅助内存交换两个数的值
首先先来看一下我们常用的交换算法,使用了一个辅助变量 void swap(int &a, int&b){ int temp = a; a = b; b = temp; }那么,怎么才能不用到这个辅助变量呢? 这里用到辅助变量是因为在更改a=b后要保留原有a的信息,如果想不用辅助变量,就要在已有的a和b中储存足够的信息。所以我们尝试在b中原创 2017-05-22 00:11:48 · 993 阅读 · 0 评论 -
526. Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤原创 2017-05-20 16:14:11 · 284 阅读 · 0 评论 -
477. Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given numb原创 2017-06-05 15:09:00 · 465 阅读 · 0 评论 -
503. Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first grea原创 2017-05-31 15:48:46 · 324 阅读 · 0 评论 -
413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:原创 2017-05-19 16:46:55 · 294 阅读 · 0 评论 -
406. Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this p原创 2017-05-18 15:06:42 · 199 阅读 · 0 评论 -
滑动窗口的最大值
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {原创 2017-04-25 16:21:42 · 521 阅读 · 0 评论 -
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by原创 2017-04-30 16:38:45 · 351 阅读 · 0 评论 -
数字在排序数组中出现的次数
统计一个数字在排序数组中出现的次数。题解:解法1:遍历从前往后遍历一遍,找到k开始计数为1,到不是k时终止。 时间复杂度O(n)解法2:二分查找找出k第一次和最后一次出现的下标,相减 时间复杂度O(lgn)如果只查找一次,然后向左右遍历有几个,并没有改变时间复杂度,还是O(n)class Solution {public: int GetNumberOfK(vector<int> da原创 2017-04-05 12:47:22 · 348 阅读 · 0 评论 -
401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit on the原创 2017-03-23 15:52:02 · 238 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5题解删除链表中所有等于val的节点非递归维持head的上一个节点prev(为方便用原创 2017-03-29 12:23:51 · 226 阅读 · 0 评论 -
414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1: Input: [3, 2, 1]原创 2017-04-07 16:28:45 · 174 阅读 · 0 评论 -
409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not consider原创 2017-03-22 12:31:14 · 304 阅读 · 0 评论 -
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 always原创 2017-03-21 16:44:02 · 248 阅读 · 0 评论 -
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the原创 2017-03-28 12:00:22 · 210 阅读 · 0 评论 -
542. 01 Matrix
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.Example 1: Input:0 0 00 1 00 0 0Output:0 0 00 1 00 0 0Example 2:原创 2017-03-20 21:43:42 · 259 阅读 · 0 评论 -
538. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.Example:I原创 2017-03-19 16:08:21 · 2870 阅读 · 0 评论 -
543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may no原创 2017-03-19 13:32:41 · 667 阅读 · 0 评论 -
地牢逃脱
给定一个 n 行 m 列的地牢,其中 ‘.’ 表示可以通行的位置,’X’ 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。输入描述每个输入包含 1 个测试用例。原创 2017-03-18 20:54:58 · 1079 阅读 · 0 评论 -
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2017-03-30 15:30:13 · 220 阅读 · 0 评论 -
213. House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all原创 2017-03-31 12:06:43 · 187 阅读 · 0 评论 -
304. Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Range Sum Query 2D The above rectangle原创 2017-03-31 15:38:44 · 246 阅读 · 0 评论 -
231. Power of Two
Given an integer, write a function to determine if it is a power of two.题解判断一个数是不是2的幂,即1,2,4,8……2^30(对int范围而言)解法1循环class Solution {public: bool isPowerOfTwo(int n) { //不大于0的肯定不是 if原创 2017-04-13 17:51:45 · 198 阅读 · 0 评论