"Leetcode-array" 1, Sum

Source: Internet
Author: User

1 Preface

The reason for the beginning of the brush Leetcode algorithm problem, one is to face the autumn strokes, the 2nd is because the ability to improve their programming, in the blog to brush 5 or so on the algorithm of the problem, adhere to two months, hope Bo friends to supervise.

This series intends to use C # and Java programming, why with two languages, because of the spring strokes, found that the company is in the C # is too few, only learning C # at the same time, the Java Foundation also incidentally practice, C # and Java syntax is similar, not a bit too much disaster.

Don't say much nonsense, start with the question.

2 Topics and Solutions 2.1 topics

Given an array of integers and a target value, find the two numbers in the array and the target values.

You can assume that each input corresponds to only one answer, and that the same element cannot be reused.

Example:

Given nums = [279 because nums[0] + nums[12  79 so return [01]
2.2 C # Solution 2.2.1 Violence resolution

The problem is quite simple, and violence can be solved.

 Public classSolution { Public int[] Twosum (int[] Nums,inttarget) {        int[] res =New int[2];  for(inti =0; I < Nums. Length-1; i++){             for(intj = i +1; J < Nums. Length; J + + ){                if(Nums[i] + nums[j] = =target) {res[0] =i; res[1] =J;  Break; }            }        }        returnRes; }}

Brute force solution, two times for loop, traverse all possible, this is also easy to think of method, Time complexity O (n^2), Space complexity O (1);

2.2.2 Using a dictionary
 Public classSolution { Public int[] Twosum (int[] Nums,inttarget) {Dictionary<int,int> symbol =Newdictionary<int,int>(); int[] res =New int[2]; //data stored in a dictionary             for(intI=0; I<nums. length;i++) {symbol.              ADD (Nums[i], i); }                 for(intj=0; J<nums. length;j++)              {                  intt = target-Nums[j]; if(symbol. ContainsKey (t) && symbol[t]!=j) {res[0] =J; res[1] =Symbol[t];  Break; }              }              returnRes; }      }

Time complexity t (n) = O (n)

2.3 Java Solutions 2.3.1 Violence resolution
classSolution { Public int[] Twosum (int[] Nums,inttarget) {                int[] res =New int[2];  for(inti =0; I < nums.length-1; i++){             for(intj = i +1; J < Nums.length; J + + ){                if(Nums[i] + nums[j] = =target) {res[0] =i; res[1] =J;  Break; }            }        }        returnRes; }}
2.3.2 using HashMap
classSolution { Public int[] Twosum (int[] Nums,inttarget) {Map<integer, integer> map =NewHashmap<>();
for(inti =0; i < nums.length; i++) {map.put (nums[i], i); }
for(inti =0; i < nums.length; i++) { intcomplement = target-Nums[i]; if(Map.containskey (complement) && map.Get(complement)! =i) {return New int[] {i, map.Get(complement)}; } } Throw NewIllegalArgumentException ("No and Sum solution"); }}

Using a hash table, each time the target is stored minus the difference (key) of the current value, the value is subscript (value), and when this value is encountered, the values that meet the requirement are found. Time complexity O (n), spatial complexity O (n);

3 Summary 3.1 Arrays in C # and Java represent different 3.1.1 C # array representations

Declaration and assignment of one-dimensional arrays:

    classProgram {Static voidMain (string[] args) {            //one or one-D arrays//1.1 declaring and initializing a one-dimensional array            int[] Array1 =New int[Ten]; //1.2 Assigning a one-dimensional array//Mode 1 can create and initialize an array, such as: ("" can also not omit the size of the array, but as long as the size of the array is written, the subsequent assignment will satisfy the size of the array)            int[] Array2 =New int[] {1,2,3,4,5 }; int[] Array3 =New int[Ten] {1,2,3,4,5,6,7,8,9,Ten }; //Mode 2 assigns a value to a single array element by using an index number, such as:array1[0] =1; //Mode 3 assigns a value to the array while declaring the array, such as:            Double[] balance = {2340.0,4523.69,3421.0 }; Console.WriteLine (array1.        length);//display array lengths Console.readkey (); }    }

Declaration and assignment of two-dimensional arrays:

        Static voidMain (string[] args) {//declaring and initializing and assigning a two-D array            int[,] a =New int[3,4] {                                         {0,1,2,3} ,/*initialize row with index number 0*/                                         {4,5,6,7} ,/*initialize row with index number 1*/                                         {8,9,Ten, One}/*initialize row with index number 2*/                                      };            Console.WriteLine (A.length);        Console.readkey (); }
3.1.2 Array representations in Java

One-dimensional arrays are the same as C #

Declaration and assignment of two-dimensional arrays:

        Static void Main (string[] args)        {            new string[3][4];            System.out.println (total.length);        }

Java two-dimensional array and C # two-dimensional array to indicate the gap is quite large, pay attention to use!!!

The "[]" in a one-dimensional array in the 1:java can be placed behind a variable or in the front, while the C # is placed before it. A two-dimensional array in Java is placed behind a variable with "[] []", whereas a two-dimensional array in C # is placed in front of the variable with "[,]". (in order to facilitate the memory is not mixed, the same C # placed before the variable, Java placed after the variable)

The difference between 2:c# means that the length of the array is the first letter to be written, and the length of the array in Java is lowercase.

Difference 3: In. Net imitate Java's process abandoned HashMap, so later go to interview. Net when someone asks you the difference between Hashtable and HashMap, please tell him that there is no hashmap in c#.net.

"Leetcode-array" 1, Sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.