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;
  }
}
时间: 2024-08-29 07:42:09

shorter concat [reverse longer]的相关文章

数组的concat reverse sort方法

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title></title> 6 <script type="text/javascript"> 7 /* 8 concat() 9 连接两个或多个数组,并将数组返回 10 该方法不会对原数组产生影响 11 */ 12 var a=[1,2,3]; 13 v

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

JavaScript 原型 原型链

一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object .Function 是 JS 自带的函数对象.下面举例说明 var o1 = {}; var o2 =new Object(); var o3 = new f1(); function f1(){}; var f2 = function(){}; var f3 = new Function('str','console.log(str)'); console.log(type

数组处理技巧

1.针对数组的逻辑判断 1)写一个函数,功能是判断字符串是否为对称的字符串: 涉及数组处理方法:1.concat();2.reverse(); function judgeStr(str) { var a = str.split(''); var b = a.concat([]).reverse(); var i; var count = 0; for(i = 0;i< Math.ceil(a.length / 2);i++ ) { if(a[i] === b[i]) { count++; };

js类数组转数组的方法(ArrayLike)

1. 什么是类数组ArrayLike 拥有length属性,其它属性(索引)为非负整数(对象中的索引会被当做字符串来处理,这里你可以当做是个非负整数串来理解) 不具有数组所具有的方法 //类数组示例 var a = {'1':'gg','2':'love','4':'meimei',length:5}; Array.prototype.join.call(a,'+');//'+gg+love++meimei' //非类数组示例 var c = {'1':2}; //没有length属性就不是类数

Add Binary字符数字相加,字符串合成

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". Hide Tags Math String class Solution { public: string addBinary(string a, string b) { string longer=a.length()>b.l

从js的repeat方法谈js字符串与数组的扩展方法

js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { return (new Array(n + 1)).join(target); } //版本2:之所以要创建一个带length属性的对象 是因为要调用数据的原型方法,需要指定call的第一个参数为类数组对象 //类数组对象的必要条件是其length属性的值为非负数 function repeat(t

chapter15 正则表达式

核心编程 正则表达式使用的特殊符号和字符 记号         说明                                         举例 literal       匹配字符串的值                             foo re1|re2       匹配字符串1或者2 foo|bar .              匹配任意字符(换行符除外)             b.b ^               匹配字符串的开始                

6kyu Unary function chainer

题目: Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions. chained([a,b,c,d])(input)Should yield the same result as d(c(