思路:每回碰到负数就取反
- class Solution {
- public int arraySign(int[] nums) {
- int res = nums[0];
- if(nums[0]>0){
- res = 1;
- }else if(nums[0]<0){
- res = -1;
- }else{
- return res;
- }
- for(int i = 1;i
- if(nums[i]<0){
- res = -res;
- }else if(nums[i] == 0){
- return 0;
- }
- }
- return res;
- }
- }
2.阶乘尾数
思路:15!= 1*2*3*4*5*6*7*8*9*(2*5)11*12*13*14*15
可以发现2的因子是肯定会比5的因子多,而只要拆分中出现2*5=10结果就会多一个0,所以问题就转换为求出有多少个为5的因子
- class Solution {
- public int trailingZeroes(int n) {
- int count = 0;
- int num = 5;
- while(n>=num){
- count += n/num;
- n = n/5;
- }
- return count;
- }
- }
溢出问题
1.整数反转
思路:反转方法有使用栈、字符串转整数、取模,这里使用取模更好
判断溢出的方法:max/10>num
- class Solution {
- public int reverse(int x) {
- int res = 0;
- int max = Integer.MAX_VALUE;
- while(x != 0){
- int t = x%10;
- System.out.print(t);
- if(res>max/10||res == max/10 && t >7){
- return 0;
- }
- if(res<-max/10||res == max/10 && t <=-8){
- return 0;
- }
- res = res*10 + t;
- x=x/10;
- }
- return res;
- }
- }
2.回文数
思路:判断回文方法双指针、截取一半比较
- class Solution {
- public boolean isPalindrome(int x) {
- if(x < 0){
- return false;
- }
- if(x %10 == 0 && x != 0){
- return false;
- }
- int book = 0;
- while(x>book){
- book = book*10+x%10;
- x /= 10;
- }
- return x==book || x == (book/10);
- }
- }
进制问题
1.七进制数
思路:
-
二进制到十进制:
- 二进制数每一位的权值为2的幂,从右往左依次是1、2、4、8、16...
- 将二进制数的每一位与对应权值相乘,然后求和即可得到十进制数。
例如,二进制数1011转换为十进制:(1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 8 + 0 + 2 + 1 = 11。
-
八进制到十进制:
- 八进制数每一位的权值为8的幂,从右往左依次是1、8、64、512...
- 将八进制数的每一位与对应权值相乘,然后求和即可得到十进制数。
-
十六进制到十进制:
- 十六进制数每一位的权值为16的幂,从右往左依次是1、16、256、4096...
- 将十六进制数的每一位与对应权值相乘,然后求和即可得到十进制数。需要注意的是,十六进制中的A表示10,B表示11,C表示12,D表示13,E表示14,F表示15。
-
十进制到其他进制:
- 将十进制数不断除以目标进制的基数,取余数作为新进制数的一位,直到商为0。
- 反转余数的顺序,即可得到目标进制数。
例如,将十进制数231转换为二进制:
- 231 / 2 = 115 余 1
- 115 / 2 = 57 余 1
- 57 / 2 = 28 余 1
- 28 / 2 = 14 余 0
- 14 / 2 = 7 余 0
- 7 / 2 = 3 余 1
- 3 / 2 = 1 余 1
- 1 / 2 = 0 余 1
反转余数的顺序为 11100111,即231的二进制表示。
-
其他进制到其他进制:
- 可以通过先将原数转换为十进制,然后再从十进制转换为目标进制来实现。
- class Solution {
- public String convertToBase7(int num) {
- StringBuilder sb = new StringBuilder();
- int sign = num<0 ? 1 : 0;
- if(num < 0){
- num = -num;
- }
- do{
- sb.append(num%7);
- num = num /7;
- }while(num>0);
- if(sign == 1){
- sb.append("-");
- }
- return sb.reverse().toString();
- }
- }
2.转换为N机制
- public static String convert(int M, int N) {
- Boolean flag = false;
- if (M < 0) {
- flag = true;
- M *= -1;
- }
- StringBuffer sb = new StringBuffer();
- int temp;
- while (M != 0) {
- temp = M % N;
- //技巧一:通过数组F[]解决了大量繁琐的不同进制之间映射的问题
- sb.append(F[temp]);
- M = M / N;
- }
- //技巧二:使用StringBuffer的reverse()方法,让原本麻烦的转置瞬间美好
- sb.reverse();
- //技巧三:最后处理正负,不要从一开始就揉在一起。
- return (flag ? "-" : "") + sb.toString();
- }