1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2014年1月10日

摘要: 每个string sort。。。然后这个作为group的keyclass Solution {public: vector anagrams(vector &strs) { unordered_map >group; for(int i = 0 ; i ans; for(auto it = group.begin() ; it != group.end() ; it++) { if(it -> second.size() > 1) ans.insert(ans.end() , it->second... 阅读全文

posted @ 2014-01-10 22:17 1957 阅读(115) 评论(0) 推荐(0) 编辑

摘要: = =开始想乘个矩阵啥的就转啦。。。结果说in-place...那就按对角线翻转,在上下翻转吧。。。class Solution {public: void rotate(vector > &matrix) { int n = matrix.size(); if(n <= 1) return ; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < n - i; j++) { swap(matrix[i][j] , matrix[n... 阅读全文

posted @ 2014-01-10 21:34 1957 阅读(561) 评论(0) 推荐(0) 编辑

摘要: 想用递归。。。结果感觉有点麻烦。。。那要么就用stl的next_permutation吧,,,不过这样好没意思。。。还是自己实现吧。。。class Solution {public: bool next_per(vector& num) { int size = num.size(); if(size 0 && num[idx] 0) { idx --; int maxr = size - 1; while(num[maxr] > permuteUnique(vector &num) { ... 阅读全文

posted @ 2014-01-10 20:56 1957 阅读(228) 评论(0) 推荐(0) 编辑

摘要: dfs枚举class Solution {public: unordered_set st; void search(vector &num , vector > &ans , vector& tmp , int dep ,int size) { if(dep >= size) { ans.push_back(tmp); return ; } for(int i = 0 ; i > permute(vector &num) { sort(num.begin() ,... 阅读全文

posted @ 2014-01-10 19:50 1957 阅读(687) 评论(0) 推荐(0) 编辑

摘要: 大数乘法..写的有点复杂...class Solution {public: string multiply(string num1, string num2) { int size1 = num1.size(); int size2 = num2.size(); vector n1(size1,0); vector n2(size2,0); vector n3(size1+size2+2 , 0); reverse(num1.begin() , num1.end()); r... 阅读全文

posted @ 2014-01-10 15:02 1957 阅读(229) 评论(0) 推荐(0) 编辑

摘要: 扫描leftmost和rightmostclass Solution {public: int trap(int A[], int n) { if(n left(n , 0); vector right(n , 0); //left most for(int i = 1 ; i = 0 ; i--) { right[i] = max(right[i + 1] , A[i+1]); } int sum = 0; for(int i = 0 ; i A[... 阅读全文

posted @ 2014-01-10 14:10 1957 阅读(143) 评论(0) 推荐(0) 编辑

摘要: 和上一题差不多class Solution {public: void search(vector >& ans , vector& tmp ,vector &candidates , int target , int sum , int start) { if(sum > target) return; if(sum == target) { ans.push_back(tmp); return ; } int prev = -1; for(int i = start ;... 阅读全文

posted @ 2014-01-10 13:40 1957 阅读(121) 评论(0) 推荐(0) 编辑

摘要: 裸的dfsclass Solution {public: void search(vector >& ans , vector& tmp ,vector &candidates , int target , int sum , int start) { if(sum > target) return; if(sum == target) { ans.push_back(tmp); return ; } for(int i = start ; i > combination... 阅读全文

posted @ 2014-01-10 11:24 1957 阅读(136) 评论(0) 推荐(0) 编辑

摘要: 桶排序class Solution {public: int firstMissingPositive(int A[], int n) { for (int i = 0 ; i 0 && A[i] <= n && A[i] != A[A[i] -1]) swap(A[i] , A[A[i]-1]); } for (int i = 0 ; i < n ; i++) if(A[i] != i + 1) return i + 1; return n + 1... 阅读全文

posted @ 2014-01-10 10:59 1957 阅读(108) 评论(0) 推荐(0) 编辑

摘要: 按照说明模拟。。。stringstream挺好用的class Solution {public: string count(const string& now) { stringstream ss; int i = 0; int size = now.size(); int prev = -1; while(i < size) { while(i + 1< size && now[i] == now[i + 1]) i++; ss << (i - prev) << now[i];... 阅读全文

posted @ 2014-01-10 00:17 1957 阅读(137) 评论(0) 推荐(0) 编辑