简单有趣#64:几乎递增序列【难度:2级】:
答案1:
namespace myjinxin
{
using System;
public class Kata
{
public bool AlmostIncreasingSequence(int[] s)
{
if (s.Length < 3) return true;
for(int i = 0; i < s.Length - 1; i++)
{
if(s[i + 1] <= s[i])
{
if (i > 0)
{
if (s[i + 1] <= s[i - 1])
{
if (i + 1 == s.Length - 1) return true;
else return false;
}
}
for(int j = i + 1; j < s.Length - 1; j++) if (s[j + 1] <= s[j]) return false;
}
}
return true;
}
}
}
答案2:
namespace myjinxin
{
using System;
using System.Collections.Generic;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence){
Console.WriteLine($"{sequence[0]} {sequence[1]}");
var flag = true;
var index = 0;
for(int i=1;i<sequence.Length;i++)
if(sequence[i]-sequence[i-1] <=0)
if(flag) index = i!=sequence.Length-1?i-1:i;
var arr =new List<int>();
for(int i=0;i<sequence.Length;i++)
if(i!=index) arr.Add(sequence[i]);
for(int i=1;i<arr.Count;i++)
if(arr[i]-arr[i-1] <=0) return false;
return true;
}
}
}
答案3:
namespace myjinxin
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence){
bool answer = true;
List<Int32> numbersList = sequence.ToList();
for (int i = 0; i < numbersList.Count; i++)
{
numbersList.RemoveAt(i);
for (int j = 1; j < numbersList.Count; j++)
{
if (numbersList[j - 1] >= numbersList[j])
{
answer = false;
numbersList = sequence.ToList();
break;
}
else
{
answer = true;
}
}
if (answer == true)
{
break;
}
}
return answer;
}
}
}
答案4:
namespace myjinxin
{
using System;
using System.Linq;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence){
int count = 0;
for (int i = 0; i < sequence.Length - 1; i++)
{
if (i > 1 && sequence[i + 1] <= sequence[i])
{
if (sequence[i + 1] <= sequence[i-1])
{
var lst = sequence.ToList();
lst.RemoveAt(i+1);
i = 0;
sequence = lst.ToArray();
count++;
}
else
{
var lst = sequence.ToList();
lst.RemoveAt(i);
i = 0;
sequence = lst.ToArray();
count++;
}
}
if (sequence[i + 1] <= sequence[i])
{
var lst = sequence.ToList();
lst.RemoveAt(i);
i = -1;
sequence = lst.ToArray();
count++;
}
}
if (count > 1)
return false;
else
return true;
}
}
}
答案5:
namespace myjinxin
{
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence)
{
return Enumerable.Range(0, sequence.Length).Any(i => IsIncreasing(RemoveAtIndex(sequence, i)));
}
IEnumerable<int> RemoveAtIndex(IEnumerable<int> sequence, int index)
{
return sequence.Where((x, i) => i != index);
}
private bool IsIncreasing(IEnumerable<int> sequence)
{
return sequence.Aggregate(
new { Previous = int.MinValue, IsIncreasing = true },
(previous, x) => { return new { Previous = x, IsIncreasing = previous.IsIncreasing && x > previous.Previous }; }).IsIncreasing;
}
}
}
答案6:
namespace myjinxin
{
using System;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence){
bool foundOne = false;
for (int i = -1, j = 0, k = 1; k < sequence.Length; k++)
{
bool deleteCurrent = false;
if (sequence[j] >= sequence[k])
{
if (foundOne)
{
return false;
}
foundOne = true;
if (k > 1 && sequence[i] >= sequence[k])
{
deleteCurrent = true;
}
}
if (!foundOne)
{
i = j;
}
if (!deleteCurrent)
{
j = k;
}
}
return true;
}
}
}
答案7:
namespace myjinxin
{
using System;
using System.Linq;
using System.Collections.Generic;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence){
var seq=sequence.Select(x=>x).ToList();
for(var i=0;i<seq.Count-1;i++) if(seq[i]>=seq[i+1]&&i<seq.Count-2){
seq.RemoveAt(i);
return seq.Where((x,j)=>j==seq.Count-1||x<seq[j+1]).Count()==seq.Count;
}
return true;
}
}
}
答案8:
namespace myjinxin
{
using System;
public class Kata
{
public bool AlmostIncreasingSequence(int[] sequence)
{
var prev = sequence[0];
var firstIncorrectFound = false;
for (int i = 1; i < sequence.Length; i++)
{
var current = sequence[i];
if (current - prev <= 0)
{
if (firstIncorrectFound) return false;
else
{
firstIncorrectFound = true;
if (i == 1) prev = current;
else if (i > 1)
{
var beforePrev = sequence[i - 2];
if (current > beforePrev) prev = current;
else prev = prev;
}
}
}
else
{
prev = current;
}
}
return true;
}
}
}