代码:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null || headB==null)
return null;
ListNode pA=headA, pB=headB;
while(pA!=pB){
pA= pA==null ? headB : pA.next;
pB= pB==null ? headA : pB.next;
}
return pA;
}
}
改进:
双指针法。 假设两个链表相交之前的节点数目分别为a,b,相交之后的节点数目为c。若a==b,显然两个指针指向同一个节点时该节点为所求节点;否则,当第一个指针指向空时,指针重新指向B的首节点,当第二个指针指向空时,指针重新指向A的首节点。若两个链表相交,则他们最终都会经过a+b+c步指向相交的节点。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null || headB==null)
return null;
ListNode pA=headA, pB=headB;
while(pA!=pB){
pA= pA==null ? headB : pA.next;
pB= pB==null ? headA : pB.next;
}
return pA;
}
}
代码:
递归。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==p||root==q||root==null) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left==null) return right;
if(right==null) return left;
return root;
}
}
方法二:
迭代。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val>q.val){
TreeNode tmp = p;
p = q;
q = tmp;
}
while(root != null){
if(root.val > q.val)
root=root.left;
else if(root.val<p.val)
root=root.right;
else break;
}
return root;
}
}
方法三:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val>p.val&&root.val>q.val)
return lowestCommonAncestor(root.left,p,q);
if(root.val<p.val&&root.val<q.val)
return lowestCommonAncestor(root.right,p,q);
return root;
}
}
代码:
解题思路
class Solution {
public int findNthDigit(int n) {
int digit=1;
long start=1, count=9;
while(n>count){
n-=count;
digit+=1;
start*=10;
count = 9*start*digit;
}
long num =start + (n-1)/digit;
return Long.toString(num).charAt((n-1)%digit)-'0';
}
}
代码:
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length-1;
while(left<=right){
int mid = (left+right)/2;
if(nums[mid]<target)
left=mid+1;
else
right=mid-1;
}
if(nums.length ==0 || left==nums.length || nums[left]!=target)
return 0;
int res=0;
while(++right<nums.length && nums[right]==target)
res++;
return res;
}
}
法二:
class Solution {
public int search(int[] nums, int target) {
int i=0, j=nums.length-1;
while(i<=j){
int m=(i+j)/2;
if(nums[m]>target)
j=m-1;
else
i=m+1;
}
if(j>=0 && nums[j]!=target)
return 0;
int right=i;
i=0;
j=nums.length-1;
while(i<=j){
int m=(i+j)/2;
if(nums[m]<target)
i=m+1;
else
j=m-1;
}
int left=j;
return right-left-1;
}
}
法三:
class Solution {
public int search(int[] nums, int target) {
return helper(nums,target)-helper(nums,target-1);
}
int helper(int[] nums, int target){
int i = 0, j=nums.length-1;
while(i<=j){
int m = (i+j)/2;
if(nums[m]>target)
j=m-1;
else
i=m+1;
}
return j;
}
}
代码:
创建一个栈模拟进栈出栈操作。
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int i=0;
for(int num:pushed){
stack.push(num);
while(!stack.isEmpty() && stack.peek()==popped[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
|