37. 反转一个3位整数

描述

反转一个只有3位数的整数。

你可以假设输入一定是一个只有三位数的整数,这个整数大于等于100,小于1000。

您在真实的面试中是否遇到过这个题?  是

样例

123 反转之后是 321
900 反转之后是 9

class Solution {
public:/*** @param number: A 3-digit number.* @return: Reversed number.*/int reverseInteger(int number) {if(number >=100 && number <1000){int one = number % 10;int two = ((number - one) /10) % 10;int three = (number-(number%100))/100;return one*100+two*10+three;}}
};

145. 大小写转换

描述

将一个字符由小写字母转换为大写字母

你可以假设输入一定在小写字母 a ~ z 之间

您在真实的面试中是否遇到过这个题?  是

样例

a -> A

b -> B

class Solution {
public:/*** @param character: a character* @return: a character*/char lowercaseToUppercase(char character) {// write your code herereturn character-32;}
};

146. 大小写转换 II

描述

将一个字符串中的小写字母转换为大写字母。忽略其他不是字母的字符。

您在真实的面试中是否遇到过这个题?  是

样例

给出 "abc", 返回 "ABC".

给出 "aBc", 返回 "ABC".

给出 "abC12", 返回 "ABC12".

class Solution {
public:/*** @param str: A string* @return: A string*/string lowercaseToUppercase2(string &str) {// write your code herefor(int i  = 0; str[i] != '\0'; i++){if(str[i] >= 97 && str[i] <= 122)str[i] = str[i] -32;}return str;}
};

147. 水仙花数

描述

水仙花数的定义是,这个数等于他每一位上数的幂次之和 见维基百科的定义

比如一个3位的十进制整数153就是一个水仙花数。因为 153 = 13 + 53 + 33。

而一个4位的十进制数1634也是一个水仙花数,因为 1634 = 14 + 64 + 34 + 44。

给出n,找到所有的n位十进制水仙花数。

你可以认为n小于8。

您在真实的面试中是否遇到过这个题?  是

样例

比如 n = 1, 所有水仙花数为:[0,1,2,3,4,5,6,7,8,9]
而对于 n = 2, 则没有2位的水仙花数,返回 []

class Solution {
public:/*** @param n: The number of digits* @return: All narcissistic numbers with n digits*/vector<int> getNarcissisticNumbers(int n) {// write your code herevector<int> nums;if(n == 1){for(int i = 0; i<10; i++)nums.push_back(i);return nums;}else{for(int i = pow(10,n-1); i<pow(10,n);i++){int sum = 0;int x = i;while(x >= 1){sum +=pow(x%10, n);x = x/10;}if(sum == i)nums.push_back(i);}}return nums;}
};

214. 数组的最大值

描述

给一个浮点数数组,求数组中的最大值。

您在真实的面试中是否遇到过这个题?  是

样例

给出数组 [1.0, 2.1, -3.3], 返回 2.1.

class Solution {
public:/*** @param A: An integer* @return: a float number*/float maxOfArray(vector<float> &A) {// write your code heredouble max = -10000;for(int i = 0; i<A.size(); i++)if(A[i] > max)max = A[i];return max;}
};

219. 在排序链表中插入一个节点

描述

在链表中插入一个节点。

您在真实的面试中是否遇到过这个题?  是

样例

给出一个链表 1->4->6->8 和 val = 5.。

插入后的结果为 1->4->5->6->8

/*** Definition of singly-linked-list:* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*        this->val = val;*        this->next = NULL;*     }* }*/class Solution {
public:/*** @param head: The head of linked list.* @param val: An integer.* @return: The head of new linked list.*/ListNode * insertNode(ListNode * head, int val) {// write your code hereListNode *dummy = new ListNode(INT_MIN);dummy->next = head;ListNode *p = dummy;while(p->next && p->next->val < val)p = p->next;ListNode *n = new ListNode(val);n->next = p->next;p->next = n;return dummy->next;}
};

225. 在链表中找节点

描述

在链表中找值为 value 的节点,如果没有的话,返回空。

您在真实的面试中是否遇到过这个题?  是

样例

给出 1->2->3 和 value = 3, 返回最后一个节点 last node.

给出 1->2->3 和 value = 4, 返回空。

/*** Definition of ListNode* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*         this->val = val;*         this->next = NULL;*     }* }*/class Solution {
public:/** @param head: the head of linked list.* @param val: An integer.* @return: a linked node or null.*/ListNode * findNode(ListNode * head, int val) {// write your code hereListNode * p = head;if(head = NULL)return NULL;while(p != NULL){if(p->val == val)return p;p = p -> next;} }
};

228. 链表的中点

描述

找链表的中点。

您在真实的面试中是否遇到过这个题?  是

样例

链表 1->2->3 的中点是 2

链表 1->2 的中点是 1

挑战

如果链表是一个数据流,你可以不重新遍历链表的情况下得到中点么?

/*** Definition of ListNode* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*         this->val = val;*         this->next = NULL;*     }* }*/class Solution {
public:/** @param head: the head of linked list.* @return: a middle node of the linked list*/ListNode * middleNode(ListNode * head) {// write your code hereListNode * fast = head;ListNode * slow = head;if(head == NULL){return NULL;}if(head -> next == NULL)return head;while(fast->next != NULL && fast->next->next != NULL){slow = slow->next;fast = fast ->next -> next;}return slow;}
};

235. 分解质因数

描述

将一个整数分解为若干质因数之乘积

你需要从小到大排列质因子。

您在真实的面试中是否遇到过这个题?  是

样例

给出 10, 返回 [2, 5].

给出 660, 返回 [2, 2, 3, 5, 11].

class Solution {
public:/*** @param num: An integer* @return: an integer array*/vector<int> primeFactorization(int num) {// write your code herevector<int> result;while(num %2 == 0){num /= 2;result.push_back(2);}for(int i = 3; (i*i)<=num; i += 2){while(num % i == 0){num /= i;result.push_back(i);}}if(num != 1) result.push_back(num);return result;}
};

239. 方程的根

描述

给一个方程: ax2 + bx + c = 0. 求根。

  • 如果方程有两个根,就返回一个包含两个根的数组/列表。
  • 如果方程只有一个根,就返回一个包含一个跟的数组/列表。
  • 如果方程没有根,就返回一个空数组/列表。

您在真实的面试中是否遇到过这个题?  是

样例

给出 a = 1, b = -2, c = 1. 返回 [1].

给出 a = 1, b = -3, c = 2. 返回 [1, 2]. 第一个数应比第二个数小。

给出 a = 1, b = 1, c = 1. 返回 []

class Solution {
public:/** @param a: parameter of the equation* @param b: parameter of the equation* @param c: parameter of the equation* @return: a double array, contains at most two root*/vector<double> rootOfEquation(double a, double b, double c) {// write your code herevector<double> result;double d = b*b - 4*a*c;if(d<0) return result;else if(d == 0){double x =(-b - sqrt(d))/(2*a);result.push_back(x);}else{double x1 =(-b - sqrt(d))/(2*a);double x2 =(-b + sqrt(d))/(2*a);if(x1<x2){result.push_back(x1);result.push_back(x2);}else {result.push_back(x2);result.push_back(x1);}}return result;}
};

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

描述

Given a string, convert it to an integer. You may assume the string is a valid integer number that can be presented by a signed 32bit integer (-231 ~ 231-1).

您在真实的面试中是否遇到过这个题?  是

样例

给出 "123", 返回 123.

class Solution {
public:/*** @param str: A string* @return: An integer*/int stringToInteger(string &str) {// write your code herereturn stoi(str);}
};

283. 三数之中的最大值

描述

给三个整数,求他们中的最大值。

给出 num1 = 1, num2 = 9, num3 = 0, 返回 9.

您在真实的面试中是否遇到过这个题?  是

样例

Given num1 = 1, num2 = 9, num3 = 0, return 9.

class Solution {
public:/*** @param num1: An integer* @param num2: An integer* @param num3: An integer* @return: an interger*/int maxOfThreeNumbers(int num1, int num2, int num3) {// write your code hereint max = num1;if(num2 > max) max = num2;if(num3 > max) max = num3;return max;}
};

366. 斐波纳契数列

描述

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

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

  • 前2个数是 0 和 1 。
  • 第 i 个数是第 i-1 个数和第i-2 个数的和。

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

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

The Nth fibonacci number won't exceed the max value of signed 32-bit integer in the test cases.

您在真实的面试中是否遇到过这个题?  是

样例

给定 1,返回 0

给定 2,返回 1

给定 10,返回 34

class Solution {
public:/*** @param n: an integer* @return: an ineger f(n)*/int fibonacci(int n) {// write your code herevector<int> nums = {0, 1};if(n == 1 || n == 2)  return n-1;int i = 0 ;for(i = 2; i<= n-1; i++){nums.push_back(nums[i-1]+nums[i-2]);}return nums[n-1];}
};

449. 字符转整数

描述

将字符转换为一个整数。你可以假设字符是ASCII码,也就是说转换后的整数在0~255之间。

您在真实的面试中是否遇到过这个题?  是

样例

给出 a, 返回 97
给出 %, 返回 37

class Solution {
public:/*** @param character: a character* @return: An integer*/int charToInteger(char character) {// write your code herereturn (int)character;}
};

452. 删除链表中的元素

描述

删除链表中等于给定值val的所有节点。

您在真实的面试中是否遇到过这个题?  是

样例

给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5

class Solution {
public:/*** @param head: a ListNode* @param val: An integer* @return: a ListNode*/ListNode * removeElements(ListNode * head, int val) {// write your code hereListNode * tmp = new ListNode(0);tmp->next = head;head = tmp;while (head->next != NULL){if (head->next->val == val){head->next = head->next->next;}else head = head->next;}return tmp->next;}
};

463. 整数排序

描述

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

您在真实的面试中是否遇到过这个题?  是

样例

对于数组 [3, 2, 1, 4, 5], 排序后为:[1, 2, 3, 4, 5]

class Solution {
public:/*** @param A: an integer array* @return: nothing*/void sortIntegers(vector<int> &A) {// write your code heresort(A.begin(),A.end());}
};

466. 链表节点计数

描述

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

您在真实的面试中是否遇到过这个题?  是

样例

给出 1->3->5, 返回 3.

/*** Definition of singly-linked-list:* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*        this->val = val;*        this->next = NULL;*     }* }*/class Solution {
public:/*** @param head: the first node of linked list.* @return: An integer*/int countNodes(ListNode * head) {// write your code hereListNode * p = head;int i = 0;for(i = 0; p != NULL; i++){p = p ->next;}return i;}
};

479. 数组第二大数

描述

在数组中找到第二大的数

你可以假定至少有两个数字

您在真实的面试中是否遇到过这个题?  是

样例

给出 [1, 3, 2, 4], 返回 3.

给出 [1, 2], 返回 1.

class Solution {
public:/*** @param nums: An integer array* @return: The second max number in the array.*/int secondMax(vector<int> &nums) {// write your code heresort(nums.begin(),nums.end());return nums[nums.size()-2];}
};

483. 链表转数组

描述

将一个链表转换为一个数组。

您在真实的面试中是否遇到过这个题?  是

样例

给出链表 1->2->3->null, 返回 [1,2,3].

/*** Definition of singly-linked-list:* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*        this->val = val;*        this->next = NULL;*     }* }*/class Solution {
public:/*** @param head: the head of linked list.* @return: An integer list*/vector<int> toArrayList(ListNode * head) {// write your code herevector<int> nums;ListNode *p = head;while(p != NULL){nums.push_back(p->val);p = p -> next;}return nums;}
};

484. 交换数组两个元素

描述

给你一个数组和两个索引,交换下标为这两个索引的数字

您在真实的面试中是否遇到过这个题?  是

样例

给出 [1,2,3,4] index1 = 2, index2 = 3. 交换之后变成 [1,2,4,3]

class Solution {
public:/*** @param A: An integer array* @param index1: the first index* @param index2: the second index* @return: nothing*/void swapIntegers(vector<int> &A, int index1, int index2) {// write your code hereswap(A[index2], A[index1]);}
};

489. 数组化链表

描述

将一个数组变成链表

您在真实的面试中是否遇到过这个题?  是

样例

给出 [1,2,3,4], 返回 1->2->3->4->null

/*** Definition of ListNode* class ListNode {* public:*     int val;*     ListNode *next;*     ListNode(int val) {*         this->val = val;*         this->next = NULL;*     }* }*/class Solution {
public:/** @param nums: an integer array* @return: the first node of linked list*/ListNode * toLinkedList(vector<int> &nums) {// write your code hereListNode *head = new ListNode(0);ListNode *p = head;for(int i = 0; i < nums.size(); i++){ListNode *a = new ListNode(nums[i]);p -> next = a;p = p -> next;}p -> next =NULL;return head -> next;}
};

632. 二叉树的最大节点

描述

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

您在真实的面试中是否遇到过这个题?  是

样例

给出如下一棵二叉树:

     1/   \-5     2/ \   /  \
0   3 -4  -5

返回值为 3 的节点。

/*** Definition of TreeNode:* class TreeNode {* public:*     int val;*     TreeNode *left, *right;*     TreeNode(int val) {*         this->val = val;*         this->left = this->right = NULL;*     }* }*/class Solution {
public:TreeNode * a =new TreeNode(-10000);/** @param root: the root of tree* @return: the max node*/TreeNode * maxNode(TreeNode * root) {// write your code hereif(root ==  NULL) return NULL;if(root -> val > a -> val) a = root;maxNode(root -> left);maxNode(root -> right);return a;}
};

763. 进制转换

描述

给定一个十进制数 n 和 一个整数 k, 将 十进制数 n 转换成 k进制数.

1.0<=n<=2^31-12<=k<=16
2.每个大于 9 的字符都用大写字母表示

您在真实的面试中是否遇到过这个题?  是

样例

样例 1:
给定 n = 5k = 2
return "101"

样例 2:
给定 n = 30k = 16
return "1E"

class Solution {
public:/*** @param n: a decimal number* @param k: a Integer represent base-k* @return: a base-k number*/string hexConversion(int n, int k) {// write your code hereif (n == 0) return "0";const char NUMS[16] = {'0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};string result;while (n) {result = NUMS[n % k] + result;n /= k;}return result;}
};

[lintcode]入门相关推荐

  1. LintCode入门题目

    37. 反转一个3位整数 反转一个只有3位数的整数. 样例 样例 1: 输入: number = 123 输出: 321 样例 2: 输入: number = 900 输出: 9 注意事项 你可以假设 ...

  2. LintCode使用全解:如何高效提升算法和数据结构水平?

    对于IT领域的求职者来说,通过刷题提升自己的编程能力是非常有必要的.在线评测平台 LintCode,整合了当前各大IT企业技术求职的热门题库,拥有2000多道常见面试题.可有效提升你的算法与数据结构水 ...

  3. LintCode刷题(入门篇)

    最近在玩LintCode上面的算法题.下面分享一下部分题目的答案.如果其他同学有更好的答案,可以和我交流讨论,本人菜鸟一个,各位大佬多指点. 同时说一下,这个上面的二叉树 和 链表 我不懂,所以这类题 ...

  4. LintCode 算法部分入门题目 【C++】

    目录 1613.最高频的IP 483.链表转数组 449.字符转整数 228.链表的中点 225.在链表中找节点 22.列表扁平化 1613.最高频的IP 给定一个字符串数组lines, 每一个元素代 ...

  5. LintCode算法入门:

    1.A+B的问题 描述 给出两个整数 aa 和 bb , 求他们的和. 你不需要从输入流读入数据,只需要根据aplusb的两个参数a和b,计算他们的和并返回就行. 说明 a和b都是 32位 整数么? ...

  6. LintCode python入门题

    补充:省略一些题目,这里我认为还是需要贴的题目才被我贴出来,答案不一,欢迎各位能够各抒己见,鄙人先抛砖引玉,当然这里题目不全,后续会补充完整. 目录 2940 · 字节串和字符串互转 2931 · 从 ...

  7. 【精】LintCode领扣算法问题答案:入门

    文章目录 23. 判断数字与字母字符 描述 题解 25. 打印X 描述 题解 37. 反转一个3位整数 描述 题解 145. 大小写转换 描述 题解 366. 斐波纳契数列 描述 题解 454. 矩阵 ...

  8. java基础算法题(入门题与简单题)

    题目来自lintcode,答案来自九章算术,将自己在lintcode上训练的一些简单算法题贴出来,作为知识的总结与整理.便于查看复习. 第一部分(入门级别,只做了开放的部分,大部分需要收费的VIP才有 ...

  9. 如何学习编程、一门编程语言怎么算入门、快速掌握一门编程语言

    ninechapter 美帝代码搬运工,资深面试官,微信公众号-九章算法(ninechapter) 当你提出这个问题的时候,总有人会告诉你,你应该学习这个,学习那个,亦或者是刷题根本没用,你应该去做项 ...

最新文章

  1. 【杠精】问卷星防切屏解除方法-解除问卷星平台模拟考试防作弊切屏和最大化全屏的方法
  2. chrome 技术篇-控制台切换框架,切换dom操作区
  3. jetbrick-template 1.1.0 发布,支持 #tag, #macro, layout
  4. 本机速度文件支持的“纯” Java大数据存储
  5. ASP.NET 4.0升级至ASP.NET 4.5需要注意的地方
  6. 字符串左侧补0_(48)C++面试之最长不含重复字符的子字符串(动态规划)
  7. AI程序员的远方是诗和梦想的美好?还是骨感无望的现实?
  8. 无线充电手机 协议 c语言,这款无线充电器仅69元?支持Qi协议
  9. 计算机技术应用于测量,在测量绘图中计算机技术的应用分析
  10. python爬取豆瓣电影250_python爬取豆瓣电影top250数据存入数据库
  11. Git出现 Your local changes to the following files would be overwritten by merge: con
  12. 终于解决 归递调用 警告,其实程序没有 归递调用*** WARNING L13: RECURSIVE CALL TO SEGMENT
  13. android gpuimage 直播,1小时学会:最简单的iOS直播推流(四)如何使用GPUImage,如何美颜...
  14. CSS filter-仿磨砂玻璃效果
  15. mybatis 字符串比较 == 用法
  16. Golang TCP服务器用户修改名字、查询用户在线以及私聊功能
  17. hb哈勃公链_曝光!HB哈勃公链,花式套路
  18. hdu2648 shoping
  19. 西门子博途编程 - 变频器控制
  20. oa系统需要邮箱服务器,OA办公系统与邮件系统DBMail无缝结合 - 操作步骤指导(无限用户¥2200)...

热门文章

  1. 黑色星期五c语言程序,[蓝桥杯][算法训练VIP]黑色星期五 (C语言代码)
  2. Storm基础(完整版)
  3. 怎么用计算机求优秀比,计算机考试字处理求所占比值用什么公式
  4. matlab手写输入,基于matlab的手写输入板
  5. Linux与Windows双系统下的Grub引导管理器安装。
  6. 《JavaScript设计模式》读后感 觉很复杂
  7. Spring系列之强大的Spel表达式
  8. Linux命令英文全称学习
  9. 苹果CEO史蒂夫·乔布斯(Steve Jobs),2005年,斯坦福大学
  10. 发那科机器人没有码垛指令_FANUC机器人码垛教程