LintCode的问题答案
反转一个只有3位数的整数。
public class Solution {
/**
* @param number: A 3-digit number.
* @return: Reversed number.
*/
public int reverseInteger(int number) {
String value = new StringBuilder(number + "").reverse().toString();
return Integer.parseInt(value);
}
}
将一个字符由小写字母转换为大写字母
public class Solution {
/**
* @param character: a character
* @return: a character
*/
public char lowercaseToUppercase(char character) {
// write your code here
return (char) (character-32);
}
}
查找斐波纳契数列中第 N 个数。
所谓的斐波纳契数列是指:
前2个数是 0 和 1 。
第 i 个数是第 i-1 个数和第i-2 个数的和。
斐波纳契数列的前10个数字是:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
public class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public int fibonacci(int n) {
// write your code here
switch (n) {
case 1:
return 0;
// break;
case 2:
return 1;
// break;
default:
{
n = n - 2;
int result = 0;
int last2 = 0;
int last1 = 1;
while (n-- > 0) {
result = last2 + last1;
last2 = last1;
last1 = result;
}
return result;
}
// break;
}
}
}
实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:
两个共有的成员变量 width 和 height 分别代表宽度和高度。
一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
一个成员函数 getArea,返回这个矩阵的面积。
public class Rectangle {
/*
* Define two public attributes width and height of type int.
*/
// write your code here
private int width;
private int height;
/*
* Define a constructor which expects two parameters width and height here.
*/
// write your code here
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
/*
* Define a public method `getArea` which can calculate the area of the
* rectangle and return.
*/
// write your code here
public int getArea() {
return this.width * height;
}
}
给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。
public class Solution {
/**
* @param A: an integer array
* @return: nothing
*/
public void sortIntegers(int[] A) {
// write your code here
Arrays.sort(A);
}
}
jre自带的排序算法不一样是算法?合理合法。
计算链表中有多少个节点.
/**
* 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;
}
}
给你一个数组和两个索引,交换下标为这两个索引的数字
public class Solution {
/**
* @param A: An integer array
* @param index1: the first index
* @param index2: the second index
* @return: nothing
*/
public void swapIntegers(int[] A, int index1, int index2) {
// write your code here
int t = A[index1];
A[index1] = A[index2];
A[index2] = t;
}
}
在二叉树中寻找值最大的节点并返回。
/**
* 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) {
// write your code here
return this.getMaxChild(root);
}
private TreeNode getMaxChild(TreeNode parent) {
if (parent == null) {
return null;
}
TreeNode maxLeft = parent.left;
TreeNode maxRight = parent.right;
if (maxLeft == null
&& maxRight == null) {
return parent;
}
if (maxLeft != null) {
maxLeft = this.getMaxChild(maxLeft);
}
if (maxRight != null) {
maxRight = this.getMaxChild(maxRight);
}
if (maxLeft == null) {
return this.max(parent, maxRight);
}
if (maxRight == null) {
return this.max(parent, maxLeft);
}
return this.max(parent, this.max(maxLeft, maxRight));
}
private TreeNode max(TreeNode node1, TreeNode node2) {
return node1.val > node2.val ? node1 : node2;
}
}
给定一个字符串数组lines, 每一个元素代表一个IP地址,找到出现频率最高的IP。
public class Solution {
/**
* @param ipLines: ip address
* @return: return highestFrequency ip address
*/
public String highestFrequency(String[] ipLines) {
// Write your code here
int maxCount = 0;
String maxTimeIpLine = null;
Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String ipLine : ipLines) {
Integer count = countMap.get(ipLine);
if (count == null) {
count = 0;
}
count++;
countMap.put(ipLine, count);
if (count > maxCount) {
maxCount = count;
maxTimeIpLine = ipLine;
}
}
return maxTimeIpLine;
}
}
非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。
欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。