Lintcode algorithm--Mobile 0, toy factory, left padding, ugly number

Source: Internet
Author: User
Tags int size lintcode

Title:lintcode (iii)
Author: Hui
Tags
-Algorithm
Categories
-Computer date:2017-09-04 10:14:00 Mobile 0

Give an array nums write a function to move 0 to the last face of the array, not 0 elements to keep the order of the original array sample

Given nums = [0, 1, 0, 3, 12], after calling the function, nums = [1, 3, 12, 0, 0]. Problem Solving Process

Slightly understand the sorting algorithm, the problem is not difficult, low-level bubbling can be completed, but the more simple, the more we have to consider the optimization of time complexity. Here we try to make the time complexity O (n).

public class Solution {
/**
 * @param nums a integer array
 * @return Nothing, does this in-place
 */
    Publ IC static void Movezeroes (int[] nums) {
        //Write your code here
        int pre = 0;
        int tail = 0;
        int[] array = new Int[nums.length];
        for (int i = 0;i < nums.length;i++) {
            if (nums[i]!=0) {
                Array[pre] = nums[i];
                pre++;
            } else {
                Array[nums.length-tail-1] = 0;
                tail++;
            }
        }
        for (int j = 0;j < nums.length;j++)
            nums[j] = array[j];
    }
}
Toy Factory

Factory mode is a common design pattern. Please implement a toy factory toyfactory used to produce different kinds of toys. It can be assumed that only cats and dogs have two kinds of toys. Sample Example

Toyfactory tf = Toyfactory ();
Toy Toy = Tf.gettoy (' Dog ');
Toy.talk (); 
>> Wow

toy = Tf.gettoy (' Cat ');
Toy.talk ();
>> Meow
Solution Process

What is the factory model, which is a design pattern that requires more familiarity. The main is to define an interface for creating objects, so that their subclasses decide which factory class to instantiate, and the Factory mode delays the creation process to subclasses. The main solution is the interface selection problem.

/**
 * Your object would be instantiated and called as such:
 * toyfactory tf = new Toyfactory ();
 * Toy Toy = Tf.gettoy (type);
 * Toy.talk ();
 *
/interface Toy {
    void talk ();
}

Class Dog implements Toy {
    //Write Your code here,
    void Talk () {
        System.out.println ("Wow");
    }
}

Class Cat implements Toy {
    //Write Your code here,
    void Talk () {
        System.out.println ("Meow");
    }
}

public class Toyfactory {
    /**
     * @param Type a string
     * @return Get object of the type */public
    T Oy Gettoy (String type) {
        //Write Your code here
        if (type==null)
            return null;
        if (Type.equals ("Dog"))
            return new Dog ();
        else if (type.equals ("Cat"))
            return new Cat ();
        else
            return null;
    }
}
left padding

Implement a Leftpad library, if you do not know what Leftpad can look at the sample. Sample Example

Leftpad ("foo", 5)
>> "  foo"

leftpad ("Foobar", 6)
>> "Foobar"

leftpad ("1", 2, "0")
>> "01"

Note that strings can be concatenated with "+", characters converted to strings, string.valueof (), or character.tostring (), and no other logical points:

public class StringUtils {/** * @param originalstr The string "We want to append" with spaces * @param size the target Length of the String * @return a String */static public string Leftpad (string originalstr, int size) {//
        Write Your code here if (Originalstr.length () > Size) return originalstr;
        String result = "";
        for (int i = 0; i < size-originalstr.length (); i++) result = result + "";
        result = result + Originalstr;
    return result;
     }/** * @param originalstr The string we want to append to * @param size the target length of the string * @param padchar the character to pad to the left side of the string * @return a String */static public String Leftpad (string originalstr, int size, char Padchar) {//Write your code here if (originalstr.length
        () > Size) return originalstr;
        String result = ""; String C = StriNg.valueof (Padchar);
        String C = character.tostring (Padchar);
        for (int i = 0; i < size-originalstr.length (); i++) result = result + C;
        result = result + Originalstr;
    return result;
 }
}
Ugly Number

Write a program to detect whether an integer is not an ugly number.

The ugly number is defined as a positive integer containing only the mass factor 2, 3, 5. For example, 6, 8 is the ugly number, but 14 is not ugly number thought he contains a quality factor 7. Sample Example

Given num = 8, returns TRUE.
Given num = 14, returns FALSE. disintegration Process

Use recursion

public class Solution {
 /* * @param num:an integer
 * @return: True if num is an ugly number or false
 */
  public boolean isugly (int num) {
        //write your code here
        if (num = = 0) return false;
        else if (num = = 1) return true;
        else if (num% 2 = = 0)
            return isugly (NUM/2);
        else if (num% 3 = = 0)
            return isugly (NUM/3);
        else if (num% 5 = = 0)
            return isugly (NUM/5);
        else return false;    
    }
}   

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.