- 博客(47)
- 收藏
- 关注
原创 【剑指offer】字符串的排列
题目描述:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。思路:全排列问题例如:输入字符串abc,先固定a,求bc的全排列;再把a和b交换位置,固定b,
2017-03-02 15:09:27 444
原创 【剑指offer】二叉搜索树与双向链表
题目描述输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int va
2017-03-02 13:58:54 225
原创 排序算法
1.直接插入排序 //直接插入排序 //基本思想:每次将一个待排序的记录插入到已经排好序的序列中 //时间复杂度:O(n2) //稳定性:稳定的 public static void insertSort(int array[]) { //第一个数看作排好序的序列,从第二个数开始寻找插入的位置 for(int i=1;
2017-03-01 23:41:12 246
原创 【leetcode】83. Remove Duplicates from Sorted List
题目要求:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.即删除链表中重复的元素,使每
2016-11-04 23:15:43 215
原创 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
题目要求:Given inorder and postorder traversal of a tree, construct the binary tree.给定二叉树后序和中序遍历,构造二叉树思路:后序的最后一个结点是根节点,在中序遍历中找到对应的位置,记录左子树的size和右子树的size,递归构造左右子树/** * Definition for a binar
2016-11-04 21:09:42 192
原创 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
题目要求:Given preorder and inorder traversal of a tree, construct the binary tree.通过先序遍历和中序遍历构造二叉树思路:先序遍历的第一个结点在中序遍历中找到位置i,然后在先序遍历的位置i左边是左子树,先序遍历的位置i右边是右子树,然后递归的构造左右子树/** * Definition
2016-11-04 19:14:01 356
原创 【leetcode】103. Binary Tree Zigzag Level Order Traversal
题目要求:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Gi
2016-11-04 18:48:00 209
原创 【leetcode】109. 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.给定一个排好序的链表,转换成二叉搜索树思路:和108题类似,/** * Definition for singly-linked list. * p
2016-11-04 18:07:02 234
原创 【leetcode】108. Convert Sorted Array to Binary Search Tree
题目要求:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.给定一个排好序的数组,要求转换成二叉搜索树思路:数组中间的元素作为树的根节点,左边的元素是左子树,右边的元素是右子树,递归建立左右子树/** * Definition for
2016-11-04 17:26:28 236
原创 【leetcode】199. Binary Tree Right Side Viewti
题目要求:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tr
2016-11-04 17:20:20 244
原创 【leetcode】257. Binary Tree Paths
题目要求:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]
2016-11-03 15:52:50 266
原创 【leetcode】101. Symmetric Tree
题目要求:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \
2016-11-03 15:40:47 278
原创 【leetcode】107. Binary Tree Level Order Traversal II
题目要求: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,
2016-11-03 15:22:17 229
原创 【leetcode】110.Balanced Binary Tree
题目要求:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node
2016-11-02 22:14:17 167
原创 【leetcode】111. Minimum Depth of Binary Tree
题目要求:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.给定一颗二叉树,求最小深度/** * D
2016-11-02 21:46:31 164
原创 【leetcode】121. Best Time to Buy and Sell Stock
题目要求:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of
2016-06-23 12:43:09 183
原创 【leetcode】191. Number of 1 Bits
题目要求:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representa
2016-06-20 13:47:51 207
原创 【leetcode】217. Contains Duplicate
题目要求:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every e
2016-06-20 12:33:58 161
原创 【leetcode】169. Majority Element
题目要求:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majori
2016-06-19 17:44:30 196
原创 【leetcode】242. Valid Anagram
题目要求:Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:
2016-06-18 17:53:59 193
原创 【leetode】349. Intersection of Two Arrays
题目要求:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be uni
2016-06-17 15:35:21 182
原创 【leetcode】202. Happy Number
题目要求:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of
2016-06-16 21:09:28 152
原创 【leetcode】263. Ugly Number
题目要求:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is no
2016-06-16 20:54:25 218
原创 【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
题目要求:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is def
2016-05-06 13:51:56 193
原创 【leetcode】326. Power of Three 231. Power of Two 342. Power of Four
这三道题做法一致,以326.Power of Three 为例题目要求:Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?给定一个整数,判断是不是3
2016-05-05 15:19:23 244
原创 【leetcode】141. Linked List Cycle
题目要求:Given a linked list, determine if it has a cycle in it.即给定一个链表,判断是否有环的存在思路:定义一个快指针,一个慢指针,若两个指针相遇,则说明有环存在/** * Definition for singly-linked list. * class ListNode { * int va
2016-05-04 20:28:41 241
原创 【leetcode】100. Same Tree
题目要求:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.即
2016-05-04 19:57:15 213
原创 【leetcode】102. 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 / \
2016-05-03 20:39:18 296
原创 【数据结构】将有序数组转为二叉搜索树
//将有序数组转化为二叉搜索树 //二叉搜索树:每一个节点的值大于左孩子的值,小于右孩子的值,如果采用中序遍历,输出结果为从小到大的 public BinaryTree arrayToBST(T array[],int start,int end) { if(start>end) { return null;
2016-05-03 15:22:41 492
原创 【数据结构】二叉树相关操作
1、二叉树的层次遍历 //二叉树的层次遍历 //利用队列 public void FloorVisit(BinaryTree root) { Queue queue = new LinkedList(); if(root==null) { return; } Bina
2016-05-02 21:51:45 385
原创 【leetcode】226. Invert Binary Tree
题目要求:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1即将一颗二叉树进行左右翻转思路:递归的将左子树和右子树进行对调/** * Definition for a bin
2016-05-02 21:49:21 226
原创 【leetcode】104. Maximum Depth of Binary Tree
题目要求:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.即给定一个二叉树,求它的最大深度思路:递归
2016-05-02 18:12:09 212
原创 【leetcode】283. Move Zeroes
题目要求:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after ca
2016-05-01 19:33:51 176
原创 【数据结构】二叉树遍历
二叉树结构/** * Created by novax_000 on 2016/5/1. */public class BinaryTree { public T value; public BinaryTree left; public BinaryTree right; public BinaryTree(T value, BinaryTree le
2016-05-01 19:07:22 230
原创 【leetcode】9. Palindrome Number
题目要求:Determine whether an integer is a palindrome. Do this without extra space.即判断一个数x是不是回文数思路:首先获取这个整数的位数count,定义一个n等于10的count-1次幂,让一个临时变量number等于这个整数。number%10获取的是最后一位,x/n%10获取的是第一位,判断这两
2016-04-19 17:34:23 250
原创 【leetcode】125. 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 c
2016-04-19 17:05:53 227
原创 位运算
位运算是对整数在内存中的二进制位进行运算主要有以下几种:1、按位与(&): 相同位的两个数都为1结果为1,只要有一个是0,结果就为0;2、按位或(|): 相同位的两个数只要有一个为1,结果就为1;3、按位取反(~): 把0、1全部取反,0变成1,1变成04、按位异或(^): 相同位的两个数相同(即都为1或都为0),结果为0,两个数不同,结果为1
2016-04-17 15:52:01 268
原创 【leetcode】258. Add Digits
题目要求:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since
2016-04-16 23:00:56 198
原创 【leetcode】292. Nim Game
题目要求:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone wi
2016-04-16 22:54:06 217
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人