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. kettle的HTTPPOST控件发送WSDL的webservice请求配置

    1.webservice请求的URL:http://pubservice.rjhn.com.cn/AppserviceTest/JsonWcfService.svc?WSDL 2.使用SOAPUI测试 ...

  2. 为什么要用深度学习来做个性化推荐 CTR 预估

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:苏博览 深度学习应该这一两年计算机圈子里最热的一个词了.基于深度学习,工程师们在图像,语音,NLP等领域都取得了令人振奋的进展.而深 ...

  3. GitHub使用(四) - 关于分支Branch

    1. 什么是分支Branch? 我初步的理解为:GitHub仓库默认有一个master的分支,当我们在master分支开发过程中接到一个新的功能需求,我们就可以新建一个分支同步开发而互不影响,开发完成 ...

  4. 【JVM】Java中的JavaCore/HeapDump文件及其分析方法

    产生时间 Java程序运行时,有时会产生JavaCore及HeapDump文件,它一般发生于Java程序遇到致命问题的情况下. 有时致命问题发生后,Java应用不会死掉,还能继续运行: 但有时致命问题 ...

  5. oracle 行转列 列转行

    行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...

  6. Mysql配置文件my.cnf详细说明

    [表名大小写配置] MySQL在Linux下数据库名.表名.列名.别名大小写规则:  1.数据库名与表名是严格区分大小写  2.表的别名是严格区分大小写  3.列名与列的别名在所有的情况下均是忽略大小 ...

  7. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  8. bzoj3997组合数学(求最长反链的dp)

    组合数学 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走 ...

  9. vue数组语法兼容问题

    先来一行代码: <a :href="['NewsNote.asp?ID='+item.ID+'&MenuType=C']" v-text="item.Tit ...

  10. 入坑IT十年(二)技术以外

    上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...