16.11.25

来源:互联网 发布:源mac地址全是0的报文 编辑:程序博客网 时间:2024/05/21 04:39

405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:
All letters in hexadecimal (a-f) must be in lowercase.
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

Input: 26 Output: “1a”

Example 2:

Input: -1 Output: “ffffffff”

分析:将整数转换为16进制表示(小写且存在负数的情况)[位运算]

public class Solution {    public String toHex(int num) {        char[] map={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};         String result="";         if(num==0)return "0";         while(num!=0){             result=map[(num&15)]+result;            //三个大于号表示无符号右移,忽略符号位,空格以0补齐             num=(num>>>4);         }         return result;    }}

409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example “Aa” is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.
Example:
Input: “abccccdd”
Output: 7
Explanation: One longest palindrome that can be built is “dccaccd”,whose length is 7.
分析:将给定的字符串中的字符重新组合,判断能够返回的最长回文字符串的长度(大小写区分)

public class Solution {    public int longestPalindrome(String s) {        int size='z'-'A'+1;         int[] bucket=new int[size];         //用散列法将每个字符数量保存在数组中         for(char tmp:s.toCharArray()){             bucket[tmp-'A']++;         }         int count=0;         //用于判断是否存在奇数个字符         boolean odd=false;         for(int i=0;i<size;i++){             //如果是偶数,直接相加             if(bucket[i]%2==0){                 count+=bucket[i];             }else{                //如果是奇数,将字符对应的数量减去1后累加                 count+=bucket[i]-1;                //将奇数位置为true                 odd=true;             }         }         //当存在奇数个字符时,返回加1的结果         return odd==true?(count+1):count;    }}

412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
分析:根据规则输出ArrayList,当该数能被3整除则输出Fizz,当该数能被5整除输出Buzz,当该数能被3和5整除,输出FizzBuzz
Example:

n = 15,

Return: [
“1”,
“2”,
“Fizz”,
“4”,
“Buzz”,
“Fizz”,
“7”,
“8”,
“Fizz”,
“Buzz”,
“11”,
“Fizz”,
“13”,
“14”,
“FizzBuzz” ]

public class Solution {    public List<String> fizzBuzz(int n) {        List<String> resultList = new ArrayList<String>();        for(int i=1;i<=n;i++){            if(i%3==0&&i%5==0){                resultList.add("FizzBuzz");            }else if(i%3==0){                resultList.add("Fizz");            }else if(i%5==0){                resultList.add("Buzz");            }else{                resultList.add(String.valueOf(i));            }        }        return resultList;    }}
0 0