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;
}
}