StringA的字符从新排列后,可否变成StringB 详细java
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;i<stringA.length();i++){ recoder[stringA.charAt(i)]++; recoder[stringB.charAt(i)]--; } for(int i=0;i<256;i++){ if(recoder[i]!=0) return false; } return true; } }
tips:node
第一步先判断两个字符串的长度是否相等数组
字符串的长度为.length()
有括号缓存
将数组中全部为0的元素所在的行列都置为0spa
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;i<n;i++){ for(int j=0;j<n;j++){ if(mat[i][j] == 0){ row[i] = true; col[j] = true; } } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(row[i]||col[j]){ mat[i][j]=0; } } } return mat; } }
tips指针
读数据和写数据必须分开。code
检查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排序
旋转问题:先将string1 收尾拼接,再检查新的字符串是否含有s2.递归
输入一个链表,输出该链表中倒数第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;i<k;i++){ tailp = tailp.next; if(tailp == null) return null; } while(tailp.next != null){ tailp = tailp.next; headp = headp.next; } return headp; }
tips
new一个obj1对象,而后obj1 = obj2 ,错错错
核心思想:两个指针,相差k-1,tail指到尾,则前指针正好找到想要的位置。
另一种思路是采用递归,head==null时,将count置零,以后count++。
删除单向链表中间的某个结点,而且只能访问该结点
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<Integer> 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
方案1:用快慢指针
,当快指针指向链表尾部时,慢指针正好指向中部,而且将慢指针压栈,这里要注意奇偶数的区别。
方案2:先将全部链表数据压到栈1,而后弹出一半到栈2,二者再进行比较。不过该方法显然没有方法一效率高。
public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); 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<Integer> twoStacksSort(int[] numbers) { // write code here ArrayList<Integer> arrayList = new ArrayList(); Stack<Integer> stack1 = new Stack(); Stack<Integer> stack2 = new Stack(); for(int i=0;i<numbers.length;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);
代码的简洁性!思惟不要太僵硬,能够多层条件一块儿考虑,没必要非要一层一层考虑分析。
不要先考虑stack2是否为空,再嵌套考虑栈顶是否大于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
这样的改进的好处在于不用遍历全部的树节点