Leetcode537 Complex number multiplication Java implementation

Source: Internet
Author: User

Did not touch the string in Java to look for characters or slices of the operation, so just get started feeling a bit uncomfortable. Then look at the API to confirm the two ideas, Method 1 is to use String.IndexOf to find the position of the string + and I, After using the String.substring method to get the two sub-string after slicing, then using integer.valueof, the real and imaginary parts of the two numbers, and then multiply, and finally through the String.Format method to build a string. Method 2 uses String.Split (regex) to separate the characters.

Method 1 was implemented first:

 Public classcomplexnumbermultiplication {//Method 1: With String.IndexOf; 2: With String.Split (regex). Then use Integer.valueof ().      Public Static int[] GetNumv1 (String t) {intPlusindex = T.indexof ("+"); intIINDEX = T.indexof ("I"); int[] x =New int[2]; x[0] = integer.valueof (t.substring (0, Plusindex)); x[1] = integer.valueof (t.substring (plusindex+1, IINDEX)); returnx; }     Publicstring Complexnumbermultiply (String A, string b) {int[] xa =GetNumv1 (a); int[] XB =GetNumv1 (b); int[] result =New int[2]; result[0]=xa[0]*xb[0]-xa[1]*xb[1]; result[1]=xa[0]*xb[1]+xa[1]*xb[0]; String R= String.Format ("%d+%di", result[0],result[1]); returnR; }}

Use the split method of regular Expressions:

     Public Static int[] getNumv2 (String t) {string[] T1= T.split ("\\+|i");//Java first resolves this regex string to "\+|i" (automatic parsing of strings), and then this "\+|i" is represented as a regex "+|i"        int[] x =New int[2]; x[0] = integer.valueof (t1[0]); x[1] = integer.valueof (t1[1]); returnx; }     Publicstring Complexnumbermultiply (String A, string b) {int[] xa =getNumv2 (a); int[] XB =getNumv2 (b); int[] result =New int[2]; result[0]=xa[0]*xb[0]-xa[1]*xb[1]; result[1]=xa[0]*xb[1]+xa[1]*xb[0]; String R= String.Format ("%d+%di", result[0],result[1]); returnR; }//Public static void Main (string[] args) {//String t = "A+bi";//string[] T1 = t.split ("\\+|i");//For (String s:t1) System.out.println (S.equals ("")? " Nothing ": s);//    }

A lot slower, it seems that split or regular matching efficiency is not high, the pursuit of speed as far as possible with the most primitive ideas.

Improvements to Method 1 (rarely, basically not) Get Method 3:

     Public Static int[] getNumv3 (String t) {intPlusindex = T.indexof ("+"); int[] x =New int[2]; x[0] = integer.valueof (t.substring (0, Plusindex)); x[1] = integer.valueof (t.substring (plusindex+1, T.length ()-1)); returnx; }     Publicstring Complexnumbermultiply (String A, string b) {int[] xa =GetNumv3 (a); int[] XB =GetNumv3 (b); int[] result =New int[2]; result[0]=xa[0]*xb[0]-xa[1]*xb[1]; result[1]=xa[0]*xb[1]+xa[1]*xb[0]; String R= String.Format ("%d+%di", result[0],result[1]); returnR; }

That is, do not ask for the index of I, with the length-1 instead of T, found that the increase of 0.03%, did not improve too much, it seems to call length also need to loop (? Doubt), but it will be faster than looking for index on the loop side.

How can optimization be optimized? Write your own valueof method? is not necessarily higher than the original efficiency. (limited to [-100,100] speed may be faster if).

Leetcode537 Complex number multiplication Java implementation

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.