- 博客(211)
- 收藏
- 关注
原创 Sorted Merge java 走地牙 CTCI 10.1
public class Solution { /* * @param A: sorted integer array A which has m elements, but size of A is m+n * @param m: An integer * @param B: sorted integer array B which has n elemen...
2019-04-27 04:27:01
163
原创 Boolean Evaluation Java 走地牙 CTCI 8.14
Boolean Evaluation:Given a boolean expression consisting of the symbols 0 (false), 1 (true), & (AND), I (OR), and /\ (XOR), and a desired boolean result value result, implement a function to coun...
2019-04-25 02:54:44
219
原创 Eight Queens Java 走地牙 CTCI 8.12
package com.company;import java.util.*;/** Eight Queens:Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board* so that none of them share the same row, column, or d...
2019-04-21 23:09:57
198
原创 Coins Java 走地牙 CTCI 8.11
package com.company;/** Coins: Given an infinite number of quarters (25 cents), dimes (1O cents),* nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of represent...
2019-04-20 21:35:59
187
原创 Paint Fill java 走地牙 CTCI 8.10
package com.company;/** Paint Fill: Implement the "paint fill" function that one might see on many image editing programs.* That is, given a screen (represented by a two-dimensional array of color...
2019-04-18 04:05:21
204
原创 Parens/Generate Parentheses java 走地牙 CTCI 8.9
Parens:Implement an algorithm to print all valid (i.e., properly opened and closed) combinations of n pairs of parentheses.public class Solution { /** * @param n: n pairs * @return...
2019-04-17 03:17:04
163
原创 Permutations with Duplicates java 走地牙 CTCI 8.8
package com.company;import java.util.*;/* * Permutations with Duplicates: Write a method to compute all permutations * of a string whose characters are not necessarily unique. The list of permuta...
2019-04-16 07:41:15
140
原创 Permutations without Dups java 走地牙CTCI 8.7
import java.util.*;/** Permutations without Dups: Write a method to compute all permutations of a string of unique characters.* */public class Main { public static void main(String[] args) ...
2019-04-15 00:11:53
126
原创 Towers of Hanoi 递归和构建 java 走地牙 CTCI 8.6
Towers of Hanoi:In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order ...
2019-04-14 03:48:06
298
原创 Recursive Multiply java 走地牙 CTCI 8.5
/** Recursive Multiply: Write a recursive function to multiply two positive integers without using the * operator* (or / operator). You can use addition, subtraction, and bit shifting, but you shou...
2019-04-13 02:09:04
153
原创 Power Set/subset1/subset2/ java走地牙 CTCI 8.4
Power Set:Write a method to return all subsets of a set.subset 1;没有重复数字;public class Solution { /** * @param nums: A set of numbers * @return: A list of lists */ public Lis...
2019-04-12 04:46:57
206
原创 Magic Index java 走地牙 CTCI 8.3
/** Magic Index: A magic index in an array A[ 1.•.n-1] is defined to be an index such that A[ i]i. Given a sorted array of distinct integers, write a method to find a magic index, if one exists, in...
2019-04-10 03:19:57
121
原创 Robot in a Grid: java 走地牙 CTCI 8.2
Robot in a Grid:Imagine a robot sitting on the upper left corner of grid with r rows and c columns.The robot can only move in two directions, right and down, but certain cells are "off limits" such t...
2019-04-09 22:21:09
171
原创 Triple Step java 走地牙 8.1 CTCI
Triple Step:A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs...
2019-04-09 09:20:15
160
原创 The Egg Drop Problem java走地牙CTCI 6.8
public class Main { public static void main(String[] args) { System.out.println("Hello World!"); System.out.println(findBreakPoint()); } public static int findBreakPoint...
2019-04-01 22:53:41
256
原创 Conversion java 走地牙CTCI 5.6
/*5.6 Conversion: Write a function to determine the number of bits you would need to flip to convertinteger A to integer B.EXAMPLEInput: 29 (or: 11101), 15 (or: 01111) Output: 2*/public class M...
2019-03-30 03:11:00
188
原创 Next Number java 走地牙CTCI 5.4
/*Next Number: Given a positive integer, print the next smallest and the last largestnumber that have the same number of 1 bits in their binary representation.方法是:1:最小下一个:11011001111100 相同数量的1的情况下...
2019-03-29 04:52:55
185
原创 Binary to String java 走地牙 CTCI 5.2
/*Binary to String: Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double,print the binary representation. If the number cannot be represented accurately in binary with at ...
2019-03-26 03:39:37
192
原创 Insertion java 走地牙 CTCI 5.1
/*Insertion: You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert Minto N such that M starts at bit j and ends at bit i. You can assume that the bits...
2019-03-26 03:06:13
122
原创 Random Node java 走地牙 CTCI 4.11
import java.util.*;public class TreeNode { private int val; public TreeNode left, right; private int size = 0; public TreeNode(int val) { this.val = val; size = 1;...
2019-03-25 08:01:45
205
原创 Paths with Sum java 走地牙CTCI 4.12 有坑
/*不能这样算 1 2 3 4 假设这样的tree target 是6 用下面的算法 2-4 这个组合会被算两变应该用的方法: 写一个方法 只算每次剪掉root.val的值,另外再把没剪掉的重新递归; 看下面的code;class Solution { public int pathSum(TreeNode root, int...
2019-03-25 04:01:09
151
原创 BST Sequences java 走地牙CTCI 4.9 结果会输出四遍 不知道原因!!!!!
/*4.9 BST Sequences: A binary search tree was created by traversing through an array from left toright and inserting each element. Given a binary search tree with distinct elements, print allpossi...
2019-03-24 04:45:26
251
原创 Check Subtree java 走地牙 CTCI 4.10
/*CTCI 4.10 有陷阱 注意可能会有重复的val 导致不能分部递归,为了避免重复 使用嵌套递归;*//*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNo...
2019-03-24 04:44:03
142
原创 Lowest Common Ancestor III java 走地牙
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left =...
2019-03-22 06:29:57
156
原创 Lowest Common Ancestor II java 走地牙
/*** Definition of ParentTreeNode:** class ParentTreeNode {* public ParentTreeNode parent, left, right;* }*//*与1不同的是多了一个子向父的指针 先找其中一个node到root的所有父节点 然后用另外一个遍历 找到相同的节点为止;*/public...
2019-03-22 03:15:22
161
原创 Lowest Common Ancestor of a Binary Tree java 走地牙 CTCI 4.8
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left =...
2019-03-22 03:04:19
121
原创 Course Schedule II java 走地牙 CTCI 4.7 变形
类似4.7 需要变化一下形式 4.7中课程不是数字 先构造graph 然后在操作现在看起来有点麻烦以后再实现/*想写一个通用一点的 因为course不一定是数字 所以建立两个map 一个用于储存课程 与他需要的前置课程数量,另一个储存课程 与他后置的课程list 这样 利用bfs 每次把当前课程后置list的课程找出来 将前置课程已经为0的课程放置到queue里面 最终可以找出上课...
2019-03-21 04:24:53
134
原创 Validate BST: java 走地牙CTCI 4.5
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left =...
2019-03-15 03:26:41
192
原创 Check Balanced java 走地牙CTCI 4.4
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left =...
2019-03-15 03:01:24
165
原创 Binary Tree Level Order Traversal II java走地牙CTCI 4.3变形
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left =...
2019-03-14 23:34:01
136
原创 Minimal Tree java 走地牙 CTCI 4.2
import java.util.*;public class Main { public static void main(String[] args) { System.out.println("Hello World!"); int[] nums = new int[] {1,2,3,4,5,6,7,8,12,34,67,89,111,444}...
2019-03-13 08:11:19
187
原创 Route Between Nodes java 走地牙 CTCI 4.1
/*** Definition for Directed graph.* class DirectedGraphNode {* int label;* ArrayList<DirectedGraphNode> neighbors;* DirectedGraphNode(int x) {* label = x;* ...
2019-03-13 03:26:40
143
原创 Animal Shelter 走地牙 java CTCI 3.6
import java.util.*;public class Main { public static void main(String[] args) { System.out.println("Hello World!"); Animal a = new Cat("lili"); Animal b = new Dog("lala...
2019-03-11 03:20:16
226
原创 Sort Stack java 走地牙 CTCI 3.5
/*思路:栈1(5 — 8 - 3 - 1) 栈2(7 - 6) 第一步 拿出5 栈1(8 3 1) 栈2(7 6) 第二步 那5和栈2里面的数比较大于5的pop出来存入栈1,栈1(6 7 8 3 1) 栈2(5)依次弄下去 直到栈1为空*/import java.util.*;public class Main { public static void main(Str...
2019-03-08 04:09:12
242
原创 Implement Queue by Two Stacks java 走地牙 CTCI 3.4
public class MyQueue { private Stack<Integer> stackIn, stackOut; public MyQueue() { // do intialization if necessary stackIn = new Stack(); stackOut = new St...
2019-03-07 23:55:58
162
原创 Min Stack java 走地牙 CTCI 3.2
/*建立两个栈 分别存放正常的数据和最小的数据,用来存放最小数据的栈 每一层都存放上一层最小与新存放数中最小的那个,*/public class MinStack { private Stack<Integer> stack; private Stack<Integer> minStack; public MinStack() {...
2019-03-06 22:06:12
193
原创 Linked List Cycle II java 走地牙 CTCI 2.8
/*思路:1 判断是否有循环。 2 用快慢指针找到循环重合的节点。 3 找出循环开始的节点;证明 2步骤找到的节点 距离 循环开始的节点与list开头的节点 距离 循环开始的节点相等证: 快节点是慢节点速度的两倍, 1 设开始到第一个循环距离k 设 第一个节点到相遇的节点距离是x。 n是循环的圈数 r是循环内节点的个数。 2 2*(k+x)= k + n*r 整理可得 k+x = n*r, ...
2019-03-05 05:28:19
216
原创 Palindrome Linked List java 走地牙 2.6 CTCI
/*思路: 1.找到中间点 2.以中间点开始翻转整个后面的list;3.以第二个list为限度 对比两个list 得出结果;*//*** Definition for ListNode* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* v...
2019-03-05 03:55:56
233
原创 Add Two Numbers II java 走地牙 CTCI 2.5
/*** Definition for ListNode* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/public class Sol...
2019-03-04 10:12:16
403
原创 Partition: java 走地牙 CTCI 2.4
把list分成两个 一个before 一个after 最后在合并;import java.util.*;public class Main { public static ListNode partition(ListNode node, int part) { ListNode head = new ListNode(0); ListNode ...
2019-03-02 03:54:00
283
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人