Leetcode two_sum (Easy)/java

Source: Internet
Author: User

Brush title Background

Worry about not finding a job.

See think in Java slightly dull, the video of the Wisdom podcast is too obvious.

So the practice algorithm and Java brush problem.

Say less nonsense.

Problem:

Java rookie one, with their own compiler to easily check syntax errors.

So first I wrote a main function for testing.

1  Public Static voidMain (string[] args)2     {3Scanner input=NewScanner (system.in);4String s=input.nextline ();5String[] S1=s.split (",");6         intlen=s1.length;7         int[] nums=New int[Len];8         intI=0;9          for(String str:s1)TenSystem.out.print (str+ ""); OneSystem.out.println (' \ n '); A          for(i=0;i<s1.length;i++) -         { -Integer it=integer.valueof (S1[i]); the             inttemp=It.intvalue (); -nums[i]=temp; -         } -          for(i=0;i<len;i++) +System.out.print (nums[i]+ ""); -System.out.println (' \ n '); +         inttarget=input.nextint (); A System.out.println (target); at  -         int[] res=New int[2]; -res=twosum (nums,target); -System.out.print (res[0]+ "-" +res[1]); -}
View Code

Mentioned here: A little difference between Java and C + +, C + + cannot directly return an array, only the pointer to an array, making the life cycle of the control array difficult and prone to memory leaks. In Java, you return an array directly, without being responsible for it, and when you are done, the garbage collector cleans up. (from Think in Java)

First question, how do I read an array with commas?

Reference http://blog.csdn.net/DeMonliuhui/article/details/70157651

Second question, how do I convert an int array into a function?

Reference http://www.blogjava.net/ljc-java/archive/2011/02/28/345344.html

About function functions. Two-tier for loop traversal. Add the i!=j limit because an element is required to not be reused.

1  Public Static int[] Twosum (int[] Nums,inttarget) {2         int[] r=New int[2];3         intI=0,j=0;4         Booleanflag=false;5          for(i=0;i<nums.length;i++)6         {7              for(j=0;j<nums.length;j++)8             {9                 if(nums[i]+nums[j]==target&&i!=j)Ten                 { Oner[0]=i; Ar[1]=J; -flag=true; -                      Break; the                 } -             } -             if(flag) -                  Break; +         } -  +         returnR; A}
View Code

Leetcode two_sum (Easy)/java

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.