• Java--剑指offer(3)


    11.输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

    a)使用Integer.toBinaryString(n);来计算得出二进制的字符串,然后使用for循环截取字符串是否为1

           

    public class Solution {
    
                  public int NumberOf1(int n) {
    
                         String res = new String();
    
                         //这里定义一个变量用来计算二进制数中的1的个数
    
                         int i=0;
    
                 
    
                         res = Integer.toBinaryString(n);
    
                 
    
                         //判断二进制的字符串中的是不是为1
    
                         for(int j = 0; j<res.length(); j++){
    
                                String s = new String();
    
                                if(s.equals("1")){
    
                                       i++;
    
                                }
    
                         }
    
                 
    
                         return i;
    
                  }
    
     
    
                  public static void main(String[] args) {
    
                         Solution s = new Solution();
    
                         System.out.println(s.NumberOf1(5));
    
                  }
    
     
    
    }

    12.给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

           a)这里有一种很简单的方法,就是直接调用Math的Math.pow(base, exponent);方法。

          

     public class Solution {
    
                  public double Power(double base, int exponent) {
    
                         return Math.pow(base, exponent);
    
                  }
    
     
    
                  public static void main(String[] args) {
    
                         Solution s = new Solution();
    
                         System.out.println(s.Power(5,5));
    
                  }
    
     
    
    }

    b)还有一种方法就是自己使用Java来实现跟Math.pow(base, exponent);方法一样的功能。

    public class Solution {
    
           public double Power(double base, int exponent) {
    
                  double sum = 1;
    
                 
    
                  if(exponent == 0){
    
                         sum = 1;
    
                  }else if(exponent > 0){
    
                         for(int i = 0; i < exponent; i++){
    
                                sum = sum * base;
    
                         }
    
                  }else if(exponent < 0){
    
                         int flag = -exponent;
    
                         for(int i = 0; i < flag; i++){
    
                                sum = sum * base;
    
                         }
    
                         sum =  1/sum;
    
                  }
    
                  return sum;
    
           }
    
     
    
           public static void main(String[] args) {
    
                  Solution s = new Solution();
    
                  System.out.println(s.Power(5,-1));
    
           }
    
     
    
    }

    13.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

           a)这里首先new一个长度和输入的数组长度相同的数组,然后遍历数组进行判断。

           

    public class Solution {
    
                  public void reOrderArray(int[] array) {
    
                         int len = array.length;
    
                         int j=0;
    
                         int[] arr1 = new int[len];
    
     
    
                         for (int i = 0; i < len; i++) {
    
                                if(array[i] % 2 != 0){
    
                                       arr1[j] = array[i];
    
                                       j++;
    
                                }
    
                         }
    
                         for(int i = 0;i < len; i++){
    
                                if(array[i] % 2 == 0){
    
                                       arr1[j] = array[i];
    
                                       j++;
    
                                }
    
                         }
    
                 
    
                         for(int i = 0; i < len; i++){
    
                    array[i] = arr1[i];
    
                    System.out.print(array[i]);
    
                 }
    
                  }
    
     
    
                  public static void main(String[] args) {
    
                         Solution s = new Solution();
    
                         s.reOrderArray(new int[]{1,4,2,3,5,6,8});
    
                  }
    
     
    
    }

    14.输入一个链表,输出该链表中倒数第k个结点。

           

    public class Solution {
    
              public ListNode FindKthToTail(ListNode head,int k) {
    
                 ListNode pre=null,p=null;
    
                 //两个指针都指向头结点
    
                 p=head;
    
                 pre=head;
    
                 //记录k值
    
                 int a=k;
    
                 //记录节点的个数
    
                 int count=0;
    
                  //p指针先跑,并且记录节点数,当p指针跑了k-1个节点后,pre //指针开始跑,
    
                 //当p指针跑到最后时,pre所指指针就是倒数第k个节点
    
                 while(p!=null){
    
                    p=p.next;
    
                    count++;
    
                    if(k<1){
    
                       pre=pre.next;
    
                    }
    
                    k--;
    
                  }
    
                 //如果节点个数小于所求的倒数第k个节点,则返回空
    
                 if(count<a) return null;
    
                 return pre;
    
               
    
              }
    
    }

    15.输入一个链表,反转链表后,输出链表的所有元素。

           

    public class Solution {
    
               public ListNode ReverseList(ListNode head) {
    
                 if(head==null)
    
                    return null;
    
                 ListNode newHead = null;
    
                 ListNode pNode = head;
    
                 ListNode pPrev = null;
    
                 while(pNode!=null){
    
                    ListNode pNext = pNode.next;
    
                    if(pNext==null)
    
                       newHead = pNode;
    
                    pNode.next = pPrev;
    
                    pPrev = pNode;
    
                    pNode = pNext;
    
                 }
    
                 return newHead;
    
              }
    
    }
  • 相关阅读:
    [MyBatis]最简MyBatis工程
    eclipse中如何删除已经添加到 Web App Libraries 中引用的jar包
    day48_项目管理学习笔记
    项目流程之失败的案例
    项目流程之婚礼流程
    day47_Maven学习笔记
    快还要更快,让PHP 7 运行更加神速
    Linux服务器时间同步
    Win10系统开启Linux Bash命令行
    红帽RHOP 8 发布一条龙方案
  • 原文地址:https://www.cnblogs.com/wgl1995/p/5771205.html
Copyright © 2020-2023  润新知