shorter concat [reverse longer]
shorter concat [reverse longer]
Description:
Given 2 strings, a and b, return a string of the form: shorter+reverse(longer)+shorter.
In other words, the shortest string has to be put as prefix and as suffix of the reverse of the longest.
Strings a and b may be empty, but not null (In C# strings may also be null. Treat them as if they are empty.).
If a and b have the same length treat a as the longer producing b+reverse(a)+b
我的解法,让人不满意的是if和else的判断太多了。
空字符串和非空字符串,本身是可以直接拼接的
using System;
using System.Linq; class ReverseLonger
{
public static string ShorterReverseLonger(string a, string b)
{
a = a ?? string.Empty;
b = b ?? string.Empty;
string str = string.Empty;
if (a.Equals(string.Empty))
{
if (b.Equals(string.Empty))
{ }
else
{
str = string.Join(string.Empty, b.Reverse());
}
}
else
{
if (b.Equals(string.Empty))
{
str = string.Join(string.Empty, a.Reverse());
}
else
{
if (a.Length < b.Length)
{
str = a + string.Join(string.Empty, b.Reverse()) + a;
}
else
{
str = b + string.Join(string.Empty, a.Reverse()) + b;
}
}
}
return str;
}
}
其他人的解法:
比我好的地方是,通过比较大小,之后用统一的格式来处理
using System.Linq; class ReverseLonger
{
public static string ShorterReverseLonger(string a, string b)
{
if(a == null)
a = string.Empty;
if(b == null)
b = string.Empty; if(a.Length < b.Length)
{
string d = a;
a = b;
b = d;
} return b + (new string(a.Reverse().ToArray())) + b;
}
}
shorter concat [reverse longer]的更多相关文章
- concat,reverse
1.concat 连接,拼接 <script> var arr1=[1,2,3]; var arr2=[4,5,6]; var arr3=[7,8,9]; alert(arr1.conca ...
- 剑指offer练习
1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. public c ...
- 从js的repeat方法谈js字符串与数组的扩展方法
js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- javascript中,数组常用的方法有哪些?
答案: push pop shift unshift join sort concat reverse splice slice indexOf
- 使用mysql5.7新特性(虚拟列)解决使用前通配符性能问题
众所周知,在mysql里的后通配符可以使用索引查找,前通配查询却无法使用到索引,即使是使用到了索引,也是使用了索引全扫描,效率依然不高,再MySQL5.7之前,一直都没有好的办法解决,但是到了MySQ ...
- js原型链的深度理解!
一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object .Function 是 JS 自带的函数对象.下面举例说明 var o1 = ...
- VBA随机地牢生成
无聊啊--于是,我想做一个随机地图. 但是我很懒,不想做. 但是身体很诚实. 这次是直接在Excel中制作的地图,但是,VB的执行效率很慢,我代码的效率也很慢,导致,一旦地图长宽稍大,就会出现好几分钟 ...
- pyhton 核心编程 正则表达式习题
方案一 import re #1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” import re def test1(self): bt = ...
随机推荐
- C#获取“所有用户桌面”的路径
想用C#得到The All Users Desktop(Public\Desktop)的路径. 原来以为很简单,然而 Environment.GetFolderPath(Environment.Spe ...
- Python串行运算、并行运算、多线程、多进程对比实验
转自:http://www.redicecn.com/html/Python/20111223/355.html Python发挥不了多核处理器的性能(据说是受限于GIL,被锁住只能用一个CPU核心, ...
- 处理部分WordPress核心代码或功能,让你的网站更快
处理部分WordPress核心代码或功能,让你的网站更快 http://www.wpdaxue.com/speed-up-wordpress.html
- The architecture of LTE network.
3GPP定义的LTE网络架构结构变得扁平化,无线RNC/BSC 消失,只有eNodeB.控制面使用MME进行处理,用户面使用SGW和PGW进行处理.相比GSM和UMTS,在逻辑接口上定义了S1/X2逻 ...
- Throwing cards away I
Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top a ...
- js 手机端触发事事件、javascript手机端/移动端触发事件
处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 ...
- vi使用教程
Vi有3种模式: 命令模式——命令操作 插入模式——进入vi之后,输入i/a/o,按Esc键,进入命令模式 编辑模式——:set nu, 以回车结束 1.插入 a - 光标后插入 A - 本行末尾插入 ...
- Java中的IO流系统详解
Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...
- 微软职位内部推荐-SDE II-MODC-Beijing
微软近期Open的职位: JOB TITLE: Software Design Engineer IIDEPARTMENT: Microsoft Office Division ChinaIMMEDI ...
- willMoveToParentViewController 与 didMoveToParentViewController
在iOS 5.0以前,我们在一个UIViewController中这样组织相关的UIView 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖 ...