LintCode第241题转换字符串到整数(容易版),133题最长单词,771题-二阶阶乘

第241题:

 转换字符串到整数(容易版)

描述

给一个字符串, 转换为整数。你可以假设这个字符串是一个有效整数的字符串形式, 且范围在32位整数之间 (-231 ~ 231 - 1)。

样例  1:
	输入:  "123"
	输出: 123
	
	样例解释: 
	返回对应的数字.

样例 2:
	输入:  "-2"
	输出: -2
	
	样例解释: 
	返回对应的数字,注意负数.

代码如下: 

public class Solution {

    /**

     * @param target: A string

     * @return: An integer

     */

    public int stringToInteger(String target) {

        // write your code here

        return  Integer.parseInt(target);

    }

}

第133题:

描述

给一个词典,找出其中所有最长的单词

样例 1:
	输入:   {
		"dog",
		"google",
		"facebook",
		"internationalization",
		"blabla"
		}
	输出: ["internationalization"]



样例 2:
	输入: {
		"like",
		"love",
		"hate",
		"yes"		
		}
	输出: ["like", "love", "hate"]

 代码如下:

public class Solution {

    /*

     * @param dictionary: an array of strings

     * @return: an arraylist of strings

     */

    public List longestWords(String[] dictionary) {

        // write your code here

       

        Integer maxLength=-1;

        int n=dictionary.length;

        List returnStrList=new ArrayList<>();

        for(int i=0;i

        {

            if(maxLength

            {

            maxLength=dictionary[i].length();

            returnStrList.clear();

            returnStrList.add(dictionary[i]);

            }else if(maxLength==dictionary[i].length())

            {

            returnStrList.add(dictionary[i]);

            }

        }

        return returnStrList;

    }

}

第771题:

描述

给定一个数n,返回该数的二阶阶乘。在数学中,正整数的二阶阶乘表示不超过这个正整数且与它有相同奇偶性的所有正整数乘积

  • 结果一定不会超过long
  • n是一个正整数

样例1:

输入: n = 5
输出: 15
解释:
5!! = 5 * 3 * 1 = 15

样例2:

输入: n = 6
输出: 48
解释:
6!! = 6 * 4 * 2 = 48

 代码如下:

public class Solution {

    /**

     * @param n: the given number

     * @return:  the double factorial of the number

     */

    public long doubleFactorial(int n) {

        // Write your code here

        long returnValue=1L;

        while(n>=1)

        {

            returnValue=returnValue*n;

            n=n-2;

        }

        return returnValue;

    }

}

你可能感兴趣的:(算法,新手必刷编程50题,数据结构)