- 博客(49)
- 收藏
- 关注
转载 可重入函数(转载)
可重入函数 在 实时系统的设计中,经常会出现多个任务调用同一个函数的情况。如果这个函数不幸被设计成为不可重入的函数的话,那么不同任务调用这个函数时可能修改其他任 务调用这个函数的数据,从而导致不可预料的后果。那么什么是可重入函数呢?所谓可重入是指一个可以被多个任务调用的过程,任务在调用时不必担心数据是否会 出错。不可重入函数在实时系统设计中被视为不安全函数。 满足下列条件的函数多...
2017-09-04 22:08:00
92
转载 328. Odd Even Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNod...
2017-07-13 14:39:00
97
转载 206.Reverse Linked List
迭代:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: L...
2017-07-13 13:50:00
82
转载 67.Add Binary
思路:先翻转,然后相加。(需要多了解一些操作字符串的函数,能方便好多)。class Solution {public: string addBinary(string a, string b) { string res = ""; int carry = 0; int valA; int valB; ...
2017-06-17 10:11:00
82
转载 8.String to Integer(atoi)
思路:细节题,应该不断和面试官交流确定一些特殊情况。class Solution {public: int myAtoi(string str) { int num = 0; int sign = 1; int i; for(i = 0; i < str.length(); i++){ ...
2017-06-17 09:26:00
70
转载 28.Implement strStr()
思路:暴力:class Solution {public: int strStr(string haystack, string needle) { if(needle.length() == 0) return 0; bool res = true; int len1 = haystack.length(); ...
2017-06-16 09:31:00
61
转载 125.Valid Palindrome
思路:开始想的是先把特殊符号去掉,但是超时了。事实上直接在遍历中处理就行了。参考class Solution {public: bool isPalindrome(string s) { int left = 0, right = s.length()-1; while(left < right){ if(!...
2017-06-16 08:42:00
58
转载 20.Valid Parentheses
思路:栈的应用。如果是'(','{','[',则只需入栈即可,如果是'}',']',')',则要看栈顶的字符是否和其匹配。class Solution {public: bool isValid(string s) { stack<char> res; for(int i = 0; i < s.length(); i++...
2017-06-16 07:16:00
77
转载 102.Binary Tree Level Order Traversal
思路:递归,利用level表示第几层。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r...
2017-06-15 16:34:00
65
转载 94.Binary Tree Inorder Traversal
思路:递归。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {...
2017-06-15 14:57:00
67
转载 144.Binary Tree Preorder Traversal
思路:递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}...
2017-06-15 11:06:00
63
转载 106.Construct Binary Tree from Inorder and Postorder Traversal
思路递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} ...
2017-06-15 10:42:00
78
转载 105.Construct Binary Tree from Preorder and Inorder Traversal
思路:递归。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}...
2017-06-15 09:06:00
74
转载 90.Subsets II
思路:和Subsets类似,因为数组中有重复元素,所以用set存储结果,相当于正常遍历然后去掉了重复。class Solution {public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { sort(begin(nums),end(nums...
2017-06-14 14:06:00
64
转载 78.Subsets
思路:dfs,每个元素有两种选择,选或者不选。class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { sort(begin(nums),end(nums)); vector<vector<int...
2017-06-14 09:34:00
94
转载 83.Merge Sorted Array
思路:从后向前赋值,这样就可以避免移动数组元素。参考class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int k = m+n-1; int i = m-1; ...
2017-06-14 08:19:00
60
转载 80.Remove Duplicates from Sorted Array II
思路:两个指针,比Remove Duplicates from Sorted Array多了一个count,记录是否超过两次。直接贴代码。class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size() <= 2) return nu...
2017-06-13 22:10:00
65
转载 79.Word Search
思路:dfs,重点写在注释里面。这道题做了好久,以后不做无意义的尝试,想好了再改,调试。class Solution {public: bool exist(vector<vector<char>>& board, string word) { for(int i = 0; i < board.size(); i++...
2017-06-13 10:14:00
90
转载 75.Sort Colors
思路:设置两个指针red,blue,遇到0就交换到前面,red++,i++,遇到blue就交换到后面,因为red前面已经遍历过,所以交换red时需要i++,但是交换blue只需要blue++。(描述的好乱啊,还是贴上分析的好的博客吧。。)参考class Solution {public: void sortColors(vector<int>& nu...
2017-06-12 09:52:00
110
转载 74.Search a 2D Matrix
思路:将二维数组看成一个一维数组,数组长度为m*n,对于位置x,它映射到matrix数组里面的位置为i/n,i%n,然后利用二分查找即可。class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { if(matri...
2017-06-11 19:29:00
65
转载 73.Set Matrix Zeroes
思路:设置两个bool数组row[matrix[0].size()],col[matrix.size()]分别记录行和列中的0,如果matrix[i][j]为0,那么将row[j]和col[i]赋值为0。先遍历数组matrix,更新bool数组,然后遍历bool数组,更新matrix。(也可以将0标记到第一行和第一列,这样空间复杂度为\(O(1)\),以后更新)class Sol...
2017-06-11 19:08:00
71
转载 66.Plus One
思路:直接模拟加法class Solution {public: vector<int> plusOne(vector<int>& digits) { int lead = 0; int in = 1; for(int i = digits.size()-1; i >= 0; i--)...
2017-06-11 08:22:00
49
转载 64.Minimum Path Sum
64.Minimum Path Sum思路:DFS + 备忘录,和Unique Path类似。参考文章class Solution {public: int minPathSum(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[...
2017-06-10 23:14:00
54
转载 63.Unique Paths II
思路:dfs,超时class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].si...
2017-06-10 14:47:00
50
转载 62.Unique Paths
思路:dfs,超时class Solution {public: int uniquePaths(int m, int n) { return dfs(m,n,0,0); } int dfs(int m,int n,int curi,int curj){ if(m-1 == curi && n-1 == c...
2017-06-10 13:33:00
85
转载 54.Spiral Matrix
思路直接模拟程序运行,设置\(rowbegin,rowend,colbegin,colend\)变量。注意:要判断是否遍历过。 参考class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<i...
2017-06-09 12:03:00
57
转载 59.Spiral Matrix II
思路类似Spiral Matrix,直接模拟class Solution {public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n,vector<int>(n)); int row...
2017-06-09 12:02:00
53
转载 55.Jump Game
思路:dfs,超时class Solution {public: bool canJump(vector<int>& nums) { return dfs(nums,0); } bool dfs(vector<int>& nums,int i){ if(i == nums....
2017-06-09 10:41:00
59
转载 【转载】我做科研的几点体会
上网时看到的,虽然方向不同,但是有些建议很中肯。链接:http://blog.sina.com.cn/s/blog_50c1545101008spw.html梁大伟 序 我刚刚开始做实验的时候,别人怎么说我就怎么做,每天在实验台旁干到深夜,以为这就是科研了。两个月过去,突然发现自己还在原地踏步。那种感觉,只能用“沮丧”来形容。我开始置疑自己的行为和观念。感觉有种习惯的...
2017-06-08 18:42:00
435
转载 53.Maximum Subarray
思路:DP,设置数组res,存储以i结尾的连续数组和的最大值。如果i之前子数组最大和小于等于0,那么res[i] = nums[i],否则res[i] = nums[i] + res[i-1]。复杂度为\(O(n)\)。class Solution {public: int maxSubArray(vector<int>& nums) { ...
2017-06-08 09:55:00
61
转载 48.Rotate Image
思路:先转置,然后对称。复杂度为\(O(n^2)\)。每次用swap交换,额外占用一个空间,所以空间复杂度为\(O(1)\)。class Solution {public: void rotate(vector<vector<int>>& matrix) { for(int i = 0; i < matrix[0]....
2017-06-08 09:19:00
52
转载 40.Combination Sum II
思路:类似Combination Sum,但是数组有重复元素,同时要求元素不能重复使用。复杂度和Combination Sum类似。class Solution {public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {...
2017-06-08 08:53:00
53
转载 39.Combination Sum
思路:利用回溯,其运行过程可以看成遍历一颗4叉树(因为要求不重复,所以中间剪掉了部分树枝),复杂度为\(O(n^h)\),\(h\)为树的高度,这里\(h\)相当于只用数组里最小的数加起来大于等于target时所用的数目。比如[2,3,6,7],target = 7。2+2+2+2 > 7,所以h为4。class Solution {public: vector&...
2017-06-07 23:14:00
58
转载 35.Search Insert Position
思路:直接遍历数组。复杂度为\(O(n)\)。class Solution {public: int searchInsert(vector<int>& nums, int target) { if(target < nums[0]) return 0; if(target > nums[nums.size...
2017-06-07 22:28:00
66
转载 34.Search for a Range
思路:二分查找,找到后分别向左向右遍历有多少重复的,然后记录左右端点即可,但是这样在某些情况下复杂度为\(O(n)\),比如{8,8,8,8,8,8,8,8,8}。竟然过了。。。class Solution {public: vector<int> searchRange(vector<int>& nums, int target) {...
2017-06-07 15:56:00
57
转载 33.Search in Rotated Sorted Array
思路:利用二分查找,设\(mid = (lo+hi)/2\),如果 \(nums[hi]>nums[mid]\)说明从\(mid\) 到\(hi\)是有序的,然后判断target是否在\(mid\)和\(hi\)之间,如果在,则\(lo = mid+1\),否则\(hi = mid-1\)。另一种情况类似。复杂度为\(O(log(n))\)。class Solution {...
2017-06-07 10:48:00
55
转载 31.Next Permutation
思路:参考 http://blog.csdn.net/abcjennifer/article/details/40152323, 复杂度为 \(O(nlog(n))\)class Solution {public: void nextPermutation(vector<int>& nums) { int i,k = 0; ...
2017-06-07 09:17:00
45
转载 27.Remove Element
思路:复杂度 \(O(n)\)。class Solution {public: int removeElement(vector<int>& nums, int val) { int res,k=0; for(int i = 0; i < nums.size(); i++){ if(num...
2017-06-07 08:32:00
53
转载 26.Remove Duplicates from Sorted Array
思路:记录每个元素前面有几个重复的数字,但是写的时候感觉容易出错,还是第二种好一点。复杂度\(O(n)\)。class Solution {public: int removeDuplicates(vector<int>& nums) { int i = 0,k = 0; if(nums.size() < 2) ...
2017-06-06 22:59:00
50
转载 18.4Sum
思路:类似3Sum,利用map存储可以省去判断重复数组,复杂度为 \(O(n^4)\),因为set判断元素是否重复也需要 \(O(n)\).class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { s...
2017-06-06 21:20:00
52
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人