leetcode: permutation sequence

用了https://github.com/soulmachine/leetcode上面康托编码的思路:

class Solution {
public:string getPermutation(int n, int k) {string s(n,'0');for(int i=1;i<=n;i++) {s[i-1]+=i;   }string res = "";k = k-1;int base = getFatorial(n-1);for(int i=1;i<n;i++) {int index = k/base;k = k%base;base/=n-i;res+=s[index];s.erase(index,1);}res+=s[0];return res;}int getFatorial(int n) {int res = 1;for(int i=1;i<=n;i++) {res*=i;}return res;}
};

主要看一下string的erase用法:

sequence (1)
string& erase (size_t pos = 0, size_t len = npos);
character (2)
iterator erase (iterator p);
range (3)
iterator erase (iterator first, iterator last);

Erases part of the string, reducing its length:
(1) sequence

Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is string::npos.
Notice that the default argument erases all characters in the string (like member function clear).
returns *this.
(2) character
Erases the character pointed by p.
(3) range
Erases the sequence of characters in the range [first,last).
(2)&(3) return an iterator referring to the character that now occupies the position of the first character erased, or string::end if no such character exists.

所以上面的代码里用的是erase的第1种方式。

还可以改改,用第二种方式:

for(int i=1;i<n;i++) {auto p = next(s.begin(),k/base);k = k%base;base/=n-i;res+=*p;s.erase(p);}

转载于:https://www.cnblogs.com/parapax/p/3637490.html

STL: string:erase相关推荐

  1. 详细解说STL string

    详细解说STL string 详细解说STL string 0 前言: string 的角色 1 string 使用 1.1 充分使用string 操作符 1.2 眼花缭乱的string find 函 ...

  2. (转)详细解说STL string

    详细解说STL string 详细解说STL string 0 前言: string 的角色 1 string 使用 1.1 充分使用string 操作符 1.2 眼花缭乱的string find 函 ...

  3. STL中erase()的用法

    erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下: 1.有两个参数,且参数类型都是size_t型: string& erase ( s ...

  4. C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)

    1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...

  5. C++中string erase函数的使用

    erase函数的原型如下: (1)string& erase ( size_t pos = 0, size_t n = npos ); (2)iterator erase ( iterator ...

  6. stl string常用函数

    原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...

  7. C++ STL string 简单使用

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <string> ...

  8. stl string 函数_使用C ++ STL中的string :: append()函数将文本追加到字符串

    stl string 函数 append() is a library function of <string> header, it is used to append the extr ...

  9. c++ string类_C++|细说STL string类概貌及底层细节

    C语言中的字符串称为C风格字符串,是一个以'0'结尾的字符数组,string.h库只提供了有限.不甚安全的字符串操作函数.char str[]只能定义编译期确定大小的字符串,而保存在堆内存的动态字符数 ...

最新文章

  1. Quartz教程二:API,Job和Trigger
  2. Alpha冲刺(8/10)
  3. MySQL 1093 You can't specify target table for update in FROM c 的解决办法
  4. Qt工作笔记-在ListWidget中单线程检索数据
  5. Linux(debian7)操作基础(十四)之文本搜索命令grep使用方法
  6. html 行自动对齐,html – 行元素不会对齐
  7. 机器学习Scikit-Learn基本操作实战
  8. [网络安全自学篇] 二十五.Web安全学习路线及木马、病毒和防御初探
  9. JAVA 正则表达式(大全)
  10. office visio连接线控制方法
  11. 摄动法在计算机中的应用,渐近分析与摄动方法的计算机辅助求解.pdf
  12. 7 EXCEL常用快捷键
  13. 离散分布——泊松分布、指数分布
  14. JS实现鼠标点击出现文字特效
  15. rxjava背压_RxJava(九):背压
  16. First-chance exception in KERNEL32.DLL 0xE06D7363 Microsoft C++ Exception
  17. 在平板电脑与移动3G大爆炸的时代 昔日霸主微软的反击
  18. h5单页,微信浏览器唤醒APP
  19. 【Leetcode_SQL】1179.重新格式化部门表
  20. 联想u盘启动linux,联想thinkpad e335台式机bios设置u盘启动的方法

热门文章

  1. Linux配置nginx打开报404,Linux下Nginx配置404页面的方法
  2. python去除图像光照不均匀_【转载】光照不均匀图像分割技巧
  3. spyder替换_Spyder 快捷键大全
  4. win7 nginx php 环境,win7下docker环境搭建nginx+php-fpm+easyswoole+lavarel开发环境
  5. 获取客户端ip_获取客户端访问真实IP
  6. c c python的区别_python和c语言的主要区别总结
  7. cuda 图片拆分_急需,PDF怎么拆分啊?
  8. java编译sql存过_SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨
  9. Java功能模块代码源码_Windows计算机功能Java源码
  10. tk域名管理后台_不知道“域名怎么解析”的伙伴,可以来看看