Leetcode_6_ZigZag Conversion
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41408021
看完这篇文章,你可能会学到到知识如下:
(1)对于给定的算法题,如何正确对其进行分析,而不是通过穷举的方式来得到答案。
(2)当解题逻辑非常清晰时,才开始编码,否则你很难编写出正确的代码。
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"
.
思路:
(1)通过字符串在每一行出现的顺序,可以其分两个部分:第一行和最后一行,中间行。
(2)对于第一行和最后一行,由上图可知,具有相同的对应关系。
(3)对于中间行,前一个下标的值和后一个下标的值需要根据这个下标是该行中的奇数列还是偶数列来计算。
(4)对已第一行和最后一行,通过规律发现,其对应关系为:start + 2 * 总行数 - 2 ,其中start为该行起始字符在数组的位置。例如第一行开始的start = 0,则后续对
应位置 为0 + 2 * 5 - 2 = 8,初始start为各行的起始值。同理,对于最后一行,当start = 4时,则后续对应位置为4+ 2 * 5 - 2 = 12,依此类推。
(5)对于中间行,当列数为奇数时,其对应位置关系为:start + 2 * (总行数 - 当前行 - 1);当列数为偶数时,其对应关系为:start + 2 * 当前行,其中当前列从0开始
。例如,对于第三行的起始start = 3,其后续位置所在列为1,是奇数列,则后续位置为 3 + 2 * (5 - 3 -1)= 5 ,此时start = 5,则其后续位置所在列数为4,是偶数
列,则后续位置为5 + 2 * 3= 11。
这道题有点类似数学中的归纳和总结相关的题目,其考的不是编程的技巧,而是通过发现规律,并对其进行分析,从而达到解题的目的。总体上感觉,要想算法比较好,扎实的数
学功底很有必要,所以,在业余时间,多花时间学数学也是很有必要的。这里推荐几本和计算机相关的数学书籍:《程序员的数学》、《具体数学》、《质数的孤独》。希望对你
有所帮助,也希望大家分享各自的学习心得和体会。
解题代码如下:
public static String convert(String s, int nRows) { if (nRows <= 0) return ""; if (nRows == 1 || nRows >= s.length()) return s; StringBuffer buffer = new StringBuffer(); int len = s.length(); for (int i = 0; i < len && i < nRows; ++i) { // 在字符串中的变化的位置 int start = i; buffer.append(s.charAt(start)); for (int j = 1; start < len; ++j) { // 第一行和最后一行 if (i == 0 || i == nRows - 1) { start = start + 2 * nRows - 2; } else { // 对于中间行,在字符串中位置为基数时 if (j % 2 == 1) { start = start + 2 * (nRows - i - 1); } else { // 位置为偶数时 start = start + 2 * i; } } // 边界判断 if (start < len) buffer.append(s.charAt(start)); } } return buffer.toString(); }
Leetcode_6_ZigZag Conversion的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- View and Data API Tips : Conversion between DbId and node
By Daniel Du In View and Data client side API, The assets in the Autodesk Viewer have an object tree ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- Conversion Operators in OpenCascade
Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法
最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt
今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
随机推荐
- 服务器&阵列卡LSI SAS223&组raid 10
组raid10 如配置: raid LSI SAS2236 双E5-2450L 96G 4*1TB 要求: 至少4块HDD 将接上Raid card的机器开机,根据提示按组合键进入Raid配置界面 ...
- python笔记三(list、tuple)
一.list list的增删改查 #增, classmates.append("nadech") #在末尾追加一个元素 classmates.insert(1,"agui ...
- 利用Bioperl的SeqIO模块解析fastq文件
测序数据中经常会接触到fastq格式的文件,比如说拿到fastq格式的原始数据后希望查看测序碱基的质量并去除低质量碱基.一般而言大家都是用现有的工具,比如说fastqc这个Java写的小程序,确实很好 ...
- jQuery – AJAX load() 方法
jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...
- Docker常见仓库CentOS
CentOS 基本信息 CentOS 是流行的 Linux 发行版,其软件包大多跟 RedHat 系列保持一致. 该仓库提供了 CentOS 从 5 ~ 7 各个版本的镜像. 使用方法 默认会启动一个 ...
- ngx.ctx
https://github.com/openresty/lua-nginx-module#ngxctx 要点 生命周期和请求一致 每个请求的ngx.ctx是相互独立的,包括ngx.location. ...
- android MultiDex multidex原理原理下遇见的N个深坑(二)
android MultiDex 原理下遇见的N个深坑(二) 这是在一个论坛看到的问题,其实你不知道MultiDex到底有多坑. 不了解的可以先看上篇文章:android MultiDex multi ...
- Dynamics CRM 安装CRM程序系统检查界面报未将对象引用设置到对象的实例的解决方法
今天在安装CRM的时候,在系统检查阶段遇到了如下的错误,咋看之下直接是懵逼的 但不要着急,界面上有两个按钮,一个是详细信息,一个是帮助,详细信息不用看了就那一行字也看不出什么,咱们点下帮助看看,定位到 ...
- android studio 转为eclipse快捷键后还存在的问题汇总
提取局部变量:Ctrl+Alt+V 提取全局变量:Ctrl+Alt+F 提取方法:Shit+Alt+M 使用android studio 出现红色下划线代表有错误产生,eclipse中的Ctrl+1( ...
- 使用java操作HDFS
新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...