JinFei's Blog

Thinking will not overcome fear but action will.

[LeetCode]378. Kth Smallest Element in a Sorted Matrix

最大堆

题目描述 Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted o...

[LeetCode]494. Target Sum

dfs深度优先搜索

题目描述 You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find ...

[LeetCode]75. Sort Colors

索引的使用

题目描述 Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will ...

[LeetCode]581. Shortest Unsorted Continuous Subarray

水题,双指针的应用,遍历序列

题目描述 Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You ne...

[LeetCode]20. Valid Parentheses

水题,栈的应用

题目描述 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same...

[LeetCode]155. Min Stack

辅助栈的应用

题目描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the s...

[LeetCode]226. Invert Binary Tree

树的翻转,递归

题目描述 Invert a binary tree. 递归版本解题思路 如果当前节点为空, 为本层递归结束的标志 当前节点的左右子树进行互换位置 分别对当前节点的左子树,右子树进行翻转 返回root即可 /** * Definition for a binary tree node. * struct TreeNode { * int val;...

[LeetCode]206. Reverse Linked List

翻转链表,递归,循环

题目描述 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iterat...

[剑指Offer]扑克牌顺子

排序算法的应用

题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…..LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,...

[剑指Offer]按照之字形打印二叉树/把二叉树打印成多行

树的层次遍历,顺序打印,层次遍历

题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 解题思路 借助队列来层次遍历 一个变量表示第i层,如果是奇数层,需要调用STL中的reverse函数 每次遍历当前队列元素个数的元素 每次遍历的时候,将左右子节点入队列 /* struct TreeNod...