![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构和算法
文章平均质量分 65
SHW_SEU
All Life is Problem-solving
展开
-
求子数组的最大和
输入一个整形数组,里有正数负数。数组中连续的一个或多整数组成子数组,每个子数组都有一个和。求所有子数组的和最大值。要时间复杂度为 O(n)。例如输入的数组为 1,-2,3,10,-4,7,2,-5,和最大的子数组为3,10,-4,7,2。因此输出为该子数组的和 18 。#include using namespace std;//遍历求和,当和为负数时置零后继续求和/原创 2014-04-02 17:10:29 · 543 阅读 · 0 评论 -
LeetCode:Remove Element(删除数组中的特定元素)
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new leng原创 2015-02-03 15:45:45 · 507 阅读 · 0 评论 -
LeetCode:Trapping Rain Water(装雨水问题)
原题:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Fo原创 2015-02-02 22:00:35 · 815 阅读 · 0 评论 -
LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 /原创 2015-02-03 10:42:16 · 609 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
原题:Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], You原创 2015-01-31 10:52:57 · 494 阅读 · 0 评论 -
LeetCode:Rotate List(旋转链表)
原题:Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.原创 2015-01-30 15:08:53 · 466 阅读 · 0 评论 -
LeetCode:Search a 2D Matrix (在元素递增的矩阵中搜寻特定元素)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first int原创 2015-01-30 09:21:09 · 438 阅读 · 0 评论 -
寻找只出现一次的数字(LeetCode: Single Number II)
原题:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2015-01-28 19:30:53 · 578 阅读 · 0 评论 -
判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
原题:Given a binary tree, determine if it is a valid binary search tree (BST).方法1:可以根据二叉搜索树的规律写出约束规则。对于二叉搜索树的任意结点,其左子树结点均小于结点本身值,右子树结点均大于结点本身值。根据这个规律可以给出两个变量min和max来限定每个结点的有效取值范围。假设任一结点current的取值范原创 2015-01-29 15:45:03 · 1176 阅读 · 0 评论 -
有序递增链表转化为平衡的二叉搜索树(LeetCode: Convert Sorted List to Binary Search Tree )
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 首先,这里是有序递增链表,那么链表的中间节点一定是二叉搜索树的根节点,左侧一半为左子树,右侧一半为右子树,这可以构建一个递归的过程。那么如何找到链表的中间节点呢?原创 2015-01-27 15:47:06 · 543 阅读 · 0 评论 -
面试题:两个无序数组合并成一个有序数组
昨天面试,问了一个简单的算法题,当时快速排序的代码有点忘了,还想了一会儿。最后写的估计也还有点问题,再写一遍。 思路:先分别对两个数组进行排序(可以用快速排序),然后两路归并。#include using namespace std;class Solution {public: int *sort(int *a,int lenA,int *b,int le原创 2015-03-20 10:38:18 · 5455 阅读 · 0 评论