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