【精】LintCode领扣算法问题答案:入门
文章目录
- 23. 判断数字与字母字符
- 描述
- 题解
- 25. 打印X
- 描述
- 题解
- 37. 反转一个3位整数
- 描述
- 题解
- 145. 大小写转换
- 描述
- 题解
- 366. 斐波纳契数列
- 描述
- 题解
- 454. 矩阵面积
- 描述
- 题解
- 463. 整数排序
- 描述
- 题解
- 466. 链表节点计数
- 描述
- 题解
- 484. 交换数组两个元素
- 描述
- 题解
- 632. 二叉树的最大节点
- 描述
- 题解
- 1613. 最高频率的IP
- 描述
- 题解
- 297. 寻找最大值
- 描述
- 题解
- 298. 寻找素数
- 描述
- 题解
- 1910. 数组中出现次数最多的值
- 描述
- 题解
- 最后说两句
23. 判断数字与字母字符
描述
给出一个字符c,你需要判断它是不是一个数字字符或者字母字符。
如果是,返回true,如果不是返回false。
题解
public class Solution {/*** @param c: A character.* @return: The character is alphanumeric or not.*/public boolean isAlphanumeric(char c) {// write your code herereturn Character.isDigit(c) || Character.isLetter(c);}
}
25. 打印X
描述
输入一个正整数N, 你需要按如下方式返回一个字符串列表。
题解
public class Solution {/*** @param n: An integer.* @return: A string list.*/public List<String> printX(int n) {// write your code here.List<String> ret = new ArrayList<>();StringBuilder sb = new StringBuilder();for (int i = 0; i < n; ++i) {sb.setLength(0);for (int j = 0; j < n; ++j) {if (j == i || j == n - 1 - i) {sb.append('X');} else {sb.append(' ');}}ret.add(sb.toString());}return ret;}
}
37. 反转一个3位整数
描述
反转一个只有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);}
}
145. 大小写转换
描述
将一个字符由小写字母转换为大写字母
题解
public class Solution {/*** @param character: a character* @return: a character*/public char lowercaseToUppercase(char character) {// write your code herereturn (char) (character-32);}
}
366. 斐波纳契数列
描述
查找斐波纳契数列中第 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 hereswitch (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;}}
}
454. 矩阵面积
描述
实现一个矩阵类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 herepublic 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 herepublic int getArea() {return this.width * height;}
}
463. 整数排序
描述
给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。
题解
public class Solution {/*** @param A: an integer array* @return: nothing*/public void sortIntegers(int[] A) {// write your code hereArrays.sort(A);}
}
jre自带的排序算法不一样是算法?合理合法。
466. 链表节点计数
描述
计算链表中有多少个节点.
题解
/*** 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 hereint count = 0;while (head != null) {count++;head = head.next;}return count;}
}
484. 交换数组两个元素
描述
给你一个数组和两个索引,交换下标为这两个索引的数字
题解
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 hereint t = A[index1];A[index1] = A[index2];A[index2] = t;}
}
632. 二叉树的最大节点
描述
在二叉树中寻找值最大的节点并返回。
题解
/*** 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 herereturn 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;}
}
1613. 最高频率的IP
描述
给定一个字符串数组lines, 每一个元素代表一个IP地址,找到出现频率最高的IP。
题解
public class Solution {/*** @param ipLines: ip address* @return: return highestFrequency ip address*/public String highestFrequency(String[] ipLines) {// Write your code hereint 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;}
}
297. 寻找最大值
描述
寻找 n 个数中的最大值。
题解
public class Solution {/*** @param nums: the list of numbers* @return: return the maximum number.*/public int maxNum(List<Integer> nums) {// write your code hereint max = Integer.MIN_VALUE;for (Integer n : nums) {if (n > max) {max = n;}}return max;}
}
298. 寻找素数
描述
输出n以内所有的素数。
- 保证 n 是100以内的整数。
题解
public class Solution {/*** @param n: an integer* @return: return all prime numbers within n.*/public List<Integer> prime(int n) {// write your code hereBitSet cache = new BitSet();List<Integer> ret = new ArrayList<>();for (int i = 2; i <= n; ++i) {if (cache.get(i)) {continue;}if (this.isPrime(i)) {ret.add(i);}int t = i;while (t <= n - i) {t += i;cache.set(t);}}return ret;}private boolean isPrime(int n) {for (int i = 2; i <= Math.sqrt(n); ++i) {if (n % i == 0) {return false;}}return true;}
}
1910. 数组中出现次数最多的值
描述
在给定的数组中,找到出现次数最多的数字。
出现次数相同时,返回数值最小的数字。
- 数组长度不超过100000。
- 0 <= a[i] <= 2147483647
题解
public class Solution {/*** @param array: An array.* @return: An integer.*/public int findNumber(int[] array) {// Write your code here.Map<Integer, Integer> counter = new HashMap<>();int maxCountNum = array[0];int maxCount = 0;for (int n : array) {int count = counter.getOrDefault(n, 0) + 1;counter.put(n, count);if (count > maxCount) {maxCount = count;maxCountNum = n;} else if (count == maxCount && n < maxCountNum) {maxCountNum = n;}}return maxCountNum;}
}
最后说两句
非常感谢你阅读本文章,如果你觉得本文对你有所帮助,请留下你的足迹,点个赞,留个言,多谢~
作者水平有限,如果文章内容有不准确的地方,请指正。
希望小伙伴们都能每天进步一点点。
本文由 二当家的白帽子 https://le-yi.blog.csdn.net/ 博客原创,转载请注明来源,谢谢~
【精】LintCode领扣算法问题答案:入门相关推荐
- 【精】LintCode领扣算法问题答案:1029. 寻找最便宜的航行旅途(最多经过k个中转站)
1029. 寻找最便宜的航行旅途(最多经过k个中转站) 描述 有n个城市被一些航班所连接.每个航班 (u,v,w) 从城市u出发,到达城市v,价格为w. 给定城市数目 n,所有的航班flights.你 ...
- 【精】LintCode领扣算法问题答案:1084. “马”在棋盘上的概率
1084. "马"在棋盘上的概率 描述 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1) ...
- 【精】LintCode领扣算法问题答案:306. 商品列表
306. 商品列表: 有一个商品列表,该列表是由L1.L2两个子列表拼接而成.当用户浏览并翻页时,需要从列表L1.L2中获取商品进行展示.展示规则如下: 用户可以进行多次翻页,用offset表示用户已 ...
- 【精】LintCode领扣算法问题答案:1086. 重复字符串匹配
1086. 重复字符串匹配: 给定两个字符串A和B,找到A必须重复的最小次数,以使得B是它的子字符串. 如果没有这样的解决方案,返回-1. A和B的长度在1到10000之间. 样例 1 输入 : A ...
- 【精】LintCode领扣算法问题答案:316. 组合集
316. 组合集 描述 给一个数组,给出所有可能的排列组合出的数小于给定的数字 1 <= len(num) <= 10 0 <= num[i] <= 9 target < ...
- 【精】LintCode领扣算法问题答案:993. 数组划分 I
993. 数组划分 I: 给一个有 2n 个整数的数组,你的任务是把这些整数分成 n 组,如(a1, b1),(a2, b2),-,(an, bn).并且使得 i 从 1 到 n 的 min(ai, ...
- 【精】LintCode领扣算法问题答案:1371. 链表组件
1371. 链表组件 描述 给定一个链表(链表结点包含一个整型值)的头结点 head. 同时给定列表 G,该列表是上述链表中整型值的一个子集. 返回列表 G 中组件的个数,这里对组件的定义为:链表中一 ...
- 【精】LintCode领扣算法问题答案:3. 统计数字
3. 统计数字 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 1: 输入:k = 1, n = 1 输出:1 解释:在 [0, 1] 中,我们发现 1 出现了 1 ...
- 【精】LintCode领扣算法问题答案:626. 矩形重叠
626. 矩形重叠 给定两个矩形,判断这两个矩形是否有重叠. l1代表第一个矩形的左上角 r1代表第一个矩形的右下角 l2代表第二个矩形的左上角 r2代表第二个矩形的右下角 保证:l1 != r1 并 ...
最新文章
- 漫画设计模式:什么是 “职责链模式” ?
- Ubuntu安装软件失败
- 自动化测试里的数据驱动和关键字驱动思路的理解
- 嵌入式中主动触发hardfault
- 区块链BaaS云服务(38)点存DCpool分布式存储
- 【Java面试宝典】深入理解JAVA虚拟机
- 进制转换,字符串,字节串之间转换
- input ios问题
- 设计灵感|信息图表海报竟然能设计的这么有趣!
- 敏捷开发般若敏捷系列之三:什么是敏捷(下)(无住,不住于空,破空执,非法,非非法)...
- 学习下新塘M0芯片的下载方法
- seo教程之对搜索引擎的研究
- 【论文笔记】多智能体强化学习值分解基础论文5篇
- OpenCV 模板匹配(Template Match)
- IE插件加载题目调试
- 数学笔记30——无穷级数和收敛判定
- 高效开发神器 | 合宙LuatIDE 1.1.0正式版本免费下载
- 阿朱:研发管理者必读文章
- EverNote开源协议-Android
- OpenMV零基础教程