17주차(ZigZag Conversion)

4454 단어 알고리즘 설계

17주차(ZigZag Conversion)


디렉토리:
  • 이번 주 문제 완성
  • 주요 과정 사고방식
  • 관련 코드
  • 1. 이번 주에 문제 완성


    이번 주에는 총 2문항, 1문항, 1문항, 1문항을 마쳤다.
    구체적인 완성 문제 및 난이도는 다음과 같다.
    #
    Title
    Difficulty
    6
    ZigZag Conversion
    Medium
    7
    Reverse Integer
    Easy

    제목 내용


    1、ZigZag Conversion  The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
    P   A   H   N
    A P L S I I G
    Y   I   R
    

    And then read line by line: “PAHNAPLSIIGYIR” Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
    제목 대의: 문자열을 주고 Z자형으로 새 순서의 문자열을 되돌려줍니다.
    2、Reverse Integer  Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
    Example:
    Input: "babad"
    
    Output: "bab"
    
    Note: "aba" is also a valid answer.
    

    Example:
    Input: "cbbd"
    
    Output: "bb"
    

    제목 대의: 문자열을 주고 가장 긴 문자열을 되돌려줍니다.

    2. 주요 과정 사고방식


    1、ZigZag Conversion:


    이 문제의 주요 관심사는 바로 ZigZag이 구체적으로 어떤 것인지이다.이 점에서 나는 아래의 이 해석을 참고했다.https://discuss.leetcode.com/topic/22925/if-you-are-confused-with-zigzag-pattern-come-and-see
    /*n=numRows
    Δ=2n-2    1                           2n-1                         4n-3
    Δ=        2                     2n-2  2n                    4n-4   4n-2
    Δ=        3               2n-3        2n+1              4n-5       .
    Δ=        .           .               .               .            .
    Δ=        .       n+2                 .           3n               .
    Δ=        n-1 n+1                     3n-3    3n-1                 5n-5
    Δ=2n-2    n                           3n-2                         5n-4
    */
    

    즉 위의 방문 순서를 실현해야 한다는 것이다.새 문자열에 문자를 한 줄씩 추가하는 for 순환을 사용합니다.줄마다 먼저 i번째 값에 접근하고 그 다음에 증가(numRows-i-1)*2, 2i(1칸막이 증가).문자열 길이보다 큰 값에 접근할 때 이 줄에 대한 접근을 중지합니다.numRows 줄에 접근이 끝날 때까지.

    2、Reverse Integer:


    이 문제는 비교적 간단하다.매번 x에 대한 여분을 구한 후에 목표 정수에 넣으면 된다.하지만 데이터가 넘치는 문제에 주의해야 한다.범위를 초과하면 0으로 돌아갑니다.

    3. 관련 코드


    ZigZag Conversion

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            vector<int> visit(256, -1);
            int start=-1,result=0;
            for(int i=0;iif(visit[s[i]]>start){
                    start=visit[s[i]];
                }
                result=max(result,i-start);
                visit[s[i]]=i; 
    
            }
            return result;
        }
    };

    Reverse Integer

    class Solution {
    public:
        int reverse(int x) {
            long result=0;
            while(x!=0){
                result=x%10+result*10;
                x=x/10;
                if(resultresult>INT_MAX)
                    return 0;
            }
            return result;
        }
    };

    좋은 웹페이지 즐겨찾기