标签:new turn 顺序 off cti inter 习惯 包含 i++
public class Solution { public int GetUglyNumber_Solution(int index) { if(index <= 0){ return 0; } //分别记录质因子2,3,5所计算到的位置。 int i2 = 0; int i3 = 0; int i5 = 0; int[] arr = new int[index]; arr[0] = 1; for(int i = 1;i < index;i++){ int a = arr[i2]*2; int b = arr[i3]*3; int c = arr[i5]*5; while(a <= arr[i-1]){ a = arr[++i2] * 2; } while(b <= arr[i-1]){ b = arr[++i3] * 3; } while(c <= arr[i-1]){ c = arr[++i5] * 5; } arr[i] = a>b?(b>c?c:b):(a>c?c:a); } return arr[index - 1]; } }
更优写法:
public class Solution { public int GetUglyNumber_Solution(int index) { if(index <= 0){ return 0; } //分别记录质因子2,3,5所计算到的位置。 int i2 = 0; int i3 = 0; int i5 = 0; int[] arr = new int[index]; arr[0] = 1; for(int i = 1;i < index;i++){ int a = arr[i2]*2; int b = arr[i3]*3; int c = arr[i5]*5; arr[i] = a>b?(b>c?c:b):(a>c?c:a); if(a == arr[i]){i2++;} if(b == arr[i]){i3++;} if(c == arr[i]){i5++;} } return arr[index - 1]; } }
标签:new turn 顺序 off cti inter 习惯 包含 i++
原文地址:https://www.cnblogs.com/MoonBeautiful/p/13067282.html