LeetCode #6 简单题(小模拟)

题目: 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。然后横向输出。LeetCode上有几个样例可以看看。

题解:模拟一下就好了- -,对原字符串s排列完后,横向每个添加到ans中去就行了

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) return s;
        int len = (int)s.size(), gridSize = numRows * 2 - 2;
        string ans = s;
        int pos = 0;
        for (int i = 0; i < numRows; ++i){
            int index = i;
            if (index == 0 || index == numRows - 1){
                while (index < len){
                    ans[pos++] = s[index];
                    index = index + gridSize;
                }
            }
            else{
                bool bottom = true;
                while (index < len) {
                    ans[pos++] = s[index];
                    if (bottom){
                        index = index + (numRows - i - 1) * 2;
                    }
                    else{
                        index = index + i * 2;
                    }
                    bottom = !bottom;
                }
            }
        }
        return ans;
    }
};

原文地址:https://www.cnblogs.com/error408/p/11610499.html

时间: 2024-08-28 21:36:07

LeetCode #6 简单题(小模拟)的相关文章

LeetCode #1 简单题

题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 题解:简单题,没啥可说的,不想 n2 复杂度搞,map存一下logn吧先 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 std::map<int, int> numMap; 5 vector<int>

每日一题之LeetCode 栈简单题集合496,682,232,225,155,844,20

496 下一个最大的元素方法1 :自己瞎写的 没有考虑到栈的特性class Solution:def nextGreaterElement(self, nums1, nums2): L=[] for i in nums1: k=nums2.index(i) lenth2=len(nums2) if k==lenth2-1: L.append(-1) for j in range(k+1,lenth2): if nums2[j]>i: L.append(nums2[j]) break if j==

leetCode #2 简单题

题目:给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 题解:就指向两个链表的头的指针依次向后遍历.算和的余数,记录一下进位.如果L1先遍历到末尾的话,就把L1最后一个指针指向L2当前的next,即把L2剩余的部分接在L1的结尾.这样可以节省不少开辟新Node的空间,然后继续用进位和L1当前N

LeetCode #3 简单题

题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 题解: i j 分别记录目标字符串的左右边界.对当前字符 x,如果前面出现过,则更新左边界为上次出现位置的下一个,然后更新当前字符 x 的位置,遍历过程中记录一下 j - i + 1的最大值就好了. class Solution { public: int lengthOfLongestSubstring(string s) { int ans = 0; int len = s.size(); std::map<int,

LeetCode #5 简单题

题目:求最长回文子串 题解:manacher算法,百度一堆讲解- -,我也不说了,想知道啥的自己百度去吧 class Solution { public: string longestPalindrome(string s) { string manaStr = "$#"; for (int i=0;i<s.size();i++){ manaStr += s[i]; manaStr += '#'; } vector<int> rd(manaStr.size(), 0)

LeetCode #7 简单题(反转整数)

题目:翻转整数  123 -> 321,-123 -> -321 题解: long long 存一下好了,注意溢出返回0就行了 class Solution { public: int reverse(int x) { long long orix = x; long long rev = 0; bool isLess0 = orix < 0; orix = orix < 0 ? -1 * orix : orix; while(orix != 0){ rev = rev * 10

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

Leetcode动态规划【简单题】

目录 Leetcode动态规划[简单题] 53. 最大子序和 题目描述 思路分析 复杂度分析 70.爬楼梯 题目描述 思路分析 复杂度分析 121.买卖股票的最佳时机 题目描述 思路分析 复杂度分析 303.区域和检索-数组不可变 题目描述 思路分析 复杂度分析 Leetcode动态规划[简单题] 动态规划(Dynamic programming,简称DP),是一种把原问题分解为相对简单的子问题的方式求解复杂问题的方法.动态规划相较于递归,拥有更少的计算量. 53. 最大子序和 题目描述 给定一

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a