if (input[i] == ' ' && indics[1] == 0) indics[1] = -1;
else if ( input[i] != ' ' && indics[1] == -1 ) indics[1] = i;
else if (input[i] == ' ' && indics[1] > 0) break; }
int firstWordEnd = 0;
if (indics[1] > 0) {
// copy second word
for (firstWordEnd = indics[1]; firstWordEnd < i; firstWordEnd++) result[resultIter++] = input[firstWordEnd];
// copy spaces
for (firstWordEnd = indics[1] - 1; firstWordEnd > 0; firstWordEnd--) {
if (input[firstWordEnd] != ' ') break;
result[resultIter++] = input[firstWordEnd]; } } else
firstWordEnd = input.Length - 1;
for (int j = indics[0]; j <= firstWordEnd; j++) result[resultIter++] = input[j];
while (i < input.Length && input[i] == ' ') {
result[resultIter++] = input[i]; i++; }
if ( i < input.Length )
ReverseWordPairImp(result, resultIter, input, i); }
Write a function to zero all duplicate values in an integer array. How would you test it?
Approach 1:
Sort the array, and remove all duplicate values.
Approach 2:
Remove dups while sorting, (as highlighted in below code):
static void swap(int[] list, int x, int y) {
if (list[x] == list[y]) list[y] = 0;
int temp;
temp = list[x];
list[x] = list[y]; list[y] = temp; }
static int choose_pivot(int i, int j) {
return((i+j) /2); }
static void quicksort(int[] list, int m, int n) {
int key,i,j,k;
if( m < n) { k = choose_pivot(m,n); swap(list, m, k); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(list, i, j); } // swap two elements swap(list, m, j); // recursively sort the lesser list quicksort(list,m,j-1); quicksort(list,j+1,n); } }
编程去掉两个string list中重复的部分。
///
/// Remove all characters occupied in s2 from s1 ///
/// /// ///
/// RemoveCharactersOccursInS2FromS1(\/// Th a book /// 1. private static string RemoveCharactersOccursInS2FromS1(string s1, string s2)
2. { 3. if (string.IsNullOrEmpty(s1)) 4. throw new ArgumentNullException(\); 5. if (string.IsNullOrEmpty(s2)) 6. throw new ArgumentNullException(\); 7. 8. int[] occurrency = new int[53]; 9. foreach (char c in s2) 10. { 11. if (c >= 'a' && c <= 'z') 12. occurrency[c - 'a']++; 13. else if (c >= 'A' && c <= 'Z') 14. occurrency[c - 'A' + 26]++; 15. else if (char.IsWhiteSpace(c)) 16. occurrency[occurrency.Length - 1]++; 17. else 18. throw new ArgumentException(\'z' and characters between 'A' and 'Z' are supported.\); 19. } 20. 21. char[] result = new char[s1.Length]; 22. int resultIndex = 0; 23. foreach (char c in s1) 24. { 25. int index = -1; 26. if (c >= 'a' && c <= 'z') 27. index = c - 'a'; 28. else if (c >= 'A' && c <= 'Z') 29. index = c - 'A' + 26; 30. else if (char.IsWhiteSpace(c)) 31. index = occurrency.Length - 1; 32. else 33. throw new ArgumentException(\'z' and characters between 'A' and 'Z' are supported.\); 34. 35. if (occurrency[index] == 0) 36. { 37. result[resultIndex++] = c; 38. } 39. } 40. 41. return new string(result, 0, resultIndex); 42. }
现有一单链表,输出从尾端开始的第5个数值,并写出相应的测试用例
两个指针遍历,一个提前走5步,一个走一步,步步更新,走5步碰到结尾了以后,第二个指针指向倒数第5个数值。
What is reflection? Can you tell me some use scenarios of reflection, and how do you use reflection?
反射提供了封装程序集、模块和类型的对象(Type 类型)。可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。有关更多信息,请参见属性。 反射在下列情况下很有用:
1. 需要访问程序元数据的属性。请参见主题使用反射访问属性。 2. 检查和实例化程序集中的类型。
3. 在运行时构建新类型。使用 System.Reflection.Emit 中的类。
4. 执行后期绑定,访问在运行时创建的类型的方法。请参见主题动态加载和使用类型。
What is delegate? Please provide some use scenarios of delegate. Is delegate the same as event, what’s the different?
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。
与委托的签名(由返回类型和参数组成)匹配的任何方法都可以分配给该委托。这样就可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,便可以分配自己的委托方法。 将方法作为参数进行引用的能力使委托成为定义回调方法的理想选择。例如,可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。 委托概述
1. 委托类似于 C++ 函数指针,但它是类型安全的。
2. 委托允许将方法作为参数进行传递。委托可用于定义回调方法。委托可以链接在一起; 3. 例如,可以对一个事件调用多个方法。
4. 方法不需要与委托签名精确匹配。有关更多信息,请参见协变和逆变。
5. C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。
What’s box and unbox? What is the performance impact?
装箱就是隐式的将一个值型转换为引用型对象。比如: int i=0;
Syste.Object obj=i;
这个过程就是装箱!就是将i装箱!
拆箱就是将一个引用型对象转换成任意值型!比如: int i=0;
System.Object obj=i; int j=(int)obj;
这个过程前2句是将i装箱,后一句是将obj拆箱!
装箱和拆箱需要在堆上分配空间,因此效率上会比较慢。
C#里值传递与引用传递有什么区别?
所谓的值传递,是指将要传递的值作为一个副本传递 引用传递,传递的是引用对象的内存地址
相关推荐: