StringA的字符重新排列后,能否变成StringB 详细
import java.util.*;
public class Same {
public boolean checkSam(String stringA, String stringB) {
// write code here
if(stringA.length()!=stringB.length())
return false;
int[] recoder = new int[256];
for(int i=0;ifor(int i=0;i<256;i++){
if(recoder[i]!=0)
return false;
}
return true;
}
}
tips:
.length()
有括号将数组中所有为0的元素所在的行列都置为0
import java.util.*;
public class Clearer {
public int[][] clearZero(int[][] mat, int n) {
// write code here
boolean[] row = new boolean[n];
boolean[] col = new boolean[n];
for(int i=0;ifor(int j=0;jif(mat[i][j] == 0){
row[i] = true;
col[j] = true;
}
}
}
for(int i=0;ifor(int j=0;jif(row[i]||col[j]){
mat[i][j]=0;
}
}
}
return mat;
}
}
tips
检查string2是否为sting1旋转而成
public boolean checkReverseEqual(String s1, String s2) {
// write code here
if (s1 == null || s2 == null || s1.length() != s2.length())
return false;
return (s1+s1).contains(s2);
}
tips
输入一个链表,输出该链表中倒数第k个结点
public ListNode FindKthToTail(ListNode head,int k) {
//需不需要new???
//ListNode headp = new ListNode(-1);
if(head == null||k<1) return null;
ListNode tailp = head;
ListNode headp = head;
for(int i=1;iif(tailp == null) return null;
}
while(tailp.next != null){
tailp = tailp.next;
headp = headp.next;
}
return headp;
}
tips
删除单向链表中间的某个结点,并且只能访问该结点
public boolean removeNode(ListNode pNode) {
// write code here
if(pNode == null || pNode.next == null)
return false;
pNode.val = pNode.next.val;
pNode.next = pNode.next.next;
return true;
}
tips
链表头为个位,A{1,2,3},B{3,2,1},则返回{4,4,4}
public ListNode plusAB(ListNode a, ListNode b) {
// write code here
int flag = 0;
ListNode result = new ListNode(-1);
ListNode phead = result;
while(a!=null || b!=null || flag!=0){
int sum = flag;
if(a!=null){
sum+=a.val;
a = a.next;
}
if(b!=null){
sum+=b.val;
b = b.next;
}
int val = sum%10;
flag = sum/10;
result.next = new ListNode(val);
result = result.next;
}
return phead.next;
}
tips
之前有一个想法就是先相加公共部分,然后处理多出来的部分,这样处理起来非常麻烦。
如果链表头为高位,则采用栈方法处理。先对两个链表分别压栈,最后弾栈,直至两个都为空并且进位等于0。
检查链表是否为回文,{1,2,3,2,1},返回true
public boolean isPalindrome(ListNode pHead) {
// 快慢指针
ListNode fast = pHead;
ListNode slow = pHead;
Stack stack = new Stack<>();
while(fast != null && fast.next != null){
stack.push(slow.val);
slow = slow.next;
fast = fast.next.next;
}
if(fast != null)
slow = slow.next;
while(slow != null){
if(slow.val != stack.pop())
return false;
slow = slow.next;
}
return true;
}
public boolean isPalindrome(ListNode pHead) {
//双栈
if(pHead == null || pHead.next == null)
return true;
Stack stack1 = new Stack();
Stack stack2 = new Stack();
while(pHead!=null){
stack1.push(pHead.val);
pHead = pHead.next;
}
while(stack1.size()>stack2.size()){
stack2.push(stack1.pop());
}
if(stack2.size()>stack1.size()){
stack2.pop();
}
while(!stack1.empty() && !stack2.empty()){
if(stack1.pop() != stack2.pop())
return false;
}
return true;
}
tips
快慢指针
,当快指针指向链表尾部时,慢指针正好指向中部,并且将慢指针压栈,这里要注意奇偶数的区别。public class Solution {
Stack stack1 = new Stack();
Stack stack2 = new Stack();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.isEmpty() && stack2.isEmpty()){
throw new RuntimeException("the queue is empty!");
}
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
tips
throw new RuntimeException("the queue is empty!");
下次可以用要求只有一个缓存栈,并且排好序的栈最大元素在栈顶。
public ArrayList twoStacksSort(int[] numbers) {
// write code here
ArrayList arrayList = new ArrayList();
Stack stack1 = new Stack();
Stack stack2 = new Stack();
for(int i=0;ilength;i++){
stack1.push(numbers[i]);
}
while(!stack1.isEmpty()){
int temp = stack1.pop();
while(!stack2.isEmpty() && stack2.peek()>temp){
stack1.push(stack2.pop());
}
stack2.push(temp);
}
while(!stack2.isEmpty()){
arrayList.add(stack2.pop());
}
return arrayList;
}
tips
while(!stack2.isEmpty() && stack2.peek()>temp){
stack1.push(stack2.pop());
}
stack2.push(temp);
树的平衡指左右高度相差不能大于1
public boolean isBalance(TreeNode root) {
// 遍历整个树的所有节点
if(root == null)return true;
int left = getHeight(root.left);
int right = getHeight(root.right);
int cha = Math.abs(left-right);
if(cha>1)return false;
else return true;
}
public int getHeight(TreeNode root){
if(root == null) return 0;
int left = getHeight(root.left);
int right = getHeight(root.right);
return Math.max(left,right)+1;
}
public boolean isBalance(TreeNode root) {
// write code here
if(root == null)return true;
if(getHeight(root) == -1)return false;
return true;
}
public int getHeight(TreeNode root){
if(root == null) return 0;
int left = getHeight(root.left);
if(left == -1) return -1;
int right = getHeight(root.right);
if(right == -1) return -1;
if(Math.abs(left-right)>1) return -1;
else return Math.max(left,right)+1;
}
tips
给定一个元素各不相同的升序数组,生成一个最小二叉查找树
public TreeNode creatBST(int[] arr,int start,int end){
if(start>end)return null;
int mid = (start+end)>>1;
TreeNode n = new TreeNode(arr[mid]);
n.left = creatBST(arr,start,mid-1);
n.right = creatBST(arr,mid+1;end);
return n;
}
tips
给定一颗二叉树,创建含有某一深度上所有结点的链表。
private ListNode result= new ListNode(-1);
public ListNode getTreeLevel(TreeNode root, int dep) {
// write code here
ListNode list = result;
getTree(root,dep,1);
return list.next;
}
public void getTree(TreeNode root,int dep,int i){
if(root == null) return;
if(i == dep){
result.next = new ListNode(root.val);
result = result.next;
}else{
getTree(root.left,dep,i+1);
getTree(root.right,dep,i+1);
}
}
int former = Integer.MIN_VALUE;
public boolean checkBST(TreeNode root){
if(root == null) return true;
if(!checkBST(root.left)) return false;
if(root.val <= former) return false;
former = root.val;
if(!checkBST(root.right)) return false;
return true;
}
tips
Integer.MIN_VALUE
即min-value是Integer类里面的变量值。