- 博客(39)
- 收藏
- 关注
原创 顺时针打印矩阵(不一定是方阵)
public ArrayList<Integer> printMatrix(int [][] matrix) { ArrayList<Integer> list=new ArrayList<Integer>(); int row=matrix.length; int col=matrix[0].length; ...
2018-05-09 10:50:01
309
原创 丑数
public int GetUglyNumber_Solution(int index) { if(index<7) return index; int[] ret = new int[index]; ret[0]=1; int t2=0,t3=0,t5=0; for(int i=1;i<index;i++) { ...
2018-05-05 22:03:42
156
原创 连续子序列最大和
1. 时间复杂度也为 n 。从头开始累加数组的元素和,之和若小于0,则舍弃之前的数,当前最大和重置0;之和与当前数比较,之和若小于当前数,则舍弃之前的数,当前最大和置为当前数。 int sum=array[0]; //最大和 int cur=array[0]; //累加值 for(int i=1;i<array.length;i++){ ...
2018-05-04 21:34:06
177
原创 二进制中1的个数
//如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。 public int NumberOf1(int n) { int count=0; while(n!=0){ count++; ...
2018-04-22 21:44:28
162
原创 递归
//青蛙跳台阶:先找出地推公式,在递归编程实现public class Solution { public int JumpFloor(int target) { return recur(target); } public int recur(int n){ if(n==1){ return 1; } ...
2018-04-10 19:15:43
116
原创 第二个序列是否为第一个序列的栈弹出顺序
/*当出栈序列popA[j]没匹配完 且 栈s不空 时, * (1)popA[j]==s 匹配,出栈,j++ * (2)popA[j]!=s 不匹配,1)入栈序列空了,return false; * 2)入栈序列不空 pushA[i]入栈. * */ public static boolean IsPopOrder(int [] pushA,int [] popA) {...
2018-03-22 10:14:11
213
原创 用两个栈来实现一个队列
Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } pub...
2018-03-21 22:13:51
134
原创 找出和为sum的两个数
import java.util.ArrayList;/* * 一组递增序列中,找出和为sum的两个数,若有多组这样的数,找出乘积最小的这两个数 * 如(1,2,3,5,6,7),和为8,则返回1,7 * */import 数组.FindNumAppearOnce;public class SumisS { public static void main(String[] args) { int...
2018-03-18 20:20:50
326
原创 连续n个数的和为sum
import java.util.ArrayList;/*连续n个数的和为sum *从m到n的连续序列和为(m+n)(n-m+1)/2=sum * */public class SumSseq { public static void main(String[] args) { ArrayList<ArrayList<Integer>> list1=getsum(100)...
2018-03-18 20:19:34
574
原创 中序遍历的下一个结点
/*1.该节点右子树不为空,下一结点为右子树最左结点2.该节点右子树为空且为左孩子3.该节点右子树为空且该父节点的分支为左分支 * */class TNode{ TNode left=null; TNode right=null; TNode parent=null; int val; TNode(int val){ this.val=val; }
2017-12-27 10:50:57
185
原创 二叉树序列化与反序列化
//序列化 public static String Serialize(TreeNode root) { String s=""; if(root==null)return s; LinkedList list=new LinkedList(); list.add(root); while(!list.
2017-12-27 09:32:29
140
原创 蛇形打印二叉树
package 树;import java.util.ArrayList;import java.util.Deque;import java.util.LinkedList;//蛇形打印二叉树public class Printerzigzag { public static void main(String[] args) { treeNode root=new
2017-12-20 15:20:49
788
原创 判断是否为平衡二叉树
public boolean IsBalanced_Solution(TreeNode root) { if(root==null)return true; //空结点是平衡的 int left=treedepth(root.left); //左子树的深度 int right=treedepth(root.right); //右子树的深度
2017-12-18 21:07:12
173
原创 重建二叉树(根据前序和中序遍历结果)
public TreeNode reConstructBinaryTree(int [] pre,int [] in) { return build(pre,0,pre.length-1,in,0,in.length-1); } public TreeNode build(int[]preorder,int start1,int end1,int[]inord
2017-12-18 16:50:11
286
原创 01背包图解
01背包01背包(ZeroOnePack): 有N种物品和一个容量为V的背包。(每种物品均只有一件)第i个物品的重量是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。这种背包问题的特点是:每种物品仅有一件,可以选择放或不放。基于以上描述,定义我们的子问题:f[v]表示种子用户i分流给请求某种多媒体流用户数x的最小化能耗(最大化能耗分之一)。蜂窝网可看做单位能耗最大的一个种
2017-12-13 16:48:34
597
原创 把两个有序链表合并为一个有序链表(注意空指针异常!)
public static Node Merge(Node list1,Node list2) { if(list1==null)return list2; if(list2==null)return list1; Node pre1=null; Node nhead=new Node(-1);
2017-12-11 21:20:52
309
原创 两链表相交问题
/*两链表相交问题1.一个有环一个无环,则不相交2.两个都无环{有两种情况:有无交点}3.两个都有环{ 1环入口结点相同 2环入口结点不同(有两种情况:有无交点)}*/public static Node isIntersect(Node head1,Node head2){ if(head1==null||head2==null)retu
2017-12-08 21:40:49
181
原创 链表是否存在环,有则返回入环结点
public static Node entryNode(Node head){ if(head==null)return null; Node p=isloop(head); //环中某一节点 Node q=p.next; //计算环中结点个数 int count=1; //环的结点总数量count while(q!=p){ q=q.next;
2017-12-08 21:38:33
145
原创 判断是否为回文结构
法一:栈 时间O(n)+空间O(n)public static boolean ispanlindrome(Node head){ if(head==null||head.next==null)return true; Node first=head;//快指针 Node second=head;//慢指针 while(first.next!=null&&firs
2017-12-08 17:31:35
314
原创 找到链表中倒数第k个结点
public static Node findK(Node head,int k){if(head==null&&k==0)return null;Node first=head;//快指针Node second=null;//慢指针for(int i=0;iif(first.next!=null){first=first.next;}else{return n
2017-12-08 17:26:21
144
原创 将单链表的每k个节点之间逆序
public static Node reversek(Node head,int k){if(kNode cur=head;//当前结点Node next=null; //下一结点Node pre=null;//上一组数逆序后的最后一个Node start=null;//本组开始结点int count=1;while(cur!=null){next=cur
2017-12-06 09:37:34
563
原创 单链表插入排序
public static Node Insertsort(Node head){if (head==null||head.next==null) return head;Node pre=new Node(-1);pre.next=head;Node nhead=pre;Node p=head;Node q=head.next;while (q!=null){
2017-12-04 21:00:01
190
原创 把单链表按某值划分成左边小,中间相等,右边大的形式,时间O(n),空间O(1)
public static Node partation(Node list,int k){Node head=list;Node sf=null,se=null;//小的头,尾Node ef=null,ee=null;//相等的头,尾Node bf=null,be=null;//大的头,尾while(head!=null){Node next=head.next;//把头
2017-12-04 16:36:58
307
原创 单链表选择排序
法一:public static Node sort(Node list){Node end =null;Node head=null;while(list!=null){Node cur=list;//指向当前值Node pre=null;//指向当前值前一个Node p=list; //指向最小值Node prep=null;//指向最小值前一个whi
2017-12-04 16:21:00
371
原创 求数组中只出现一次的数,其他数都出现三次
package 数组;//求数组中只出现一次的数,其他数都出现三次public class Find2numAppear {public static void main(String[] args) {int[]a={1,1,3,1,3,3,4,4,2,4};int num=find(a);System.out.println(num);}public stati
2017-11-30 21:26:01
858
原创 找出数组中出现一次的三个数,其他数都出现两次
package 数组;/*找出数组中出现一次的三个数,其他数都出现两次讲解链接:http://zhedahht.blog.163.com/blog/static/25411174201283084246412/f(x^a)^f(x^b)^f(x^c)结果最后一位为1的位数可以找到第一个出现一次的数*/public class Find3numAppearOnce {publ
2017-11-30 20:38:05
726
原创 统计一个数字在排序数组中出现的次数
public class Solution { public int GetNumberOfK(int [] array , int k) {int len=array.length; int num=0;if(array.length>0&&array!=null) {int firstk=findfirst(array,k,0,len-1);int l
2017-11-30 09:26:45
155
原创 第一个只出现一次的字符
package 数组;import java.util.HashMap;public class FirstAppearOnce {public static void main(String[] args) {String s="abaccdebff";char[]c=s.toCharArray();HashMap map=new HashMap();for(ch
2017-11-28 21:26:00
142
原创 java中与或非,异或,位运算
//1、&= 与运算二进制中,只有相同才是1 a=2; a&=3;(a=a&3) 同2&3;结果都是2 [java] view plain copy//2、|= 非运算 二进制中只要一个为1就为1 a = 4; b = 2; a |= b; (a
2017-11-28 20:36:20
4110
1
原创 数组中只出现一次的两个数(异或)
package 数组;public class FindNumAppearOnce { static int num1,num2;public static void main(String[] args) {int[]a={3,2,3,4,5,5,6,4};findtwoNum(a,num1,num2);}public static void findtwoN
2017-11-28 20:21:23
167
原创 数组中的逆序数(用到归并排序)
package 数组;public class InversePairs {static int count=0;public static void main(String[] args) {int[]a={4,2};Sortmerge(a,0,a.length-1);System.out.println(count);}public static void
2017-11-28 15:59:19
182
原创 旋转数组的最小数
package 数组;public class Min {public static void main(String[] args) {int[]a={4,5,6,7,8,1,2,3};System.out.println(find(a));}public static int find(int[]a){int index1=0;int index2=a.le
2017-11-27 20:34:11
95
原创 把数组排成最小的数
package 数组;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class PrintMinNumber {public static void main(String[] args) {int[]num={32,3,321};
2017-11-26 21:32:39
113
原创 字符串的所有组合(非空子集)
package 字符串;//字符串的所有组合(非空子集)import java.util.ArrayList;public class AllCombination { public static void main(String[] args) { char []s={'a','b','c','d'}; ArrayList arr=new ArrayLi
2017-11-26 20:56:08
1649
原创 字符串的全排列
package 字符串;import java.lang.reflect.Array;//字符串的全排列public class PermutationString {public static void main(String[] args) {char[]array={'a','b','c'};int len=array.length;permutation(arr
2017-11-26 11:39:33
131
原创 遍历List,Set的方法
一:ArrayList,LinkedList,Vector遍历方法一样 List list = new ArrayList(); //方法1 Iterator it1 = list.iterator(); while(it1.hasNext()){ System.out.println(it1.next()
2017-11-25 21:28:58
256
原创 最小的k个数
package 数组;//法一:基于快排partition函数,时间复杂度为O(n)public class SmallerKnumber {public static void main(String[] args) {int[]a={2,4,5,46,32,42,7};int k=5;int start=0,end=a.length-1;int index=part
2017-11-25 20:38:28
121
原创 JDBC连接数据库mysql
导入jar包:mysql-connector-java-5.0.4-bin.jarlog4j-1.2.16.jar (在JDBC->Build path->add external ...)建数据库工具类:测试类:
2017-11-15 21:29:44
146
原创 mysal命令行版安装及配置出现的问题
安装教程https://jingyan.baidu.com/article/597035521d5de28fc00740e6.html安装时出现的问题:错误1.要在c:\windows\system32目录下以管理员身份运行cmd:,不然安装成功之后输入net start mysql显示'net'不是内部或外部命令...错误2.输入net start mysql显示正在启动,启动失
2017-11-14 21:13:26
640
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人