题五十一 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
public class Solution {
public boolean isNumeric(char[] str) {
String res = String.valueOf(str);
boolean result = res.matches("[\\+-]?[0-9]*(\\.[0-9]+)?([eE][\\+-]?[0-9]+)?");
return result;
}
}
import java.util.*;
public class Solution {
HashMap map = new HashMap<>();
ArrayList list = new ArrayList<>();
//Insert one char from stringstream
public void Insert(char ch)
{
if(map.containsKey(ch)) map.put(ch,map.get(ch)+1);
else map.put(ch,1);
list.add(ch);
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{ char res = '#';
for(char ch:list){
if(map.get(ch) == 1){
res = ch;
break;
}
}
return res;
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode meetingNode(ListNode head){
if(head == null || head.next == null) return null;
ListNode slow = head.next;
ListNode fast = slow.next;
while(slow != null && fast != null){
if(slow == fast) return fast;
slow = slow.next;
fast = fast.next;
if(fast != null) fast = fast.next;
}
return null;
}
public ListNode EntryNodeOfLoop(ListNode pHead)
{
ListNode meet = meetingNode(pHead);
if(meet == null) return null;
int cnt =1;
ListNode p1 = meet;
while(p1.next != meet){
p1 = p1.next;
cnt++;
}
p1 = pHead;
for(int i=0; i< cnt; i++){
p1 = p1.next;
}
ListNode p2 = pHead;
while(p1 != p2){
p1 = p1.next;
p2 = p2.next;
}
return p2;
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null || pHead.next == null) return pHead;
ListNode tmp = new ListNode(-1);
tmp.next = pHead;
ListNode curr = pHead;
ListNode pre = tmp;
while(curr != null && curr.next != null){
ListNode nxt = curr.next;
if(curr.val == nxt.val){
while(nxt != null && curr.val == nxt.val) nxt = nxt.next;
pre.next = nxt;
curr = nxt;
}
else {
pre = curr;
curr = curr.next;
}
}
return tmp.next;
}
}
题五十五 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode == null ) return null;
if(pNode.right != null){
pNode = pNode.right;
while(pNode.left != null) pNode = pNode.left;
return pNode;
}
while(pNode.next != null){
if(pNode == pNode.next.left ) return pNode.next;
pNode = pNode.next;
}
return null;
}
}
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot == null) return true;
return help(pRoot.left,pRoot.right);
}
boolean help(TreeNode left,TreeNode right){
if(left == null && right == null) return true;
if(left != null && right != null){
return(left.val == right.val && help(left.left,right.right)&&help(left.right,right.left));
}
return false;
}
}
import java.util.ArrayList;
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList > Print(TreeNode pRoot) {
ArrayList > result = new ArrayList > ();
if(pRoot == null) return result;
Deque q = new LinkedList();
q.offer(pRoot);
int layer = 0;
while(!q.isEmpty()){
layer++;
ArrayList res = new ArrayList();
int curr = 0, size = q.size();
if((layer &1 ) == 0){
Iterator it = q.descendingIterator();
while(it.hasNext()){
res.add(it.next().val);
}
}else{
Iterator it = q.iterator();
while(it.hasNext()){
res.add(it.next().val);
}
}
while(curr
题五十八 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
import java.util.ArrayList;
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList > Print(TreeNode pRoot) {
ArrayList > result = new ArrayList > ();
if(pRoot == null) return result;
Deque q = new LinkedList();
q.offer(pRoot);
int layer = 0;
while(!q.isEmpty()){
layer++;
ArrayList res = new ArrayList();
int curr = 0, size = q.size();
Iterator it = q.iterator();
while(it.hasNext()){
res.add(it.next().val);
}
while(curr
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
String Serialize(TreeNode root) {
if(root == null ) return "#,";
String res = root.val +",";
res += Serialize(root.left);
res += Serialize(root.right);
return res;
}
TreeNode Deserialize(String str) {
String[] values = str.split(",");
Queue q = new LinkedList<>();
for(int i =0; i!= values.length;i++){
q.offer(values[i]);
}
return preOrder(q);
}
TreeNode preOrder(Queue q){
String s = q.poll();
if(s.equals("#")) return null;
TreeNode node = new TreeNode(Integer.valueOf(s));
node.left = preOrder(q);
node.right = preOrder(q);
return node;
}
}
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot == null || k <= 0) return null;
TreeNode p = pRoot;
Stack stack = new Stack<>();
int cnt = 1;
while(p!= null || !stack.isEmpty()){
while(p!= null){
stack.push(p);
p = p.left;
}
TreeNode tmp = stack.pop();
if(cnt++ == k) return tmp;
p = tmp.right;
}
return null;
}
}
import java.util.*;
public class Solution {
int cnt;
PriorityQueue minHeap = new PriorityQueue();
PriorityQueue maxHeap = new PriorityQueue(11,new Comparator(){
public int compare(Integer t1,Integer t2){
return t2.compareTo(t1);
}
});
public void Insert(Integer num) {
cnt++;
if((cnt & 1) == 0){
if(!maxHeap.isEmpty() && num < maxHeap.peek()){
maxHeap.offer(num);
num = maxHeap.poll();
}
minHeap.offer(num);
}else{
if(!minHeap.isEmpty() && num > minHeap.peek()){
minHeap.offer(num);
num = minHeap.poll();
}
maxHeap.offer(num);
}
}
public Double GetMedian() {
if(cnt == 0)
throw new RuntimeException("no available number");
double res;
if((cnt & 1)== 1) res = maxHeap.peek();
else res = (minHeap.peek() + maxHeap.peek())/2.0;
return res;
}
}
import java.util.*;
public class Solution {
public ArrayList maxInWindows(int [] num, int size)
{
ArrayList res = new ArrayList();
if(size == 0) return res;
Deque deq = new LinkedList();
for(int i=0; i deq.peekFirst()) deq.pollFirst();
while((!deq.isEmpty())&& num[deq.peekLast()]<=num[i]){
deq.pollLast();
}
deq.offerLast(i);
if( begin>= 0) res.add(num[deq.peekFirst()]);
}
return res;
}
}
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
boolean[] visited = new boolean[matrix.length];
for(int i=0; i=rows || c<0 || c>=cols || matrix[r*cols+c]!=str[index] || visited[r*cols+c]) return false;
if(index == str.length -1) return true;
visited[r*cols+c] = true;
if(searchFromHere(matrix,rows,cols,r-1,c,index+1,str,visited)||
searchFromHere(matrix,rows,cols,r,c-1,index+1,str,visited)||
searchFromHere(matrix,rows,cols,r+1,c,index+1,str,visited)||
searchFromHere(matrix,rows,cols,r,c+1,index+1,str,visited))
return true;
visited[r*cols+c] = false;
return false;
}
}
题六十四 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
public class Solution {
public int movingCount(int threshold, int rows, int cols)
{ if(threshold <0) return 0;
boolean [][]dp = new boolean[rows+1][cols+1];
dp[0][0] = true;
for(int i=1; i<=rows; i++){
if(dp[i-1][0] && reach(threshold,i,0)) dp[i][0] = true;
else dp[i][0] = false;
}
for(int i=1; i<= cols; i++){
if(dp[0][i-1] && reach(threshold,0,i)) dp[0][i] = true;
else dp[0][i] = false;
}
for(int i=1; i<= rows; i++){
for(int j=1; j<= cols; j++){
if((dp[i-1][j] && reach(threshold,i,j))||(dp[i][j-1] && reach(threshold,i,j))) dp[i][j] = true;
else dp[i][j] = false;
}
}
int cnt=0;
for(int i=0; i< rows; i++){
for(int j=0; j< cols; j++){
if(dp[i][j]) cnt++;
}
}
return cnt;
}
public boolean reach(int thr, int x, int y){
int sum = 0;
while(x>0){
sum += x%10;
x /= 10;
}
while(y>0){
sum += y%10;
y /= 10;
}
return sum <= thr;
}
}