leetcode2
来源:互联网 发布:鹏博士收购中信网络 编辑:程序博客网 时间:2024/06/05 21:18
1.判断字符串是否是回文数
public class Solution { public boolean isPalindrome(String s) { if(s ==null || s.length() ==0) return true; int start = 0; int last = s.length()-1; while(start<last){ while(!isLetterOrNumber(s.charAt(start))&& start<last) start++; while(!isLetterOrNumber(s.charAt(last)) && start<last) last--; if(Character.toLowerCase(s.charAt(start))!=Character.toLowerCase(s.charAt(last))){ return false; } start++; last--; } return true; } public boolean isLetterOrNumber(char i){ if(i>='0'&& i<='9' || i>='a'&& i<='z' || i>='A' && i<='Z') return true; return false; }}2.二叉树最大路径和
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { int maxsum = Integer.MIN_VALUE; //初始化最小值 public int maxPathSum(TreeNode root) { if(root ==null) return 0; maxSum(root); return maxsum; } public int maxSum(TreeNode root){ if(root==null) return 0; int left = maxSum(root.left); int right = maxSum(root.right); int sum = root.val; if(left>0) sum+=left; if(right>0) sum+=right; if(maxsum<sum) maxsum = sum; return Math.max(left, right) > 0 ? Math.max(left, right) + root.val : root.val; }}
3获取杨辉三角的第k行
import java.util.*;public class Solution { public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> list = new ArrayList<Integer>(); if(rowIndex < 0) return list; if(rowIndex ==0){ list.add(1); return list; } int num[][] = new int[rowIndex+1][rowIndex+1]; for(int i=0;i<rowIndex+1;i++){ num[i][0] = 1; num[i][i] = 1; for(int j=1;j<i;j++){ num[i][j] = num[i-1][j] + num[i-1][j-1]; } } for(int i=0;i<rowIndex+1;i++){ list.add(num[rowIndex][i]); } return list; }}
import java.util.*;public class Solution { public ArrayList<ArrayList<Integer>> generate(int numRows) { ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); if(numRows == 0) return lists; int num[][] = new int[numRows][numRows]; for(int i=0;i<numRows;i++){ num[i][0] = 1; num[i][i] = 1; for(int j=1;j<i;j++){ num[i][j] = num[i-1][j]+ num[i-1][j-1]; } } for(int i=0;i<numRows;i++){ ArrayList<Integer> list = new ArrayList<>(); for(int j=0;j<numRows;j++){ if(num[i][j]>0) list.add(num[i][j]); } lists.add(list); } return lists; }}
5.判断是否平衡二叉树
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(root ==null) return true; int left = depth(root.left); int right = depth(root.right); if(Math.abs(left-right)>1) return false; boolean boolleft = isBalanced(root.left); boolean boolright = isBalanced(root.right); return boolleft&&boolright; } public int depth(TreeNode root){ if(root ==null) return 0; int left = depth(root.left); int right=depth(root.right); return (left>right)?(left+1):(right+1); }}
6,将升序的单项链表转为二叉搜索树
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; next = null; } * } *//** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode sortedListToBST(ListNode head) { if(head ==null) return null ; if(head.next==null) return new TreeNode(head.val) ; ListNode mid = head; ListNode end = head; ListNode premid = null; while(end!=null&&end.next!=null){ premid = mid; mid = mid.next; end = end.next.next; } TreeNode root = new TreeNode(mid.val); premid.next = null; root.left = sortedListToBST(head); root.right = sortedListToBST(mid.next); return root; }}
7.排序数组转为二叉搜索树
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode sortedArrayToBST(int[] num) { int len = num.length; if(len<=0) return null; return sorted(num,0,len-1); } public TreeNode sorted(int[] num,int start,int end){ if(start>end) return null; int mid = (start+end)/2 +(start+end)%2; TreeNode root = new TreeNode(num[mid]); root.left = sorted(num,start,mid-1); root.right =sorted(num,mid+1,end); return root; }}
8,将二叉树按Z层次输出
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */import java.util.*;public class Solution { public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); if(root ==null) return lists; Queue<TreeNode> q = new LinkedList<TreeNode>(); q.add(root); int i=1; while(!q.isEmpty()){ int n = q.size(); ArrayList<Integer> list = new ArrayList<>(); while(n>0){ TreeNode cur= q.poll(); if(cur.left!=null) q.add(cur.left); if(cur.right!=null) q.add(cur.right); list.add(cur.val); n--; } i++; if(i%2!=0){/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */import java.util.*;public class Solution { public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); if(root==null) return lists; Queue<TreeNode> q =new LinkedList<TreeNode>(); q.add(root); while(!q.isEmpty()){ ArrayList<Integer> list = new ArrayList<>(); int n = q.size(); while(n>0){ TreeNode cur = q.poll(); if(cur.left!=null) q.add(cur.left); if(cur.right!=null) q.add(cur.right); n--; list.add(cur.val); } lists.add(list); } return lists; }}
for(int j=0;j<list.size()/2;j++){ int temp = list.get(j); list.set(j, list.get(list.size()-j-1)); list.set(list.size()-j-1, temp); } } lists.add(list); }return lists; }}
阅读全文
0 0
- leetcode2
- Leetcode2
- leetcode2
- leetcode2
- LeetCode2 AddTowNumbers
- leetcode2 multi_string
- LeetCode2-AddTwoNumbers
- leetcode2:inorder tranversal
- [LeetCode2]Add Two Numbers
- leetcode2 add two numbers
- LeetCode2 Add Two Numbers
- Leetcode2: Rotate Array
- leetcode2 two sum
- leetcode2.1.8.3sum
- LeetCode2.1.10(4Sum)
- LeetCode2.1.11(Remove Element)
- LeetCode2.1.12(Next Permutation)
- LeetCode2.1.16(Rotate Image)
- c写文件
- 实习的一些感想
- 《Python学习手册》学习笔记(17)之第17章作用域(关键词:编程语言/Python/作用域/global/nonlocal)
- 用py-faster-rcnn训练自己的VOC数据集
- bzoj 4443: [Scoi2015]小凸玩矩阵
- leetcode2
- 51node 1351 吃点心
- 10月7日 c语言 输入一串字符,单词之间用空格隔开,统计其中有多少个单词
- 《Python学习手册》学习笔记(18)之第18章参数(关键词:编程语言/Python/参数)
- 使用ZooKeeper实现Java跨JVM的分布式锁(读写锁)
- hdu1213 How Many Tables
- Gym
- 《Python学习手册》学习笔记(19)之第19章函数的高级话题(关键词:编程语言/Python/递归函数/匿名函数)
- Qt 学习之路 2(75):线程总结