- 博客(21)
- 收藏
- 关注
原创 79. Word Search
题目:我的代码:class Solution {public: bool exist(vector>& board, string word) { if (board.empty() || board[0].empty()) return false; int m = board.size(); int n = board[0]
2018-01-19 18:27:56 108
原创 17. Letter Combinations of a Phone Number
题目:我的代码:class Solution {public: vector key {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector letterCombinations(string digits) { vector res; if (digi
2018-01-19 17:28:27 125
原创 46. Permutations
题目:我的代码:class Solution {public: vector> permute(vector& nums) { vector> res; sort(nums.begin(), nums.end()); do { res.push_back(nums); } while (next_pe
2018-01-19 17:16:48 108
原创 49. Group Anagrams
题目:我的代码:class Solution {public: vector> groupAnagrams(vector& strs) { int n = strs.size(); sort(strs.begin(), strs.end()); map> map_; for (int i = 0; i < n; i++) {
2018-01-18 21:32:20 157
原创 14. Longest Common Prefix
题目:我的代码:class Solution {public: string longestCommonPrefix(vector& strs) { if(strs.size()==0)return ""; if(strs.size()==1)return strs[0]; string s = strs[0]; strin
2018-01-18 21:05:10 84
原创 40. Combination Sum II
题目:我的代码:class Solution {public: vector> vvi; vector> combinationSum2(vector& can, int target) { sort(can.begin(), can.end()); vector vi; dfs(can, target, 0, vi);
2018-01-18 21:03:12 111
原创 39. Combination Sum
题目:我的代码:class Solution {public: vector> vvi; vector> combinationSum(vector& can, int target) { sort(can.begin(), can.end()); vector vi; dfs(can, target, 0, vi);
2018-01-18 20:53:49 97
原创 125. Valid Palindrome
题目:我的代码:class Solution {public: bool isPalindrome(string s) { //if(s = " ") return true; for (int i = 0, j = s.length()-1; i <= j; ) { if (!((s[i] >= 'A' && s[i] = 'a'
2018-01-18 15:33:25 130
原创 112. Path Sum
题目:我的代码:二叉树问题一般都需要用到递归,这道题也一样。如果树为空则返回false,如果判断的节点没有孩子并且值等于sum则返回true,每次向下递归都减去root->val,如果递归到叶子节点,能找到一个相等的节点,则返回true。
2018-01-18 14:54:14 115
原创 74. Search a 2D Matrix
题目:我的代码:class Solution {public: bool searchMatrix(vector>& matrix, int target) { if (matrix.size() == 0 || matrix[0].size() == 0) return false; int m = matrix.size(), n = matrix[0
2018-01-18 14:28:40 116
原创 223. Rectangle Area
题目:我的代码:class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int area1 = (C-A)*(D-B); int area2 = (G-E)*(H-F); int x = 0, y =
2018-01-18 10:31:19 105
原创 64. Minimum Path Sum
题目:我的代码:class Solution {public: int minPathSum(vector>& grid) { int n = grid.size(); int m = grid[0].size(); int res[n][m]; res[0][0] = grid[0][0]; for(int
2018-01-18 09:55:44 109
原创 100. Same Tree
题目:我的代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };
2018-01-18 09:31:45 95
原创 78. Subsets
题目:我的代码:class Solution {public: vector> subsets(vector& nums) { vector> res; if(nums.size() == 0) return res; vector row; res.push_back(row); row.pus
2018-01-13 11:01:06 92
原创 199. Binary Tree Right Side View
题目: 我的代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
2018-01-12 22:33:43 100
原创 200. Number of Islands
题目:我的代码:class Solution {public: int numIslands(vector>& grid) { if (grid.empty()) return 0; typedef pair Index; int dx[4] = {0, 0, -1, 1}; int dy[4] = {-1, 1
2018-01-12 21:28:28 130
原创 120. Triangle
题目:我的代码:class Solution {public: int minimumTotal(vector>& triangle) { int n = triangle.size(); int num[n] = {0}; for (int i = 0; i < n; i++) { num[i] = t
2018-01-12 18:13:45 95
原创 2. Add Two Numbers
题目:我的代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:
2018-01-12 17:38:48 122
原创 48. Rotate Image
题目:我的代码:题目要求不可以新建一个矩阵来存放,必须在原矩阵上修改。网上查到可以通过对称变换得到旋转90度的矩阵,于是我就先对矩阵进行对角线的对称变换,再进行竖轴对称变换。但我一开始写出来的代码得到的结果是错的,检查发现,在对角线对称变换的时候,由于条件没有设置正确,导致每个数字都变换了两次结果等于没有变换(上图的错误版本),后来修改了j的条件之后就ac了。
2018-01-12 15:47:34 115
原创 27.Remove Element
题目:我的代码:本题主要的问题在于不允许重新开一个数组,是要在原数组上改动。不过由于题目对数组的顺序没有要求,所以我先使用冒泡排序对数组进行排序,然后将数组中等于val的数字的数量保存在cnt中,最后一步修改nums数组的内容得到结果。
2018-01-12 15:15:25 124
原创 1.tow sums
题目:我的答案:该题目比较简单,由于输入确保了只有一个解,即只需要对输入数组进行遍历两两相加,一旦结果等于target直接返回下标即可
2018-01-12 14:38:26 132
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人