java算法
南瓜王子
这个作者很懒,什么都没留下…
展开
-
java二进制和十进制的互相转换
public static void main(String[] args) { int i =101; int i2=i; i =getBinarySystem(i); System.out.println(i2+"=>转换为二进制:"+i); i2=i; i=getDecimalism(i)...原创 2019-11-04 15:01:15 · 293 阅读 · 0 评论 -
机器人能否返回原点(力扣题库)
public boolean judgeCircle(String moves) { char[] chars = moves.toCharArray(); int x = 0; int y = 0; for (int i = 0; i < chars.length; i++) { if (chars[...原创 2019-03-15 09:58:34 · 113 阅读 · 0 评论 -
汉明距离(力扣题库)
public int hammingDistance(int x, int y) { String xx = Integer.toBinaryString(x); String yy = Integer.toBinaryString(y); if (xx.length()>yy.length()){ int in ...原创 2019-03-15 10:43:26 · 189 阅读 · 0 评论 -
合并二叉树(力扣题库)
public TreeNode mergeTrees(TreeNode t1, TreeNode t2){ TreeNode t=null; if (t1!=null&&t2!=null){ t= new TreeNode(t1.val+t2.val); t=new TreeNode(t1.val...原创 2019-03-15 11:52:00 · 200 阅读 · 0 评论 -
二叉树的最大深度(力扣题库)
public int maxDepth(TreeNode root) { if (root==null){ return 0; } if (root.left!=null&&root.right!=null){ int i1=tansuo(1,root.left); ...原创 2019-03-15 12:35:33 · 198 阅读 · 0 评论 -
Nim游戏(力扣题库)
public boolean canWinNim(int n) {return n%4!=0;}原创 2019-03-20 09:01:06 · 223 阅读 · 0 评论 -
翻转二叉树(力扣题库)
public TreeNode invertTree(TreeNode root) { tansuo(root); return root; } void tansuo(TreeNode node){//探索 if (node!=null){ TreeNode treeNode = node.left; ...原创 2019-03-15 18:32:49 · 212 阅读 · 0 评论 -
数字的补数(力扣题库)
public int findComplement(int num) { String i =Integer.toBinaryString(num); char[] cs=i.toCharArray(); for (int j = 0; j < cs.length; j++) { if (cs[j]=='1'){...原创 2019-03-18 09:07:48 · 131 阅读 · 0 评论 -
山脉数组的峰顶索引(力扣题库)
public int peakIndexInMountainArray(int[] A) { int max=0; for (int i = 0; i < A.length; i++) { if (A[i]>A[max]){ max=i; } } ...原创 2019-03-18 09:57:57 · 110 阅读 · 0 评论 -
删列造序(力扣题库)
public int minDeletionSize(String[] A) { int num =0; for (int i = 0; i < A[0].length(); i++) { for (int j = 0; j < A.length-1; j++) { if (A[j].charAt...原创 2019-03-18 19:02:37 · 112 阅读 · 0 评论 -
按奇偶排序数组||(力扣题库)
public int[] sortArrayByParityII(int[] A) { int i1=0; int i2=1; int[] B = new int[A.length]; for (int i = 0; i < A.length; i++) { if (A[i]%2==0){ ...原创 2019-03-18 19:23:06 · 110 阅读 · 0 评论 -
数字反转(力扣题库)
public static void main(String[] args) { System.out.println(a(-123)); } public static int a(int i){try { StringBuffer stringBuffer = new StringBuffer(i + ""); if (stringBu...原创 2019-03-13 17:23:35 · 348 阅读 · 0 评论 -
N叉树的前序遍历(力扣题库)
递归法class Node { public int val; public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) { val = _val; ...原创 2019-03-19 09:09:24 · 163 阅读 · 0 评论 -
自除数(力扣题库)
public List<Integer> selfDividingNumbers(int left, int right) { List<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) { List<...原创 2019-03-19 09:37:45 · 252 阅读 · 0 评论 -
N叉树的后序遍历(力扣题库)
List<Integer> res = new ArrayList<Integer>(); public List<Integer> postorder(Node root) { if(root == null) return res; for(Node child:root.children)...原创 2019-03-19 10:41:14 · 134 阅读 · 0 评论 -
重复 N 次的元素(力扣题库)
public static int repeatedNTimes(int[] A) {Map<Integer,Integer> map = new HashMap<>();for (int i = 0; i <A.length; i++) { if (map.get(A[i])==null){ map.put(A[i],1)...原创 2019-03-15 09:44:55 · 111 阅读 · 0 评论 -
按奇偶排序数组(力扣题库)
public static int[] sortArrayByParity(int[] A) { int [] ints=new int[A.length]; int i1 = 0; int i2 = 0; for (int i = 0; i < ints.length; i++) { if (A[i]...原创 2019-03-15 09:29:51 · 136 阅读 · 0 评论 -
获取输入的整数数字长度
public int length(int num){int num1=0;while(num>0){//如果还有大于0数字就一直循环num/=10;//让num位数-1num1++;//位数+1}return num1;}原创 2019-01-08 15:24:09 · 2960 阅读 · 0 评论 -
递归写法
int i =10public void recursion(){//这里写需要执行的方法if(0&lt;i){//如果i大于0,进入下一层递归i--; recursion();}//如果不大于0 ,不进入递归,会回到上一个递归}...原创 2019-01-08 15:45:13 · 1969 阅读 · 0 评论 -
获取用户输入内容的时间
public static void main(String[] args) { double time=System.currentTimeMillis()/1000;//获取当前系统时间() input();//用户输入内容 double second=count(time); System.out.println(&quot;您的输入用...原创 2019-01-08 19:50:32 · 777 阅读 · 2 评论 -
不用第三方变量交换两个数字
public void exchangeint(){ int i1=3;//第一个变量 int i2=10;//第二个变量 //把两个变量加在一起放在随便一个变量里面 i1 = i1+i2;//i1=两个变量的和 3+10=13 i1=13 i2 = i1-i2;// i2=10 13-10=3 i2=3 i1 =i1-i2;/...原创 2019-02-20 09:52:46 · 159 阅读 · 0 评论 -
输入一个整数,求出各位上的和
public static void main(String[] args) { System.out.println( countsum(12345)); } public static int countsum(int i){ int sum=0; while (i&amp;gt;0){ sum+=i%10;//每...原创 2019-02-20 10:06:44 · 274 阅读 · 0 评论 -
字母大小写互相转换
public static void main(String[] args) { //输入一个或者多个字母 System.out.println(interconversion("ADASDsadadSS")); System.out.println(capital("asada")); System.out.println(lowe...原创 2019-02-20 10:36:36 · 704 阅读 · 0 评论 -
增减字符串匹配(力扣题库)
public static void main(String[] args) { int i [] =diStringMatch("IDID"); for (int j = 0; j < i.length; j++) { System.out.print(i[j]+"-"); } } public s...原创 2019-03-14 15:27:32 · 142 阅读 · 0 评论 -
有序数组的平方(力扣题库)
public static void main(String[] args) { System.out.println(sortedSquares(new int[]{-1,1,10,20}));}public static int[] sortedSquares(int[] A){ int[] B = new int[A.length]; for (int i =...原创 2019-03-14 15:29:11 · 141 阅读 · 0 评论 -
独特的电子邮件地址(力扣题库)
public static void main(String[] args) { String[] emails = new String[]{"test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"}; System...原创 2019-03-14 15:30:16 · 636 阅读 · 0 评论 -
宝石与石头(力扣题库)
public static void main(String[] args) { String k = "ab";//宝石 String j = "bb";//石头 int num =count(k,j); //查找宝石里面一共有多少石头的方法 System.out.println(num); } ...原创 2019-03-14 15:33:17 · 241 阅读 · 0 评论 -
字符串反转(力扣题库)
public static void main(String[] args) { reverse(new char[]{'a','b','c'}); } public static void reverse(char[] s){ for (int i = 0; i <(int)(s.length/2); i++) { ...原创 2019-03-14 15:35:15 · 320 阅读 · 0 评论 -
唯一摩尔斯密码词(力扣题库)
public static void main(String[] args) { uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"});}public static int uniqueMorseRepresentations(String[] words){ Map<String,St...原创 2019-03-14 16:52:35 · 123 阅读 · 0 评论 -
反转图片(力扣题库)
public static void main(String[] args) { flipAndInvertImage(new int[][]{{1,1,0},{1,0,1},{0,0,0}}); } public static int[][] flipAndInvertImage(int[][] A) { for (in...原创 2019-03-14 19:40:19 · 170 阅读 · 0 评论 -
删除链表中的节点(力扣题库)
public class ListNode {int val;ListNode next;ListNode(int x) { val = x; }}class Solution {public void deleteNode(ListNode node) {node.val = node.next.val;node.next = node.next.next;}}原创 2019-03-14 21:13:47 · 231 阅读 · 0 评论 -
使用java递归来完成一个n位数的数组用m个数拼接没有重复
public static void main(String[] args) {int[] a = new int[]{1,2,3,4,5,6};//原来的元素int m = 3;//组合元素数量int result[] = new int[m];//保存结果show(a,0, result, m,result.length, a.length);}// 如果是输出3位数 ,...原创 2019-01-04 19:53:09 · 372 阅读 · 0 评论