摘要:
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 阅读全文
摘要:
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 阅读全文
摘要:
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) ... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文