面试笔试杂项积累-leetcode 261-270

来源:互联网 发布:php网店源码 编辑:程序博客网 时间:2024/06/03 20:19

263.263-Ugly Number-Difficulty: Easy

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor7.

Note that 1 is typically treated as an ugly number.

思路

只能被2,3,5整除的数为ugly number,判断是否为ugly number

能否被这三个数除尽,即可

2,3,5这三个数分别取余做除法,至余数不为零,如果除到最后是1,说明能出尽,为ugly number

public class Solution {    public bool IsUgly(int num) {                if (num <= 0)             return false;                  while (num % 2 == 0)            num /= 2;        while (num % 3 == 0)            num /= 3;        while (num % 5 == 0)            num /= 5;        if (num == 1)            return true;        return false;    }}

264.264-Ugly Number II-Difficulty: Medium

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first10 ugly numbers.

Note that 1 is typically treated as an ugly number.

Hint:

  1. The naive approach is to callisUgly for every number until you reach the nth one. Most numbers arenot ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).

方法一

思路

263题的升级版,

求第n个丑数,

博主按照hint的方法做的,用三个list列表,获取最小的为当前序列丑数,求出当前序列丑数之后,把它插在三个list后面。。直到达到n个

public class Solution {    public int NthUglyNumber(int n) {             IList<int> l1 = new List<int>();        IList<int> l2 = new List<int>();        IList<int> l3 = new List<int>();        l1.Add(1);        l2.Add(1);        l3.Add(1);        int ugly = 1;        while (n > 1)        {            int temp1 = l1[0] * 2;            int temp2 = l2[0] * 3;            if (temp1 == temp2)            {                l2.RemoveAt(0);                temp2 = l2[0] * 3;            }            int temp3 = l3[0] * 5;            if (temp3 == temp2 || temp3 == temp1)            {                l3.RemoveAt(0);                temp3 = l3[0] * 5;            }            if (temp1 < temp2 && temp1 < temp3)            {                l1.RemoveAt(0);                    l1.Add(temp1);                    l2.Add(temp1);                    l3.Add(temp1);                ugly = temp1;            }            else if (temp2 < temp1 && temp2 < temp3)            {                l2.RemoveAt(0);                    l1.Add(temp2);                    l2.Add(temp2);                    l3.Add(temp2);                ugly = temp2;            }            else if (temp3 < temp1 && temp3 < temp2)            {                l3.RemoveAt(0);                l1.Add(temp3);                l2.Add(temp3);                l3.Add(temp3);                ugly = temp3;            }            --n;        }        return ugly;    }}


方法二

思路


参考:

https://leetcode.com/discuss/71549/o-n-java-easy-version-to-understand

   int getMin(int a,int b,int c){     int min=Integer.min(a,b);     min=Integer.min(min,c);     return min;}public int nthUglyNumber(int n) {    if(n==1)      return 1;      int[] a=new int[n];    int p2=0,p3=0,p5=0,p=1,count=1;     a[0]=1;    while(count<n){        a[p]=getMin(a[p2]*2,a[p3]*3,a[p5]*5);        while(a[p2]*2<=a[p])           p2++;        while(a[p3]*3<=a[p])           p3++;        while(a[p5]*5<=a[p])           p5++;          p++;          count++;    }    return a[--p];}

方法三

思路

使用DP!!!

本质上也是三个list的hint的思路

参考:

https://leetcode.com/discuss/79589/shortest-o-n-java-dp-solution

public int nthUglyNumber(int n) {        if(n==1) return 1;        int[] dp = new int[n+1]; // dp[i] holds the ith's ugly number        dp[1] = 1;        int p2=1, p3=1, p5=1;        for(int i=2; i<=n; i++){ // loop invariant:dp[i] holds the smallest ith uglynumber            dp[i] = Math.min(2*dp[p2], Math.min(3*dp[p3],5*dp[p5])); // the next ugly number must be built from a smaller ugly number            if(dp[i]==2*dp[p2])p2++;             if(dp[i]==3*dp[p3])p3++;            if(dp[i]==5*dp[p5])p5++;        }        return dp[n];    }

268.268-Missing Number-Difficulty: Medium

Given an array containing n distinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

思路

找到序列缺的那个数

比较蠢的方法就是sort一下然后挨个找

博主发现,求出序列总数+1的和再减去当前这个缺一个数的总和,就是那个缺的数。

实现起来时间复杂度O(n),空间O(1),这是最好的方法

public class Solution {    public int MissingNumber(int[] nums) {        int sum = 0;        int cur = 0;        int sum_num = 0;        for (int i = 0; i < nums.Length; i++)        {            sum += cur;            ++cur;            sum_num += nums[i];        }        return sum + cur - sum_num;    }}






0 0