algorithm
算法小练
奇子君
每天进步一点
展开
-
PAT1005 Spell It Right
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.math.BigInteger;public class Main { public static void main(String[] args) throws IOExc...原创 2020-01-22 09:50:20 · 153 阅读 · 0 评论 -
PAT1002 A+B for Polynomials
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;...原创 2020-01-20 14:30:21 · 129 阅读 · 0 评论 -
PAT1001 A+B Format
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws IOException { BufferedRea...原创 2020-01-20 14:29:01 · 249 阅读 · 0 评论 -
Copy List with Random Pointer --- LeetCode
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.解题思路:1.遍历链表,在旧节点后面插入同样的新节点,cop原创 2015-02-03 12:57:04 · 316 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree --- LeetCode
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:使用二叉搜索树的方式遍历数组,每次遍历数组中间节点为子树的根节点,递归遍历找出中间节点。/** * Definition for binary tree * public clas原创 2015-02-02 10:56:43 · 375 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree --- LeetCode
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST解题思路:中序递归遍历构造,每次遍历找到中间节点即为根节点,左边为左子树,右边为右子树。public class Solution { public原创 2015-02-03 10:52:32 · 389 阅读 · 0 评论 -
Container With Most Water --- LeetCode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2015-02-02 10:20:14 · 336 阅读 · 0 评论 -
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.解题思路:按先序序列和中序序列构造二叉树,先序序列中第一个值为根的值。递归遍历如下:/** *原创 2015-01-30 14:14:42 · 344 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal --- LeetCode
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解题思路:按着中序遍历序列和后序遍历序列构造二叉树。后序序列中的最后一个val值为根的val值。递归遍历如下:原创 2015-01-30 13:57:21 · 316 阅读 · 0 评论 -
Count and Say --- LeetCode
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2015-02-04 10:19:48 · 507 阅读 · 0 评论 -
Decode Ways --- LeetCode
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total numb原创 2015-02-04 11:08:53 · 383 阅读 · 0 评论 -
Binary Tree Maximum Path Sum --- LeetCode
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.原创 2015-01-21 10:09:19 · 400 阅读 · 0 评论 -
Candy ---- LeetCode
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than原创 2015-01-22 09:27:33 · 380 阅读 · 0 评论 -
Sort Colors---LeetCode
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will原创 2014-11-06 11:01:07 · 709 阅读 · 0 评论 -
Add Binary----LeetCode
Add Binary----LeetCode原创 2015-01-05 12:35:15 · 470 阅读 · 0 评论 -
Add Two Numbers---LeetCode
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7原创 2015-01-06 09:55:47 · 365 阅读 · 0 评论 -
Clone Graph --- LeetCode
解题思路:使用BSF遍历图,使用HashMap保存原节点->新节点的克隆,使用HashSet记录已经访问过的节点。 队列中node节点poll,同时将node加入到set中记录已经visited。 map中是否已经有node的克隆,若无则创建新节点cNode,有则取得该节点cNode。 遍历node的neighbers并将neighbers创建或取得,克隆到cNode的neighbers中。原创 2015-01-23 10:14:37 · 379 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III ---- LeetCode
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not engage in multiple transactions at the same time (ie, you原创 2015-01-12 11:49:59 · 453 阅读 · 0 评论 -
Compare Version Numbers --- LeetCode
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2015-01-30 12:47:54 · 443 阅读 · 0 评论 -
Combination Sum --- LeetCode
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2015-01-29 11:11:18 · 337 阅读 · 0 评论 -
Anagrams --- LeetCode
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.解题思路:anagrams:回文构词法,一组单词的字母数量和种类都一样,只是字母顺序不同。空字符串的anagrams为空字符串。 使用hash来存储键值对:将每个string字母排序后的字符串作为key,value值存string在数组的位原创 2015-01-07 10:58:29 · 398 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II ---- LeetCode
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However原创 2015-01-08 12:27:51 · 348 阅读 · 0 评论 -
Binary Tree Level Order Traversal ---- LeetCode
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).原创 2015-01-09 10:23:05 · 423 阅读 · 0 评论 -
Best Time to Buy and Sell Stock ---- LeetCode
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 the stock), design an algorithm to find the maximum profit.原创 2015-01-08 11:20:14 · 341 阅读 · 0 评论 -
4Sum --- LeetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie原创 2015-01-09 09:06:24 · 422 阅读 · 0 评论 -
Binary Tree Level Order Traversal II --- LeetCode
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).原创 2015-01-09 10:51:15 · 408 阅读 · 0 评论 -
Binary Tree Inorder Traversal --- LeetCode
Given a binary tree, return the inorder traversal of its nodes' values.原创 2015-01-09 12:09:55 · 407 阅读 · 0 评论 -
Balanced Binary Tree ----- LeetCode
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 never differ by more than 1.原创 2015-01-08 09:59:22 · 385 阅读 · 0 评论 -
3Sum Closest --- LeeCode
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4原创 2015-01-08 10:36:00 · 379 阅读 · 0 评论 -
Binary Tree Preorder Traversal ---- LeetCode
Given a binary tree, return the preorder traversal of its nodes' values.原创 2015-01-09 12:57:31 · 398 阅读 · 0 评论 -
Combinations --- LeetCode
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2015-01-30 09:41:39 · 344 阅读 · 0 评论 -
Combination Sum II --- LeetCode
Combination Sum II Total Accepted: 25244 Total Submissions: 101551My SubmissionsQuestion Solution Given a collection of candidate numbers (C) and a target number (T), find all un原创 2015-01-29 11:35:18 · 406 阅读 · 0 评论 -
Binary Tree Postorder Traversal ---- LeetCode
Given a binary tree, return the postorder traversal of its nodes' values.原创 2015-01-10 09:18:48 · 367 阅读 · 0 评论 -
Binary Search Tree Iterator --- LeetCode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasNext() should run in average O(1) time and uses O(h)原创 2015-01-10 10:38:41 · 697 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal --- LeetCode
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).原创 2015-01-11 09:21:38 · 386 阅读 · 0 评论 -
Climbing Stairs --- LeetCode
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?原创 2015-01-10 09:49:41 · 450 阅读 · 0 评论 -
3Sum---LeetCode
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)The solution原创 2015-01-06 12:44:54 · 330 阅读 · 0 评论