//1、递归,注意求所有位数相加和的方法
public int addDigits(int num) {
if (num < 10) {
return num;
}
int next = 0;
while (num != 0) {
next = next + num % 10;
num /= 10;
}
return addDigits(next);
}
//2、数理方法
return (num - 1) % 9 + 1;
496、求下一个比本数大的数
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Stack < Integer > stack = new Stack < > ();
HashMap < Integer, Integer > map = new HashMap < > ();
int[] res = new int[findNums.length];
for (int i = 0; i < nums.length; i++) {
while (!stack.empty() && nums[i] > stack.peek())
map.put(stack.pop(), nums[i]);
stack.push(nums[i]);
}
while (!stack.empty())
map.put(stack.pop(), -1);
for (int i = 0; i < findNums.length; i++) {
res[i] = map.get(findNums[i]);
}
return res;
}
}