-2

For example, n=11 means, then the map should have 0-1, 1-4, 2-1, 3-1, 4-1, 5-1, 6-1, 7-1, 8-1, 9-1

 public void countDigits(int n, Map map) {
          while (n != 0) {
            int d = n%10;
            n /= 10;
            map.put(d,map.get(d)++);
          }
          return result;
        }

Other than the above method. I want to get all the digit count from 1 to N.

  • I don't think your loop is doing what you want it to do.. Are you calling it from some other loop iterating from 1 to n? – Rohit Jain Feb 09 '15 at 16:27
  • 2
    Your problem is poorly specified. Please clarify your question. –  Feb 09 '15 at 16:34
  • what's the logic behind your expected output? please explain. – dognose Feb 09 '15 at 16:34
  • There is an easy way to do this with recursion. Write f(10a+b) as a function of f(a), b, and countDigits(a). – Douglas Zare Feb 10 '15 at 01:24
  • What is your question? – talex Feb 10 '15 at 13:45
  • @Douglas Zare Can you please explain your answer. – Kumar SIVA Feb 11 '15 at 08:44
  • Actually, with these definitions, it's a little simpler to relate f(10a+b) to f(a-1), b, and countDigits(a). Suppose you know the digits which occur in the numbers up to 56. Take the numbers up to 573 and break them into the numbers from 1 through 9, from 10 through 569, and from 570 through 573. Express the digits in one of these sets in terms of the digits of numbers from 1 through 56. – Douglas Zare Feb 11 '15 at 09:00

1 Answers1

0

Your code isn't compiles at all. Try replace map.put(d,map.get(d)++); with

Integer tmp = (Integer)map.get(d);
if(tmp == null) tmp = 0;
tmp++;
map.put(d,tmp);
talex
  • 17,973
  • 3
  • 29
  • 66