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 here
return 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 here
for(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 here
vector<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 here
double 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 here
ListNode *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 here
ListNode * 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 here
ListNode * 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 here
vector<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 here
vector<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 here
return 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 here
int 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 here
vector<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 here
return (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 here
ListNode * 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 here
sort(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 here
ListNode * 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 here
sort(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 here
vector<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 here
swap(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 here
ListNode *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 here
if(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-1
, 2<=k<=16
2.每个大于 9 的字符都用大写字母
表示
您在真实的面试中是否遇到过这个题? 是
样例
样例 1:
给定 n
= 5
, k
= 2
return "101"
样例 2:
给定 n
= 30
, k
= 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 here
if (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;
}
};