#69 Sqrt(x)
#70 Climbing Stairs
#83 Remove Duplicates from Sorted List
#88 Merge Sorted Array
#94 Binary Tree Inorder Traversal
#69 Sqrt(x)
要求:给定非负整数,求其整数平方根,不能使用库函数
思路1:从0开始逐渐平方往给定数值靠近。但是总感觉这个方法很一般,倒是能通过
class Solution {
public:
int mySqrt(int x) {
long i = 0;
while(i*i < x)
i++;
return i*i == x ? i: i-1;
}
};
思路2:改进版,采用二分法找,加速一下,看来以后这种排序数组全遍历的都可以考虑二分法尝试做。
class Solution {
public:
int mySqrt(int x) {
if(x<=1) return x;
int left = 0, right = x;
while(left<right)
{
int mid = left+ (right -left)/2;
if(x/mid >= mid) left = mid+1;
else right = mid;
}
return right - 1;
}
};
参考大神博客园:[LeetCode(Q69)] Sqrt(x) (编程实现sqrt)
思路3:牛顿迭代法,中心思想就是两个值取值逐渐逼近。只当是拓展一下见识,这方法我以后估计也想不到。
class Solution {
public:
int mySqrt(int x) {
if(x == 0) return 0;
double last = 0;
double res = 1;
while(res != last)
{
last = res;
res = (res+x/res)/2;
}
return int(res);
}
};
#70 Climbing Stairs
要求:给定n层高,每次爬楼梯1梯或2梯,求多少种爬楼梯的方法
思路1:递归求解,思路没错,但是运行超时啊,因为入栈太深
class Solution {
public:
int climbStairs(int n) {
if(n<=2) return n;
else return climbStairs(n-1)+climbStairs(n-2);
}
};
思路2:换思路,写个数组,逐个递增求解,通过。
class Solution {
public:
int climbStairs(int n) {
if(n<=2)
return n;
vector<int>vec(n+1,0);
vec[0]=0;vec[1]=1;vec[2]=2;
for(int i = 3;i<=n;i++)
vec[i]=vec[i-1]+vec[i-2];
return vec[n];
}
};
思路3:添加数组的话,占用空间太多了,其实用2个变量保存即可。
class Solution {
public:
int climbStairs(int n) {
if(n<=2) return n;
int a= 1,b=2,result = 0;
for(int i = 3;i<=n;i++)
{
result = a+b;
a = b;
b = result;
}
return result;
}
};
#83 Remove Duplicates from Sorted List
要求:删除排序链表中的重复元素
思路1:依次向后判断,分为值相等和值不等依次改变指针即可。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return NULL;
ListNode * pNode = head;
ListNode * pNext = pNode->next;
while(pNext)
{
if(pNode->val != pNext->val)
{
pNode = pNext;
pNext = pNode->next;
}
else
{
pNext = pNext->next;
pNode->next = pNext;
}
}
return head;
}
};
#88 Merge Sorted Array
要求:给定2个非递减数组,合并到第一个数组上
思路1:没写出来,因为往第一个数组中插入后,索引就变了,没写出来;新增一个数组的话,倒是可以,但是感觉与题意想考察的不符。
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
vector<int>result;
int i = 0; int j = 0;
while(i<m && j<n)
{
if(nums1[i]<nums2[j])
{result.push_back(nums1[i]);i++;}
else
{result.push_back(nums2[j]);j++;}
}
if(i<m)
for(int t = i; t<m;t++)
result.push_back(nums1[t]);
else if(j<n)
for(int t = j; t<n;t++)
result.push_back(nums2[t]);
nums1=result;
}
};
参考大神博客园:Grandyang
思路2:发现我真的是死脑筋,从前往后插入会变索引,那可以从后往前插入啊。傻了。
另外就是多用自增、自减运算符,代码看起来会简单很多
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m-1, j = n-1, k = m+n-1;
while(i>=0 && j>=0)
{
if(nums1[i] > nums2[j]) nums1[k--]=nums1[i--];
else nums1[k--]=nums2[j--];
}
while(j>=0) nums1[k--]=nums2[j--];
}
};
#94 Binary Tree Inorder Traversal
要求:二叉树中序遍历
思路1:比较简单,上代码
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>result;
inOrder(root,result);
return result;
}
void inOrder(TreeNode* root,vector<int>&res)
{
if(!root) return ;
if(root->left) inOrder(root->left,res);
res.push_back(root->val);
if(root->right) inOrder(root->right,res);
}
};
参考大神博客园:Grandyang
思路2:采用栈,先将根节点,以及所有左节点压入到栈中,然后存值,遍历右节点
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>res;
stack<TreeNode*>s;
TreeNode*p = root;
while(p || !s.empty())
{
while(p)
{
s.push(p);
p = p->left;
}
p = s.top();s.pop();
res.push_back(p->val);
p = p->right;
}
return res;
}
};
菜鸡一枚,欢迎大家批评指正,谢谢~