public class ZigzagConvert {
public static String convert(String s, int nRows) {
int len = s.length();
if (len == 0 || nRows < 2) return s; String ret = "";//赋值可以定义ret=null;但是拼接字符串的时候不行,只能定义""
int lag = 2*nRows - 2; //循环周期
for (int i = 0; i < nRows; i++) {
for (int j = i; j < len; j += lag)
{
ret += s.charAt(j); //非首行和末行时还要加一个,中间奇列的字符序号相当于是偶数列的下面的字符个数加上偶数列字符序号
if (i > 0 && i < nRows-1) {
int t = j + 2*(nRows-1-i);
if (t < len)
{
ret += s.charAt(t);
}
}
}
}
return ret;
}
public static void main(String[] args)
{
String s = "abcdefg";
String s1 = convert(s, 3);
System.out.println(s1); }
}

ZigzagConvert的更多相关文章

  1. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

随机推荐

  1. Online Judge(字符串-格式)

    Online Judge Problem Description Ignatius is building an Online Judge, now he has worked out all the ...

  2. 160822、关于javascrip ==(等号) 和===(恒等)判断

    说明 在JavaScript中,下面的值被当做假(false),除了下面列出的值,都被当做真(true): false null undefined 空字符串 数字 0 NaN //属性是代表非数字值 ...

  3. UESTC 482 Charitable Exchange(优先队列+bfs)

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  4. VS中没有为此解决方案配置选中要生成的项目

    菜单->生成->配置管理器->给要生成的项目打钩

  5. spring和hibernate整合时设置自动生成数据库的表

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFa ...

  6. 怎样解决Please ensure that adb is correctly located at......

    昨天下午搭建了Android开发环境,但是天公不作美--执行新建的Android项目总是提演示样例如以下问题: [2014-10-30 15:41:47 - ] The connection to a ...

  7. 通过存储过程运行通过DBLINK的查询语句失败-单个语句成功--ORA-00604

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/q947817003/article/details/24419459 客户遇到个问题,描写叙述例如以 ...

  8. 0102-使用 API 网关构建微服务

    一.移动客户端如何访问这些服务 1.1.客户端与微服务直接通信[很少使用] 从理论上讲,客户端可以直接向每个微服务发送请求.每个微服务都有一个公开的端点(https ://.api.company.n ...

  9. STL之空间配置器

    在前面很多随笔里都有提到new对象是先分配内存然后初始化对象,主要是对operator new和placement new的使用 在SGI STL中内存的分配和初始化是分开的,分配内存是使用类模板,模 ...

  10. 简单springmvc在Eclipse的Tomcat上部署404error,直接在Tomcat上部署可以访问

    在Eclipse工程下的.setting文件夹的org.eclipse.wst.common.component这个文件,如下则可以访问: <?xml version="1.0&quo ...