![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
acm
jjfly
无他,唯手熟尔
展开
-
Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] /** * Definiti原创 2015-08-22 16:53:11 · 534 阅读 · 0 评论 -
Sudoku Solver
解决数独问题 class Solution { public: bool isValid(vector >&board, int a, int b){ for(int i = 0; i < 9; i++) if(i != a && board[i][b] == board[a][b]) return false;原创 2015-01-06 00:17:20 · 1010 阅读 · 0 评论 -
Unique Binary Search Trees II
生成不相同的二叉树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ c原创 2015-01-22 10:49:45 · 576 阅读 · 0 评论 -
Clone Graph
图的复制 class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node ){ unordered_mapmap; if(node == NULL) return NULL; return clone(node,ma原创 2015-01-01 19:59:22 · 640 阅读 · 0 评论 -
Leetcode: Reverse Nodes in k-Group
链表的倒转, /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * Definition for singly-link原创 2014-12-14 00:12:15 · 859 阅读 · 0 评论 -
4sum
k-sum问题都可以有2-sum引申出来解决,但是时间复杂度为O(n^k-1)。应该要用哈希解决才好的,之后再看看 class Solution{ private: vector > ans; public: vector >fourSum(vector &num, int target){ sort(num.begin(),num.end()); ans.clear()原创 2015-01-16 23:34:00 · 510 阅读 · 0 评论 -
Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially pos原创 2015-01-30 22:42:27 · 582 阅读 · 0 评论 -
Maximal Rectangle
求一个01二位数组最大的矩阵,把它化为直角图再一行一行地算 class Solution{ public: int maximalRectangle(vector > &martix){ int height[1000][1000]; int maxx = -1; int row = martix.size(); if(row == 0) return 0;原创 2015-01-12 15:34:50 · 623 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array I && II
这些题比较考验边界条件 Find Minimum in Rotated Sorted Array class Solution { public: int findMin(vector &num) { int left = 0, right = num.size()-1; while(num[left] > num[right]){原创 2015-01-30 18:40:38 · 568 阅读 · 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() and原创 2015-01-27 21:07:19 · 554 阅读 · 0 评论 -
Balanced Binary Tree
检测一个树是否平衡,不需要求出高度,而是从底到顶检测是否平衡,这样才算法时间复杂度为O(n)。但是需要额外的O(logn)的空间 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(原创 2015-01-27 21:19:31 · 535 阅读 · 0 评论 -
Median of Two Sorted Arrays
There are two sorted arrays A and B 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)). 用二分的方法求合并数组的中值。用找第k大的值来求,不断去掉较小的一半,原创 2015-01-29 11:20:45 · 585 阅读 · 0 评论 -
Search in Rotated Sorted Array I && II
对翻过一次的排序数组二分查找,要利用好已排序这个条件 class Solution { public: int search(int A[], int n, int target) { int left = 0, right = n-1; while(left <= right){ int mid = (left+right)/2原创 2015-01-28 16:20:02 · 571 阅读 · 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 in原创 2015-01-29 00:22:23 · 576 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. 求树的一点到另一点的最大路径,利用递归的方法,ans 在 左子树,右子树,root+左+右的最大中产生。 /** * Definition for binary tree * struct原创 2015-01-27 20:24:19 · 534 阅读 · 0 评论 -
Minimum Window Substring
双指针思想,尾指针不断往后扫,当扫到有一个窗口包含了所有T的字符,然后再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的原创 2015-01-08 23:09:55 · 515 阅读 · 0 评论 -
N-Queens
敲到n皇后,map一下算法 class Solution { public: vector > res; void helper(vector &nums, int index, int n) { if(index == n) { bool flag = true; for(int i = 0; i < n && flag; i++)原创 2014-11-12 16:17:28 · 569 阅读 · 0 评论 -
[leetcode]Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very原创 2015-01-24 14:22:28 · 583 阅读 · 0 评论 -
Path Sum II
利用递归处理 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas原创 2015-01-25 00:58:21 · 939 阅读 · 0 评论 -
First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2015-09-09 15:58:30 · 428 阅读 · 0 评论 -
Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p原创 2015-07-12 16:05:54 · 551 阅读 · 0 评论 -
Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4. 这题如果按照题意做会超时,用移位的操作很妙,因为是并操作,其实只要最小的数和最大的数从左往右取相同的位即可。遇到不同就退出,因为他们中间肯定有数该为为0。 class Solution { public:原创 2015-05-09 01:42:45 · 824 阅读 · 0 评论 -
Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume原创 2015-04-13 16:53:59 · 553 阅读 · 0 评论 -
各种排序的实现与复杂度分析(持续更新)
稳定性: 选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法, 冒泡排序、插入排序、归并排序和基数排序是稳定的排序算法。 复杂度 冒泡法: 复杂度为O(n*n)。当数据为正序,将不会有交换。复杂度为O(0)。不说了。 直接插入排序:O(n*n) 希尔排序:算法的复杂度为n的1.2次幂 选择排序:O(n*n) 快速排序:不稳定,平均时间复杂度O(nlogn),最坏情况时间复杂度为O(n...原创 2015-01-10 00:45:32 · 965 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 不难,考细节 class Solution { public: string addBinary(string a, string b) {原创 2015-03-11 20:14:39 · 623 阅读 · 0 评论 -
strcpy的实现
要考虑内存重叠 char * strcpy(char *dst,const char *src) { assert(dst != NULL && src != NULL); char *ret = dst; my_memcpy(dst, src, strlen(src)+1); return ret; } char *my_memcpy(char *dst,原创 2015-03-09 13:21:07 · 571 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2原创 2015-03-09 15:09:27 · 507 阅读 · 0 评论 -
Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number原创 2015-03-09 01:35:47 · 669 阅读 · 0 评论 -
Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string原创 2015-03-07 00:29:27 · 707 阅读 · 0 评论 -
Implement strStr() && kmp算法
用这题复习下kmp算法。kmp网上坑爹的是有很多种匹配方式,容易混淆,后人要警惕啊。这里对要查找的字符串的next全部置为-1,预处理时i和next[i-1]+1相比较。和http://kb.cnblogs.com/page/176818/ 这里相似。预处理完再匹配,第i个字符不匹配则比较第next[i-1]+1个。 class Solution{ public: cha原创 2015-03-07 21:50:03 · 551 阅读 · 0 评论 -
Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return原创 2015-03-07 15:39:26 · 510 阅读 · 0 评论 -
Multiply Strings
字符串的相乘,可用于解决大数相乘,注意首尾颠倒 class Solution { public: string multiply(string num1, string num2) { if(num1 == "0" || num2 == "0") return "0"; int l1 = num1.length(), l2 = num2.length(); i原创 2015-03-01 19:54:22 · 576 阅读 · 0 评论 -
Valid Number
判断是否数字,考虑多种情况 class Solution{ public: bool isNumber(string s){ int i = 0; while(s[i] == ' ') ++i; while(s[i] == '+' || s[i] == '-') ++i; bool exp = false, space = false, point = fa原创 2015-02-13 15:23:35 · 490 阅读 · 0 评论 -
二叉树的非递归遍历
先是中序遍历 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ ...原创 2015-01-21 21:37:45 · 617 阅读 · 0 评论 -
翻转链表
之前做过链表的翻转,这里看到一个更简单的实现 ListNode* Reverse(ListNode *head) { ListNode *reHead = NULL; ListNode *prev = NULL; ListNode *Node = head; while(Node != NULL) { ListNode *next = Node->原创 2014-12-17 00:22:03 · 846 阅读 · 0 评论 -
permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 函数原型: vect转载 2014-11-07 11:53:25 · 546 阅读 · 0 评论 -
1484. 守望者的逃离
虽然不难,但是本渣还是要看一下题解才做出来 #include #include using namespace std; int dis[300010]; int main(int argc, char const *argv[]) { int m, s, t; bool f = 0; while(cin >> m >> s >> t) {原创 2013-08-31 13:49:36 · 1064 阅读 · 0 评论 -
1152. 简单的马周游问题
深搜,不怎么会用,不过渐渐有点懂了,看了网上的代码才做出来 #include #include using namespace std; int q[31]; int xy[8][2] = {{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}}; bool vis[6][7],f; void dfs(int x, int y, int原创 2013-09-21 10:53:40 · 1500 阅读 · 0 评论 -
1006. Team Rankings
转 http://blog.csdn.net/ederick/article/details/7226807 和http://www.w3c.com.cn/sicily-1006-team-rankings 这题虽然大概懂意思,但还是不太懂那些数字怎么来的。找些感觉不错的代码先mark #include #include #include using namespace std;转载 2013-10-05 02:09:17 · 542 阅读 · 0 评论 -
1282. Computer Game
kmp算法,说实话理解起来真不容易,特别是自学。网上的讲解next有几种标志方法,虽然大概算法差不多,但是有点迷惑。想了几天,基本明白了,但是还是不太透彻。不能浪费太多时间在这里,以后回顾一下。kmp算法把复杂度O(nm)变成了O(n+m),确实很不错,算法的魅力就在于此,妙不可言,吾等渣渣只能膜拜。我按照算法导论的方法,即next[1] = 0, 下标从1开始,网上有不错的代码http://bl原创 2013-10-10 23:37:02 · 787 阅读 · 0 评论