摘要: Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line 阅读全文
posted @ 2014-01-12 21:38 七年之后 阅读(377) 评论(0) 推荐(0) 编辑
摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.思考:先首尾连成环,head前进(len-k%len)步,拆环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L 阅读全文
posted @ 2014-01-12 16:55 七年之后 阅读(154) 评论(0) 推荐(0) 编辑
摘要: The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文
posted @ 2014-01-12 16:45 七年之后 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]思考:ACM蛇形填数。class Solution {public: vector > generateMatrix(int n) { vector > res; if(n==0) ... 阅读全文
posted @ 2014-01-12 14:53 七年之后 阅读(208) 评论(0) 推荐(0) 编辑
摘要: Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思考:同样,满足条件res++即可。class Solution {private: int res;public: bool check(int *a,int k,int i) { for(int j=0;j<k;j++) { if(a[j]==i||abs(a[j]-i)==abs... 阅读全文
posted @ 2014-01-12 13:38 七年之后 阅读(197) 评论(0) 推荐(0) 编辑
摘要: Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinc... 阅读全文
posted @ 2014-01-12 13:32 七年之后 阅读(384) 评论(0) 推荐(0) 编辑