美文网首页
Java算法习题记

Java算法习题记

作者: en_young | 来源:发表于2019-04-01 19:23 被阅读0次

1、记得/表示取整,%表示取模,别搞混淆了;

2、

   大小写字母的转化:

   public class Solution {

    /**

     * @param character: a character

     * @return: a character

     */

    public char lowercaseToUppercase(char character) {

        // write your code here

        /*if(character<='z'&&character>='a')

        character-=32;

        return character;*/

        /*String character1=Character.toString(character);

        String character2=character1.toUpperCase();

        char[] result= character2.toCharArray();

        return result[0];*/

    }

}

注意:Java中String类型,开头S大写;

3、查找斐波纳契数列中第 N 个数。

所谓的斐波纳契数列是指:

2个数是 0 1

i个数是第i-1个数和第i-2个数的和。

斐波纳契数列的前10个数字是:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

样例

样例  1:

输入:  1

输出: 0

样例解释

返回斐波那契的第一个数字,是0.

样例 2:

输入:  2

输出: 1

样例解释

返回斐波那契的第二个数字是1.

Public  class solution{

        Public  int fibonacci(int n){

          if(n==1) return 0;

          if(n==2) return 1;

        Return fibonacci(n-1)+fibonacci(n-2);

}

}

4Java中数组没有length()方法,只有length的属性;即Array.length;

                 字符串stringlength()的方法;即str.length()

给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。

样例

样例  1:

输入:  [3, 2, 1, 4, 5]

输出:  [1, 2, 3, 4, 5]

样例解释: 

返回排序后的数组。

样例 2:

输入:  [1, 1, 2, 1, 1]

输出:  [1, 1, 1, 1, 2]

样例解释: 

返回排好序的数组。

public class Solution {

    /**

     * @param A: an integer array

     * @return: nothing

     */

    public void sortIntegers(int[] A) {

        // write your code here

        /*冒泡排序*/

       /* for(int j=0;j

        for (int i=0;i

            if(A[i]>A[i+1]){

                int temp=A[i];

                A[i]=A[i+1];

                A[i+1]=temp;

            }

        }

        }

        System.out.print(A);*/

         /*插入排序*/

   /* for(int i=1;i

        int j;

        int temp=A[i];

        for( j=i;j>0&&temp

            A[j]=A[j-1];

        A[j]=temp;

        

    }

    System.out.print(A);*/

    

    /*选择排序*/

   /* for(int i=0;i

        int k=i;

        int j;

        for(j=k+1;j

            if(A[j]

            k=j;

            }

        }

        if(i!=k){

            int temp=A[k];

            A[k]=A[i];

            A[i]=temp;

        }

        

    }

    System.out.print(A);*/

    /*选择排序2*/

    /*for(int i=0;i

        int Min=A[i];

        int k=i;                  //细节之处千万别忽视,如果写int k=0;则会导致错误,因为每一趟即意味着前面的数都已经排序过了,如果写int k=0,如果那趟k没有变化,则会修改已经排好序的A[0];

        for(int j=i+1;j

            if(A[j]

                Min=A[j];

                k=j;

            }

        }

        A[k]=A[i];

        A[i]=Min;

    }

    System.out.print(A);*/

    

    

    }

}

5、计算链表中有多少个节点.

样例

样例  1:

输入:  1->3->5->null

输出: 3

样例解释: 

返回链表中结点个数,也就是链表的长度.

样例 2:

输入:  null

输出: 0

样例解释: 

空链表长度为0

/**

 * Definition for ListNode

 * public class ListNode {

 *    int val;

 *    ListNode next;

 *    ListNode(int x) {

 *        val = x;

 *        next = null;

 *    }

 * }

 */

public class Solution {

    /**

     * @param head: the first node of linked list.

     * @return: An integer

     */

    public int countNodes(ListNode head) {

        // write your code here

        int count=0;

        while(head!=null){

            count++;

            head=head.next;

        }

        return count;

    }

}

6、二叉树的最大节点

在二叉树中寻找值最大的节点并返回。

样例

样例 1:

输入:

    1

   /  \

 -5    3

 / \  /  \

1  2 -4  -5 

输出: 值为3的节点

样例解释:

返回值最大的节点

样例 2:

输入:

    10

   /  \

 -5    2

 / \  /  \

0  3 -4  -5 

输出: 值为10的节点

样例解释:

返回值最大的节点

/**

 * Definition of TreeNode:

 * public class TreeNode {

 *    public int val;

 *    public TreeNode left, right;

 *    public TreeNode(int val) {

 *        this.val = val;

 *        this.left = this.right = null;

 *    }

 * }

 */

public class Solution {

    /*

     * @param root: the root of tree

     * @return: the max node

     */

    public TreeNode maxNode(TreeNode root) {

        TreeNode left;

        TreeNode right;

        if(root==null)

        return null;

        left=maxNode(root.left);

        right=maxNode(root.right);

        if(left!=null){

            if(root.val

                root=left;

            }

        }

        if(right!=null){

            if(root.val

                root=right;

            }

        }

        return root;

    }

}


7、简单的常识:

java int 类整数的最大值是 2 的 31 次方 - 1 = 2147483648 - 1 = 2147483647

可以用 Integer.MAX_VALUE 表示它,即 int value = Integer.MAX_VALUE;

Integer.MAX_VALUE + 1 = Integer.MIN_VALUE = -2147483648

再大的数就要用 long (最大值 2 的 63 次方 - 1 )或者 BigDecimal 表示;

题目描述:

二叉树的最大节点

在二叉树中寻找值最大的节点并返回。

样例

样例 1:

输入:

    1

   /  \

 -5    3

 / \  /  \

1  2 -4  -5 

输出: 值为3的节点

样例解释:

返回值最大的节点

样例 2:

输入:

    10

   /  \

 -5    2

 / \  /  \

0  3 -4  -5 

输出: 值为10的节点

样例解释:

返回值最大的节点

参考代码:

/**

* Definition of TreeNode:

* public class TreeNode {

*    public int val;

*    public TreeNode left, right;

*    public TreeNode(int val) {

*        this.val = val;

*        this.left = this.right = null;

*    }

* }

*/

public class Solution {

public  int  MIndepth(TreeNode root){

       if(root==null) return 0;

 return    getMin(root);

}

public int getMin(TreeNode root){

if(root==null) return Integer.MAX_VALUE;

if(root.left==null&&root.right==null)

return 1;

return Math.Min(getMin(root.left),getMin(root.right))+1;

}

}

相关文章

网友评论

      本文标题:Java算法习题记

      本文链接:https://www.haomeiwen.com/subject/podbbqtx.html