Leetcode题解(20)
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)的更多相关文章
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- LeetCode题解-20.有效的括号
题目 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 示例 ...
- [LeetCode 题解]: Interger to Roman
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an i ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode 题解]: Maximum Subarray
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Find the c ...
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [LeetCode 题解]: plusOne
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a no ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
随机推荐
- The area 积分积分
The area Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 初识Hibernate之关联映射(一)
上篇文章我们对持久化对象进行的学习,了解了它的三种不同的状态并通过它完成对数据库的映射操作.但这都是基于单张表的操作,如果两张或者两张以上的表之间存在某种关联,我们又该如何利用持久化对象进行操作呢?本 ...
- Python自学笔记-面向对象编程(Mr seven)
类的成员可以分为三大类:字段.方法和属性. 一.字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段属于对象 静态字段属于类 二.方法 方法 ...
- ubuntu环境下lnmp环境搭建(2)之Nginx
1. ubuntu编译安装nginx http://www.cnblogs.com/zhangjun516/archive/2013/02/03/2890990.html 1. 手动编译安装 Ngin ...
- SQL 2008 外网访问说明
1. 安装SQL2008 . 安装SQL2008之前,必须预先安装.NET Framework 3.5,和Windows Installer 4.5 Redistributable. 可能产生错误: ...
- Silverlight:telerik RadControls for Silverlight 主题使用心得
默认情况下: telerik RadControls控件使用的是Office Black 主题,就算在App.xaml.cs里写上 StyleManager.ApplicationTheme = ne ...
- 学习Ajax
1.XHR对象 IE7+.Firefox.Opera.Chrome和Safari都支持原生XMLHttpRequest对象,IE6不支持,只支持ActiveXObject对象,该对象在IE11中已经不 ...
- NSTimer的问题
iOS开发中,涉及到定时的问题,我们通常使用NSTimer来解决,例如下面的代码. SFClass.h #import <Foundation/Foundation.h> @interfa ...
- 验证代理IP
##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...
- Java简单工厂模式以及来自lambda的优化
前言 设计模式是软件工程中一些问题的统一解决方案的模型,它的出现是为了解决一些普遍存在的,却不能被语言特性直接解决的问题,随着软件工程的发展,设计模式也会不断的进行更新,本文介绍的是经典设计模式 ...