algorithm study
学习不止于前
这个作者很懒,什么都没留下…
展开
-
3sum
given an array s of integers n , 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.code example : /// <summary> /// giv...转载 2018-02-12 11:08:10 · 94 阅读 · 0 评论 -
Majority Element
Given an array of size n,find the majority element .The majority element is the element that appears that appears more than [n/2] times.You may assume that the array is non-empty and the majority elem...翻译 2018-02-22 16:55:52 · 90 阅读 · 0 评论 -
Rotate Array
Rotate an array of n elements to the right by k stepsfor example with n=7 and k=3,the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]Try to come up as many solutions as you can,there are at least...转载 2018-02-22 17:10:46 · 97 阅读 · 0 评论 -
IsPralindrome[字符串是否回文]
Given a string ,determine if it is a palindrome,considering only alphanumeric characters and ignoring cases;根据给定的字符串,判断是否回文,只考虑为字符的情况 忽略大小写code example: public static bool isPralindrome(string e) ...翻译 2018-03-01 16:37:54 · 188 阅读 · 0 评论 -
Binary Tree Right Side View
Given a binary tree,imageine yourself standing on the right side of it,return the values of the nodes you can see orderd from top to bottom 1 <--- / \2 3 <--- \ ...翻译 2018-04-10 14:57:44 · 174 阅读 · 0 评论 -
二叉树的层次遍历
给定一个二叉树,返回每一层上节点的值(从左到右)如: 4 / \ 9 21 / \ 15 7[ [4], [9,21], [15,7]]public class TreeNode { public TreeNode(int value,TreeNode left,TreeNode right) ...原创 2018-04-10 15:00:29 · 78 阅读 · 0 评论 -
相同树
如果2个二叉树的数据结构和对应节点的值都相同,则2棵树是相同树。递归版: public static bool IsSameTree(TreeNode p,TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null)...翻译 2018-04-16 16:19:09 · 153 阅读 · 0 评论 -
对称二叉树
如果以根为中心,整棵树关于根对称的,则返回true,否则为false递归版: public static bool IsSymmetric(TreeNode root) { if (root == null) return true; return IsSymmetric(root.Left,root.Right); ...翻译 2018-04-16 16:33:06 · 138 阅读 · 0 评论 -
反转二叉树
二叉树如图所示: 2 / \ 1 3 / \ / \1 2 6 5反转后结果如下: 2 / \ 3 1 / \ / \5 6 2 1程序如下: public static TreeNode InvertTree(TreeNode root) { Queue<TreeNode...翻译 2018-04-11 16:53:38 · 1178 阅读 · 0 评论 -
判定平衡二叉树
有一棵树,判断是否为平衡二叉树。平衡二叉树即为:左右子树的高度差不超过1代码: /// <summary> /// 若root为平衡二叉树,则返回二叉树的高度,否则返回-1 /// </summary> /// <param name="root"></param> /// <...翻译 2018-04-18 16:17:30 · 136 阅读 · 0 评论 -
FlattenTree
有一颗二叉树,将左子树的所有节点,转移到右子树上,如: 1 1 / \ \ 2 3 2 \ 3代码: /// <summary> /// 将root 中右子树->左子...翻译 2018-04-18 16:55:34 · 733 阅读 · 0 评论 -
在二叉搜索树查找第k大的结点
给定一颗二叉搜索树,查找第k大的结点public static int KthSmallest(TreeNode root, int k) { Stack<TreeNode> s = new Stack<TreeNode>(); TreeNode p = root; while (s.C...翻译 2018-05-10 17:00:00 · 1511 阅读 · 0 评论 -
Candy
There are N children standing in a line.Each child is assigned a rating value.You are giving candies to these children subjuected to the following requirements·Each child must have at least one candy....翻译 2018-02-22 16:45:02 · 146 阅读 · 0 评论 -
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i],You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to itsnex...翻译 2018-02-22 16:21:11 · 136 阅读 · 0 评论 -
threeSumClosed
given an array s of n integers. find three integers in s such that the sum is closed to given a number,target . Return the sum of the three integers. You may assume that each input would have exactly...翻译 2018-02-12 14:38:27 · 125 阅读 · 0 评论 -
fourSum
given an array of n integers ,are there elements a,b,c,and d such that a+b+c+d=target?Find all unique quadruplets in the array which gives the sum of targetnote:Element in a quadruplet must be in non-...翻译 2018-02-12 15:03:18 · 318 阅读 · 0 评论 -
Remove Element
Given an array and a value ,remove all instances of that value in place and return the new lengthcode Example: /// <summary> /// Remove Element from an array /// </sum...翻译 2018-02-12 15:13:46 · 103 阅读 · 0 评论 -
MoveZero
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. /// <summary> /// Given an array nums write a functi...翻译 2018-02-12 15:22:03 · 373 阅读 · 0 评论 -
nextPermutation
Implement next permutation,which rearanges numbers into the lexicographically next greater permutation of numbers.if such arrangement is not possible ,it must rearrange it as the lowest possible order...转载 2018-02-12 16:31:08 · 207 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of eahc bar is 1,compute how much water it is able to trap after raining For example,Given [0,1,0,2,1,0,1,3,2,1,2,1] ,return...转载 2018-02-22 14:26:13 · 142 阅读 · 0 评论 -
ValideSudoku
Dtermine if a Sudoku is valid,according to :Sudoku Puzzles - The Rules http://sudoku.com.au/TheRules.aspx The Sudoku board could be partially filled,where empty cells are filled with the character '.'...转载 2018-02-22 14:30:06 · 126 阅读 · 0 评论 -
Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees(clockwise).Follow up:Could you do this in-place?分析首先想到,纯模拟,从外到内一圈一圈的转,但这个方法太慢。如下图,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次。Figure...转载 2018-02-22 14:47:38 · 123 阅读 · 0 评论 -
PlusOne
Given a number represented as an array of digits,plus one to the number.code example: public static int[] plusOne(int[] digits) { return add(digits, 1); } public...翻译 2018-02-22 15:02:03 · 112 阅读 · 0 评论 -
Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the topEach time you can either climb 1 or 2 steps. In how many distingct ways can you climb to the tio?分析设f(n)表示爬n阶楼梯的不同方法数,为了爬到第n阶楼梯,有两个选择...翻译 2018-02-22 15:24:38 · 203 阅读 · 0 评论 -
Set Matrix Zeroes
给定 mxn阶矩阵,如果一个节点为0,则设置其整行和整列为0code example 1:o(m+n)复杂度 public static void setZeros_1(int [][] matrix) { int m = matrix.Length; int n = matrix[0].Length; b...翻译 2018-02-22 15:56:27 · 101 阅读 · 0 评论 -
链表-插入排序
public class ListNode { public ListNode(int data) { this.data = data; } public int data { set; get; } public ListNo...翻译 2018-05-21 13:46:42 · 77 阅读 · 0 评论