Leetcode题解(23)

69. Sqrt(x)

题目

分析,题目实现求一个int数的平方根,最暴力的算法就是逐个遍历,从1开始到x,判断是否有一个数i,其中满足i*i<=x,并且(i+1)*(i+1)>x;这个算法发虽然简单,但是效率不高。其实,按照暴力算法的思想,我们可以联想到在一个已经排好序的数组中查找一个数,该数正好满足上面的条件,因此可以采用二分查找的思想。代码如下:

 1 class Solution {
 2 public:
 3     int mySqrt(int x) {
 4         int a[10]={0,1,1,1,2,2,2,2,2,3};
 5         if(x<=9)
 6             return a[x];
 7
 8         unsigned long long left=1,right=x;
 9
10         unsigned long long middle= (left + right)>>1;
11         while(left<=right)
12         {
13             middle = (left + right)>>1;
14             if(middle * middle <x)
15             {
16                 left = middle+1;
17
18             }
19             else if(middle * middle > x)
20             {
21                 right = middle - 1;
22
23             }
24             else
25                 return middle;
26         }
27         if(middle * middle > x)
28             middle--;
29         //if(middle * middle < x)
30         //    middle++;
31         return middle;
32     }
33 };

------------------------------------------------------------------------分割线--------------------------------------------------------------------------

70. Climbing Stairs

题目

分析:费波拉契数列

代码如下:

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6
 7         if(n <= 2)
 8         {
 9             return n;
10         }
11         else
12         {
13             int* step = new int[n];
14
15             step[0] = 1;
16             step[1] = 2;
17
18             for(int i = 2; i < n; i++)
19             {
20                 step[i] = step[i-1] + step[i-2];
21             }
22             return step[n-1];
23         }
24     }
25 };

-------------------------------------------------------------------------------分割线----------------------------------------------------------------

71. Simplify Path

提示:简单的字符串操作,其基本思想是将path按照‘\‘进行截取,并将截取后的字符串压栈。

代码如下:

 1 class Solution {
 2 public:
 3     string simplifyPath(string path) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         stack<string> s;
 7         string str;
 8         for(int i = 0; i < path.size(); i++)
 9         {
10             if (path[i] == ‘/‘)
11             {
12                 if (str == "..")
13                 {
14                     if (!s.empty())
15                         s.pop();
16                 }
17                 else if (str != "." && str != "")
18                 {
19                     s.push(str);
20                 }
21
22                 str = "";
23             }
24             else
25             {
26                 str += path[i];
27             }
28         }
29
30         if (str == "..")
31         {
32             if (!s.empty())
33                 s.pop();
34         }
35         else if (str != "." && str != "")
36             s.push(str);
37
38         if (s.empty())
39             return "/";
40
41         string ret;
42         while(!s.empty())
43         {
44             ret = "/" + s.top() + ret;
45             s.pop();
46         }
47
48         return ret;
49     }
50 };
时间: 2024-08-01 03:49:35

Leetcode题解(23)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co