Preparing for the intervies of FLAG and USDA

Source: Internet
Author: User
Tags throw exception

2,array

(1) How to implement Java ArrayList

 Public classArrayList {Private intcapacity; Private intsize; Private int[] data;  PublicArrayList (intcapacity_) {Capacity=capacity_; Size= 0; Data=New int[capacity]; }     Public intGetintindex) {        if(Index < 0 | | | Index >=size) {            //Throw ExceptionSystem.out.println ("ArrayIndexOutOfBoundsException:" +index); }        returnData[index]; }     Public voidSetintIndexintvalue) {        if(Index < 0 | | | Index >=size) {System.out.println ("ArrayIndexOutOfBoundsException:" +index); } Data[index]=value; }     Public voidAddintIndexintvalue) {        if(Index < 0 | | index >size) {System.out.println ("ArrayIndexOutOfBoundsException:" +index); }        if(Size = =capacity)        {Resize (); } size++;  for(inti = size-1; I >= index + 1; --i) {Data[i]= Data[i-1]; } Data[index]=value; }    Private voidResize () {capacity*= 2; int[] TempData =New int[size];  for(inti = 0; i < size; ++i) {Tempdata[i]=Data[i]; } Data=New int[capacity];  for(inti = 0; i < size; ++i) {Data[i]=Tempdata[i]; }    }     Public voidRemoveintindex) {        if(Index < 0 | | | Index >=size) {System.out.println ("ArrayIndexOutOfBoundsException:" +index); } size--;  for(inti = index; i < size; ++i) {Data[i]= Data[i + 1]; }    }         Public Static voidMain (string[] args) {int[] A = {1}; System.out.println (a[-1]); }}
View Code

(2) Sum

1. The Sum My submissions questioneditorial solutiontotal Accepted:247444 Total submissions:1017480Difficulty:easygiven An array of integers,returnindices of the numbers such that they add up to a specific target. You may assume this each input would has exactly one solution. Example:given Nums= [2, 7, one, all], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,return[0, 1]. UPDATE (2016/2/13): thereturnFormat had been changed to zero-based indices. Please read the above updated description carefully. Subscribe to see which companies asked ThisQuestion
View Code

Analyse:use the HashMap to save (Target-nums[i], i)

 Public classSolution { Public int[] Twosum (int[] Nums,inttarget) {        int[] res =New int[2]; if(NULL= = Nums | | Nums.length = = 0) {            returnRes; } HashMap<integer, integer> map =NewHashMap ();  for(inti = 0; i < nums.length; ++i) {if(!Map.containskey (Nums[i])) {Map.put (target-Nums[i], i); } Else{res[0] =Map.get (Nums[i]); res[1] =i;  Break; }        }        returnRes; }}/*** All Error: * 1, Compile error:line 20:error:unclosed comment. Because the space of ' * ' and '/' * 2, Map.add. Carelessness*/
View Code

1,time Complexity:

if the recurrence equation is T (n) = at (b/n) + N ^ c.  if logb a > C, t (n) = n ^ (logb a)if logb a = = C, t (n) = n ^ c * Lognif
     LOGB A < C, T (n) = n ^ C
View Code

Preparing for the intervies of FLAG and USDA

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.