59. Spiral Matrix II

题目

这道题copy网上的代码

 class Solution {
private:
int step[][];
bool canUse[][];
public:
void dfs(int dep, vector<vector<int> > &matrix, int direct, int x, int y)
{
for(int i = ; i < ; i++)
{
int j = (direct + i) % ;
int tx = x + step[j][];
int ty = y + step[j][];
if ( <= tx && tx < matrix.size() && <= ty && ty < matrix[].size() && canUse[tx][ty])
{
canUse[tx][ty] = false;
matrix[tx][ty] = dep;
dfs(dep + , matrix, j, tx, ty);
}
}
} vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
step[][] = ;
step[][] = ;
step[][] = ;
step[][] = ;
step[][] = ;
step[][] = -;
step[][] = -;
step[][] = ;
vector<vector<int> > ret(n, vector<int>(n));
memset(canUse, true, sizeof(canUse));
dfs(, ret, , , -); return ret;
} };

----------------------------------------------------------------------------分割线----------------------------------------------------------------------

60. Permutation Sequence

题目

分析:这道题主要考数学推断

在n!个排列中,第一位的元素总是(n-1)!一组出现的,也就说如果p = k / (n-1)!,那么排列的最开始一个元素一定是nums[p]。

假设有n个元素,第K个permutation是
a1, a2, a3, .....   ..., an
那么a1是哪一个数字呢?
那么这里,我们把a1去掉,那么剩下的permutation为
a2, a3, .... .... an, 共计n-1个元素。 n-1个元素共有(n-1)!组排列,那么这里就可以知道
设变量K1 = K
a1 = K1 / (n-1)!
同理,a2的值可以推导为
a2 = K2 / (n-2)!
K2 = K1 % (n-1)!
 .......
a(n-1) = K(n-1) / 1!
K(n-1) = K(n-2) /2!
an = K(n-1)

代码如下:

 class Solution {
public:
string getPermutation(int n, int k) {
vector<int> nums(n);
int pCount = ;
for(int i = ; i < n; ++i) {
nums[i] = i + ;
pCount *= (i + );
} k--;
string res = "";
for(int i = ; i < n; i++) {
pCount = pCount/(n-i);
int selected = k / pCount;
res += ('' + nums[selected]); for(int j = selected; j < n-i-; j++)
nums[j] = nums[j+];
k = k % pCount;
}
return res;
}
};

------------------------------------------------------------------------分割线--------------------------------------------------------------------------

61. Rotate List

题目

代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
//如果k值大于链表长度应该怎么处理
if(NULL == head)
return NULL;
ListNode* temp = head;
int count = ;
while(temp != NULL)
{
count++;
temp = temp->next;
}
k = k%count;
if(k == )
return head;
ListNode *first,*second;
first = head;
int i=k;
while(i--)
{
//if(NULL == first)
// return NULL;
first = first->next;
}
second = head;
while(first->next != NULL)
{
first = first->next;
second = second->next;
}
temp = head;
head = second->next;
first->next = temp;
second->next = NULL;
return head;
}
};

Leetcode题解(20)的更多相关文章

  1. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

  3. LeetCode题解-20.有效的括号

    题目 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 示例 ...

  4. [LeetCode 题解]: Interger to Roman

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an i ...

  5. [LeetCode 题解]: Roman to Interger

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a ro ...

  6. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  7. [LeetCode 题解]: Maximum Subarray

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the c ...

  8. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  9. [LeetCode 题解]: plusOne

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a no ...

  10. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

随机推荐

  1. JVM菜鸟进阶高手之路五

    转载请注明原创出处,谢谢! 参考gc,发现大概一小时运行一次FGC,特别奇怪,笨神一看这样的问题就知道是system gc导致的,rmi默认一小时主动触发一次,由于没有gc日志,通过jstat命令观察 ...

  2. Spring连接池的常用配置

    1.连接池概述 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个 应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池正 ...

  3. StringBuffer类的构造方法

    public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象(默认是16个字符) public String ...

  4. MVC轻量web应用

    前言:为了能够清晰的理解mvc架构,以一个简单的mvc架构web应用为例.例子为一个beer项目,用户可通过页面选择啤酒的种类,服务端根据用户的选择,给出相应的推荐. 涉及到的代码: view层:fo ...

  5. Invoke 用法

    转自:http://blog.sina.com.cn/s/blog_5a6f39cf0100s23x.html 在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法 ...

  6. Cookie实现登录记住密码

    Cookie实现记住登录密码,用户可以自由选择是否记住密码,或者用户之前选择记住了,但是某一次又不想记住了,需要将之前对应的Cookie删除掉 Cookie相当于map 也是键值对的形式,但是并不相同 ...

  7. 关于Vue问题记录

    第一次安装Vue时,npm run dev报错处理 1.如果是报错:提示说没找到test这个文件夹 参考资料:https://segmentfault.com/q/1010000010893904 就 ...

  8. Cow Exhibition 变种背包

    Cow Exhibition Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  9. http://codeforces.com/contest/536/problem/B

    B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. Django进阶篇【2】

    在学习之前,我们补充一个知识点(static用法) 创建APP 配置: setting.py STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static') ...