当前位置:网站首页>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;
}
}
边栏推荐
- Sqli labs customs clearance hey hey~
- Hwang
- 【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
- Seeing the sudden death of a 28 year old employee, I was silent
- NFT platform development NFT mall source code NFT mall development chain game development
- upload-labs通关未半而中道崩殂
- [solution] codeforces round 798 (Div. 2)
- Line up to pick up the express. At this meeting, I sorted out all kinds of code sets
- Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall
- Several ways to recover tidb data from accidental deletion
猜你喜欢

How ZABBIX can customize MySQL monitoring items and trigger alarms

Windows technology - how to view the instruction set, model, attribute and other details supported by the CPU, and how to use the CPU-Z tool to view the processor, memory, graphics card, motherboard,

Why is the UDP stream set to 1316 bytes

After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again

社会工程学实战入门

Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall

vulhub

Expérience initiale du moteur de segmentation de l'araignée mariadb
![Spring 2021 daily question [week6 not finished]](/img/32/74cff6f057ef9a7941fd6a41dc6635.jpg)
Spring 2021 daily question [week6 not finished]

Seeing the sudden death of a 28 year old employee, I was silent
随机推荐
密评-----
送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔
The top ten trends of 2022 industrial Internet security was officially released
6-8 reading and writing of structured files 1
Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
Hello go (XIII). Go language common standard library III
Winter vacation daily question (improvement group) [end of week 4]
spawn ./ gradlew EACCES at Process. ChildProcess._ handle. onexit
jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
Why OKR needs to be challenging
mariadb spider分片引擎初體驗
Comparison of mongoose in express, KOA and egg
Experiment 2: write a program and verify that the linear table sequence represents all operations
Getting started with Wireshark
Windows technology - how to view the instruction set, model, attribute and other details supported by the CPU, and how to use the CPU-Z tool to view the processor, memory, graphics card, motherboard,
【深度学习基础】神经网络的学习(3)
社会工程学实战入门
Mysql8 installation, Navicat installation, sqli labs setup
Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
6-5 count the number of words (file) (*)