zoukankan      html  css  js  c++  java
  • Find the same elements from tow sorted int arrays

            static void Main(string[] args)
            {
                SameCode (SortedCode (
    1,2,3,4,5),SortedCode (3,4,5,7,8,9));
            }

            
    static int[] SortedCode(params int[] arr)
            {
                
    for (int i = 0; i < arr.Length; i++)
                {
                    
    for (int j = i; j < arr.Length; j++)
                    {
                        
    if (arr[j] < arr[i])
                        {
                            
    int tmp = arr[i];
                            arr[i] 
    = arr[j];
                            arr[j] 
    = tmp;
                        }
                    }
                }
                
    return arr;
            }

            
    // no 0 in both integer array 
            static int[] SameCode(int[] arr1, int[] arr2)
            {
                
    int iter1=arr1 .Length -1;
                
    int iter2 = 0;
                
    int index=0;
                
    int[] result = new int[arr1.Length < arr2.Length ? arr1.Length : arr2.Length];

                
    while (iter1>-1&&arr1[iter1] >= arr2[0])
                {
                    
    for (; iter2 < arr2.Length && arr1[iter1] >= arr2[iter2]; iter2++)
                    {
                        
    if (arr1[iter1] == arr2[iter2])
                        {
                            result[index
    ++= arr1[iter1];                     
                        }
                    }
                    iter1
    --;
                    iter2 
    = 0;
                }
                
                
    return result;
            }
  • 相关阅读:
    《冒号课堂》学习笔记 OOP-继承
    《冒号课堂》学习笔记 编程范式汇总
    EF中主表和附表一起提交的话,如果主附表的主键外键已经设定。
    超时时间已到。在操作完成之前超时时间已过或服务器未响应。 解决方法
    wpf下拉框不能多选的原因
    查询中无法构造实体或复杂类型
    wpf新增记录时用多线程的问题
    面向对象
    HTML5入门以及新标签
    前端学习本地存储
  • 原文地址:https://www.cnblogs.com/qixue/p/1681802.html
Copyright © 2011-2022 走看看