LintCode算法题解——移动零、玩具工厂、左填充、丑数

本文提供了LintCode上几个经典算法题目的解决方案,包括移动零、玩具工厂、左填充、丑数等题目,涵盖数组操作、设计模式及数学问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

title: LintCode题解(三)
author: 惠惠
tags:
- 算法
categories:
- 计算机

date: 2017-09-04 10:14:00

移动零

给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序

样例

给出 nums = [0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].

解题过程

稍懂排序算法,这题没难点,低级冒泡就可以完成,但是越是简单,就越要考虑优化时间复杂度。这里我们尽量让时间复杂度为O(n)。

public class Solution {
/**
 * @param nums an integer array
 * @return nothing, do this in-place
 */
    public 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];
    }
}

玩具工厂

工厂模式是一种常见的设计模式。请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类。可以假设只有猫和狗两种玩具。

样例

ToyFactory tf = ToyFactory();
Toy toy = tf.getToy('Dog');
toy.talk(); 
>> Wow

toy = tf.getToy('Cat');
toy.talk();
>> Meow

解答过程

何为工厂模式,这是要求再熟悉不过的一种设计模式了。主要是定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。主要解决接口的选择问题。

/**
 * Your object will 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
    public void talk(){
        System.out.println("Wow");
    }
}

class Cat implements Toy {
    // Write your code here
    public void talk(){
        System.out.println("Meow");
    }
}

public class ToyFactory {
    /**
     * @param type a string
     * @return Get object of the type
     */
    public Toy 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;
    }
}

左填充

实现一个leftpad库,如果不知道什么是leftpad可以看样例。

样例

leftpad("foo", 5)
>> "  foo"

leftpad("foobar", 6)
>> "foobar"

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

注意字符串可以用“+”相连接,字符转化为字符串,用String.valueOf(),或者Character.toString()即可,其他没有逻辑点:

public class StringUtils {
/**
 * @param originalStr the string we want to append to 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;
    }
}

丑数

写一个程序来检测一个整数是不是丑数。

丑数的定义是,只包含质因子 2, 3, 5 的正整数。比如 6, 8 就是丑数,但是 14 不是丑数以为他包含了质因子 7。

样例

给出 num = 8,返回 true。
给出 num = 14,返回 false。

解体过程

使用递归

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;    
    }
}   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值