当前位置:网站首页>Sword finger offer (2nd Edition)
Sword finger offer (2nd Edition)
2022-06-11 18:10:00 【Ppdy bud】
List of articles
https://leetcode.cn/problem-list/xb9nqhhg/
- The finger of the sword Offer 52. The first common node of two linked lists
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
ListNode temp = headA;
while(temp!=null){
set.add(temp);
temp=temp.next;
}
temp = headB;
while(temp!=null){
if(!set.add(temp)){
return temp;
}
temp=temp.next;
}
return null;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode A = headA;
ListNode B = headB;
while (A != B) {
A = A == null ? headB : A.next;
B = B == null ? headA : B.next;
}
return B;
}
}
- The finger of the sword Offer 24. Reverse a linked list
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode p = null,pre =null;
while(cur!=null){
p = cur;
cur = cur.next;
p.next =pre;
pre = p;
}
return p;
}
}
- The finger of the sword Offer 09. Queues are implemented with two stacks
class CQueue {
private Stack<Integer> inStack ;
private Stack<Integer> outStack;
public CQueue() {
inStack = new Stack<>();
outStack= new Stack<>();
}
public void appendTail(int value) {
inStack.add(value);
}
public int deleteHead() {
if(outStack.isEmpty()){
if(inStack.isEmpty()){
return -1;
}
while(!inStack.isEmpty()){
outStack.add(inStack.pop());
}
}
return outStack.pop();
}
}
- The finger of the sword Offer 11. Minimum number of rotation array
class Solution {
public int minnArray(int[] numbers) {
Arrays.sort(numbers);
return numbers[0];
}
}
class Solution {
public int minArray(int[] numbers) {
int low = 0;
int high = numbers.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (numbers[mid] < numbers[high]) {
high = mid;
} else if (numbers[mid] > numbers[high]) {
low = mid + 1;
} else {
high -= 1;
}
}
return numbers[low];
}
}
- The finger of the sword Offer 06. Print linked list from end to end
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack = new Stack<>();
ListNode p = head;
int size =0;
while(p!=null){
stack.push(p.val);
p=p.next;
size ++;
}
int [] ans = new int[size];
int o = 0;
while(!stack.isEmpty()){
ans[o++]=stack.pop();
}
return ans;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
ListNode cur = head;
ListNode p = null;
ListNode pre = null;
int size = 0;
while(cur !=null){
p = cur ;
cur = cur.next;
p.next = pre;
pre= p;
size ++;
}
int [] ans = new int [size];
for(int i =0;i< size;i++){
ans[i] = p.val;
p = p.next;
}
return ans;
}
}
The finger of the sword Offer 10- I. Fibonacci sequence
class Solution {
public int fib(int n) {
final int MOD = 1000000007;
if(n<2){
return n;
}
int p = 0, q = 0, r =1;
for(int i = 2; i<= n;++i){
p = q ;
q = r ;
r = (p+q)%MOD;
}
return r;
}
}
- The finger of the sword Offer 10- II. The problem of frog jumping on the steps
class Solution {
public int numWays(int n) {
final int MOD = 1000000007;
int p = 0, q = 1,r=1;
for(int i = 2;i <= n ; i++){
p = q;
q = r;
r = (p+q)%MOD;
}
return r;
}
}
- The finger of the sword Offer 17. Print from 1 To the biggest n digit
class Solution {
public int[] printNumbers(int n) {
int num = 9;
for(int i = 1;i<n;i++){
num = num*10+9;
}
int print[] = new int[num];
for(int i = 0 ;i < num ;i++){
print[i] = i+1;
}
return print;
}
}
边栏推荐
- Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
- [foundation of deep learning] learning of neural network (3)
- Summary of clustering methods
- 安装mariadb 10.5.7(tar包安装)
- Line up to pick up the express. At this meeting, I sorted out all kinds of code sets
- 6-8 creating and traversing linked lists
- Comparison of mongoose in express, KOA and egg
- 【先收藏,早晚用得到】100个Flink高频面试题系列(三)
- Sqli labs customs clearance hey hey~
- Chorus翻译
猜你喜欢

Initial egg framework

【C】 Compilation preprocessing and environment

密码学概述

vulhub

Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)

Understanding of distributed transactions

Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection

Service learning notes 01 start method and life cycle

Bracket generation ---2022/02/25

光纤熔接知识汇总【转载自微信公众号弱电智能化工程2018】
随机推荐
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
R language mice package error in terms Formula (TMP, simplify = true): the model formula in extractvars is incorrect
Tidb GC related problems
谈谈远程工作 | 社区征文
6-8 reading and writing of structured files 1
6-7 file read / write operation
ctf入门
Network Security Threat Intelligence System
mariadb spider分片引擎初体验
6-3 reading articles (*)
GB gb28181 protocol video platform easygbs adds or deletes offline channels
密评-----
Can 400 fans earn 20W a month? How did you do it?
ADB command learning notes
Service learning notes 01 start method and life cycle
Speed adjustment of tidb DDL
6-1 read string (*) from file
[practical Script] obtain the line number of a file, and then delete the file content.
Several ways to recover tidb data from accidental deletion
Tidb CDC log tables are not eligible to replicate