zoukankan      html  css  js  c++  java
  • LeetCode 970. Powerful Integers (强整数)

    题目标签:HashMap

      题目让我们找出所有独一的powerful integers 小于bound的情况下。

      把 x^i 看作 a;把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍。

      参考的这种方法,用for loop 写的比较简洁易懂。

      具体看code。

    Java Solution:

    Runtime: 4 ms, faster than 99.85% 

    Memory Usage: 37.5 MB, less than 7.69%

    完成日期:03/13/2019

    关键点:把 x^i 看作 a;把 y^j 看作b, 代入for loop

    class Solution 
    {
        public List<Integer> powerfulIntegers(int x, int y, int bound) 
        {
            Set<Integer> result = new HashSet<>();
    
            for(int a = 1; a < bound; a *= x) 
            {
                for(int b = 1; a + b <= bound; b *= y) 
                {
                    result.add(a + b);
                    
                    if(y == 1) 
                        break;
                }
                
                if(x == 1) 
                    break;
            }
            
            return new ArrayList<>(result);
        }
    }

    参考资料:https://leetcode.com/problems/powerful-integers/discuss/214197/Java-straightforward-try-all-combinations

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    NYOJ 734
    NYOJ 762
    NYOJ 743
    NYOJ 478
    NYOJ 451
    NYOJ 461
    NYOJ 485
    NYOJ 333
    平均互信息
    ASCII码
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/10550004.html
Copyright © 2011-2022 走看看