• Leetcode 392(会) 20注意细节 * 12 151 6 71


    12. Integer to Roman

    1.

    1. class Solution {
    2. public:
    3. string intToRoman(int num) {
    4. string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    5. string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    6. string hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    7. string thousands[] = {"", "M", "MM", "MMM"};
    8. return thousands[num/1000] + hundreds[num%1000/100] + tens[num%100/10] + ones[num%10];
    9. }
    10. };

    2.

    1. class Solution {
    2. public:
    3. string intToRoman(int num) {
    4. int normal[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    5. string roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    6. string res;
    7. for(int i=0; i<13; i++){
    8. while(num >= normal[i]){
    9. num-=normal[i];
    10. res.append(roman[i]);
    11. }
    12. }
    13. return res;
    14. }
    15. };

    151. Reverse Words in a String

    1. class Solution {
    2. private:
    3. void reverse(string& s, int start, int end){
    4. while(start <= end){
    5. swap(s[start], s[end]);
    6. start++;
    7. end--;
    8. }
    9. }
    10. public:
    11. string reverseWords(string s) {
    12. //delete all the extra space in the old string
    13. string newString;
    14. for(int i=0; isize(); i++){
    15. if((i==0 && s[i]==' ') || (i!=0 && s[i]==s[i-1] && s[i] == ' ')){
    16. continue;
    17. }else{
    18. newString.push_back(s[i]);
    19. }
    20. }
    21. while(newString[newString.size()-1] == ' '){
    22. newString.resize(newString.size()-1);
    23. }
    24. reverse(newString, 0, newString.size()-1);
    25. int start = 0;
    26. for(int i=0; i<=newString.size(); i++){
    27. if(i==newString.size() || newString[i] == ' '){
    28. reverse(newString, start, i-1);
    29. start = i+1;
    30. }
    31. }
    32. return newString;
    33. }
    34. };

    1.这道题首先要把多余的空格去掉

    2.出现的错误:

    1)在写reverse函数的时候,要写string& s, 不要把&忘掉

    2)每个单词进行翻转的时候,不要忘了判断最后一个词,因为最后一个词的后面没有空格,所以要加一个条件i==newString.size()

    6. Zigzag Conversion

    1. class Solution {
    2. public:
    3. string convert(string s, int numRows) {
    4. if(numRows == 1) return s;
    5. int dif = 2*(numRows-1);
    6. string res;
    7. int n = s.size();
    8. for(int currRow=0; currRow < numRows; currRow++){
    9. int index = currRow;
    10. while(index < n){
    11. res += s[index];
    12. if(currRow != 0 && currRow != numRows-1){
    13. int charBewtween = dif - 2*currRow;
    14. int secondIndex = index + charBewtween;
    15. if(secondIndex < n)
    16. res += s[secondIndex];
    17. }
    18. index += dif;
    19. }
    20. }
    21. return res;
    22. }
    23. };

    有基本思路:把每一组‘z'看成一个组,依次读取(像‘A-H’)

    问题:

    1.除了每组第一个数(如A, I),其余每一行都会有两个这一组的数(如B和H)

    要确定好函数关系

    392. Is Subsequence

    1. class Solution {
    2. public:
    3. bool isSubsequence(string s, string t) {
    4. int i=0, j=0;
    5. while(isize() && jsize()){
    6. if(s[i] == t[j]){
    7. i++;
    8. }
    9. j++;
    10. }
    11. return i == s.size();
    12. }
    13. };

    20. Valid Parentheses

    1. class Solution {
    2. private:
    3. char leftOf(char ch){
    4. if(ch == ')') return '(';
    5. else if(ch == ']') return '[';
    6. else return '{';
    7. }
    8. public:
    9. bool isValid(string s) {
    10. stack<char> stk;
    11. for(char ch:s){
    12. if(ch == '(' || ch=='[' || ch =='{'){
    13. stk.push(ch);
    14. }else{
    15. if(!stk.empty() && leftOf(ch) == stk.top()){
    16. stk.pop();
    17. }else{
    18. return false;
    19. }
    20. }
    21. }
    22. return stk.empty();
    23. }
    24. };

    71. Simplify Path

    1. class Solution {
    2. public:
    3. string simplifyPath(string path) {
    4. stack stk;
    5. for(int i=0; isize(); i++){
    6. if(path[i] == '/')
    7. continue;
    8. string tmp;
    9. while(isize() && path[i] != '/'){
    10. tmp += path[i];
    11. i++;
    12. }
    13. if(tmp == ".")
    14. continue;
    15. else if(tmp == ".."){
    16. if(!stk.empty()){
    17. stk.pop();
    18. }
    19. }
    20. else stk.push(tmp);
    21. }
    22. string newPath;
    23. while(!stk.empty()){
    24. newPath = '/' + stk.top() + newPath;
    25. stk.pop();
    26. }
    27. if(newPath == "") return "/";
    28. return newPath;
    29. }
    30. };

    因为这里涉及到“..”, 所以正确的是stk存储的是string

    c++要学会如何分割这些string

    另一种方法,直接使用getline(a, b, c)

    将a存储到b中,直到找到c

    这里的a要是isstream,所以要加一步stringstream ss(path)

  • 相关阅读:
    01 【Vue简介 初识Vue 模板语法和数据绑定】
    【CSS】css弹性布局、CSS hack_08
    系统架构设计之微内核架构(Microkernel Architecture)
    Chisel3 入门 (1)
    HiveServer2负载均衡
    【ES实战】ES主副分片数据不一致分析
    springboot+微信小程序的点餐系统(开题报告+论文+答辩PPT+源码)
    机组 指令系统
    RabbitMQ笔记(交换机,发布确认,延时队列,死信队列,整合SpringBoot)
    【Leetcode】70. 爬楼梯
  • 原文地址:https://blog.csdn.net/Zoeyii/article/details/132865706