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;
            }
  • 相关阅读:
    Use 'mysqld --thread_stack=#' to specify a bigger stack.
    Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;
    interface interface com.alibaba.dubbo.common.compiler.Compiler, class line com.alibaba.dubbo.common.compiler.support.JavassistCompiler) in
    javax.servlet.http.HttpServlet was not found on the Java Build Path
    js_jquery单机事件不起作用
    JavaScript 执行环境及作用域
    闭包的使用场景
    YUI css reset
    比较经典的数组去重和数组排序
    css 响应式布局
  • 原文地址:https://www.cnblogs.com/qixue/p/1681802.html
Copyright © 2011-2022 走看看