别人家的代码

作者:Zack
链接:https://www.zhihu.com/question/35485418/answer/63081850
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 

一. 树


1)求二叉树的高度(Maximum Depth of Binary Tree)

// LeetCode, Maximum Depth of Binary Tree
// 时间复杂度O(n),空间复杂度O(logn)
class Solution {
public:
  int maxDepth(TreeNode *root) {
    if (root == nullptr) return 0;
    return max(maxDepth(root->left), maxDepth(root->right)) + 1;
  }
};


2)判断二叉树是否对称(Symmetric Tree)

// LeetCode, Symmetric Tree
// 递归版,时间复杂度O(n),空间复杂度O(logn)
class Solution {
public:
  bool isSymmetric(TreeNode *root) {
    return root ? isSymmetric(root->left, root->right) : true;
  }
  bool isSymmetric(TreeNode *left, TreeNode *right) {
    if (!left && !right) return true; // 终止条件
    if (!left || !right) return false; // 终止条件
    return left->val == right->val // 三方合并
    && isSymmetric(left->left, right->right)
    && isSymmetric(left->right, right->left);
  }
};


3)二叉树-> 链表(Flatten Binary Tree to Linked List)

// LeetCode, Flatten Binary Tree to Linked List
// 递归版2
// @author 王顺达(http://weibo.com/u/1234984145)
// 时间复杂度O(n),空间复杂度O(logn)
class Solution {
public:
  void flatten(TreeNode *root) {
    flatten(root, NULL);
  }
private:
// 把root 所代表树变成链表后,tail 跟在该链表后面
  TreeNode *flatten(TreeNode *root, TreeNode *tail) {
    if (NULL == root) return tail;
    root->right = flatten(root->left, flatten(root->right, tail));
    root->left = NULL;
    return root;
  }
};

 

二. 字符串

1)最长无重复字符子串(Longest Substring Without Repeating Characters)

class Solution {
public:
  int lengthOfLongestSubstring(string s) {
    int ans = 0;
    int dic[256];
    memset(dic,-1,sizeof(dic));
    int len = s.size();
    int idx = -1;

    for (int i=0;i<len;i++)
    {
      char c = s[i];
      if (dic[c]>idx)        //如果出现新的字符
        idx = dic[c];        //更新新的字符的下标

      ans = max(ans,i-idx);    //如果第一次出现 -(-1)相当于+1,

      dic[c] = i;            //更新下标。
    }

    return ans;
  }
};

 

三. 数组


1)数组中所有数字出现两次,只有一个出现一次,求出它(Single Number)

// LeetCode, Single Number
// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
  int singleNumber(int A[], int n) {
    int x = 0;
    for (size_t i = 0; i < n; ++i)
      x ^= A[i];
    return x;
  }
};


2)顺时针旋转二维数组90度(Rotate Image)

// LeetCode, Rotate Image
// 思路1,时间复杂度O(n^2),空间复杂度O(1)
class Solution {
  public:
    void rotate(vector<vector<int>>& matrix) {
      const int n = matrix.size();
      for (int i = 0; i < n; ++i) // 沿着副对角线反转
        for (int j = 0; j < n - i; ++j)
          swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
      for (int i = 0; i < n / 2; ++i) // 沿着水平中线反转
        for (int j = 0; j < n; ++j)
          swap(matrix[i][j], matrix[n - 1 - i][j]);
  }
};

 

3)排序数组去重(Remove Duplicates from Sorted Array)

// LeetCode, Remove Duplicates from Sorted Array
// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
  int removeDuplicates(int A[], int n) {
    if (n == 0) return 0;
    int index = 0;
    for (int i = 1; i < n; i++) {
      if (A[index] != A[i])
        A[++index] = A[i];
    }
    return index + 1;
  }
};


4)两个排序数组,求中位数(Median of Two Sorted Arrays)

// LeetCode, Median of Two Sorted Arrays
// 时间复杂度O(log(m+n)),空间复杂度O(log(m+n))
class Solution {
public:
  double findMedianSortedArrays(int A[], int m, int B[], int n) {
    int total = m + n;
    if (total & 0x1)
      return find_kth(A, m, B, n, total / 2 + 1);
    else
      return (find_kth(A, m, B, n, total / 2)
        + find_kth(A, m, B, n, total / 2 + 1)) / 2.0;
  }
private:
  static int find_kth(int A[], int m, int B[], int n, int k) {
    //always assume that m is equal or smaller than n
    if (m > n) return find_kth(B, n, A, m, k);
    if (m == 0) return B[k - 1];
    if (k == 1) return min(A[0], B[0]);
    //divide k into two parts
    int ia = min(k / 2, m), ib = k - ia;
    if (A[ia - 1] < B[ib - 1])
      return find_kth(A + ia, m - ia, B, n, k - ia);
    else if (A[ia - 1] > B[ib - 1])
      return find_kth(A, m, B + ib, n - ib, k - ib);
    else
      return A[ia - 1];
  }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值