int[ ] clusterCompression(int[ ] a)
{
List lst=new List<int>();
int cur=a[0];
lst.add(cur);
foreach(int v in a)
{
if(v!=cur){lst.add(v);cur=v;}
}
return lst.ToArry();
}
{
List lst=new List<int>();
int cur=a[0];
lst.add(cur);
foreach(int v in a)
{
if(v!=cur){lst.add(v);cur=v;}
}
return lst.ToArry();
}
要用到这个句子。该怎么办?
/*
if a is and m is and n is Return reason
{3, 9, -3, 1, 0, 3, 10} 0 3 {3, 1, 0, 3} those are the elements x such that m <= x <= n. Furthermore, they are in the same order as they were in the original array.
{18, 0, 15, -5} 5 8 { } there are no elements whose values are in the given range. I.E. there are no elements that are greater than or equal to 5 and less than or equal to 8.
{3, 2, 1, 4, 6, 2, 10} 0 100 {3, 2, 1, 4, 6, 2, 10} all elements in the array are in the given range.
{2, 1, 2, 2, 1, 2} 1 1 {1, 1} only the 1-valued elements are in the given range
{} 5 100 { } no elements are in the given range.
{1, 2, 4, 8, 3} 4 3 null m must be less than or equal to n
{9, 3, 1, -9} 100 120 { } there are no elements in the given range. */
class SquareApp
{
public static void Main()
{
int[] arr = getSubarray(new int[] { 3, 9, -3, 1, 0, 3, 10 },0,3);
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("{0} ", arr[i]);
}
Console.ReadKey();
}
static int[] getSubarray(int[] a, int m, int n)
{ int[] result = new int[5]; //这里的result如何能定义为动态数组,自动改变大小呢? int i = 0,j=0,count=0;
if (m > n || a==null)
{
return null;
}
for (i = 0; i < a.Length; i++)
{
if (a[i] >= m && a[i] <= n)
{
result[j++] = a[i];
}
}
return result;
}
}
using System;
/*
if a is and m is and n is Return reason
{3, 9, -3, 1, 0, 3, 10} 0 3 {3, 1, 0, 3} those are the elements x such that m <= x <= n. Furthermore, they are in the same order as they were in the original array.
{18, 0, 15, -5} 5 8 { } there are no elements whose values are in the given range. I.E. there are no elements that are greater than or equal to 5 and less than or equal to 8.
{3, 2, 1, 4, 6, 2, 10} 0 100 {3, 2, 1, 4, 6, 2, 10} all elements in the array are in the given range.
{2, 1, 2, 2, 1, 2} 1 1 {1, 1} only the 1-valued elements are in the given range
{} 5 100 { } no elements are in the given range.
{1, 2, 4, 8, 3} 4 3 null m must be less than or equal to n
{9, 3, 1, -9} 100 120 { } there are no elements in the given range. */
class SquareApp
{
public static void Main()
{
int[] arr = getSubarray(new int[] { 3, 9, -3, 1, 0, 3, 10 },0,3);
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("{0} ", arr[i]);
}
Console.ReadKey();
}
static int[] getSubarray(int[] a, int m, int n)
{
int[] result = new int[5];//这里的result如何能定义为动态数组,自动改变大小呢? int i = 0,j=0,count=0;
if (m > n || a==null)
{
return null;
}
for (i = 0; i < a.Length; i++)
{
if (a[i] >= m && a[i] <= n)
{
result[j++] = a[i];
}
}
return result;
}
}
using System;
using System.Collections.Generic;
class SquareApp
{
public static void Main()
{
int[] arr = clusterCompression(new int[] { 0, 0, 0, 2, 0, 2, 0, 2, 1, 0 });
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.ReadKey();
}
static int[] clusterCompression(int[] a)
{
List<int> lst = new List<int>();
int cur = a[0];
lst.Add(cur);
foreach (int v in a)
{
if (v != cur) { lst.Add(v); cur = v; }
}
return lst.ToArray();
}
}
Son is invigilating me
Because I'm your father,so I don't konw