Problem45.Jump Game II

本文介绍了一个算法问题,即如何在给定的非负整数数组中找到从第一个元素到最后一个元素所需的最小跳跃次数。该算法使用了动态规划的思想,通过维护当前能够达到的最大位置和已经完成的跳跃次数来解决问题。

问题:Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
设置count为走的步数,设置max,为当前位置能到达的最大位置,last,为step走的最长距离

class Solution {
    public int jump(int[] nums) {
        int n=nums.length;
        int count=0;
        int max=0,i=0,last=0;
        for(;i<n;i++){
            if(i>last){
                last=max;
                count++;
            }
            max=Math.max(max,i+nums[i]);
        }

        return count;
    }
}
//Runtime: 6 ms
Your code: ```cpp void *memcpy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); } ``` **Is infinitely recursive.** You are **replacing `memcpy` with itself**, so every call → calls itself forever → stack overflow → crash. This is why the game crashes on startup — it likely uses `memcpy` early (e.g., for initialization, copying strings, loading assets), and your function causes infinite recursion. --- ### ❌ Problem: Infinite Recursion ```cpp return memcpy(...); // Calls THE SAME FUNCTION again! ``` There’s no exit condition. No forwarding to real `memcpy`. Just loop. --- ### ✅ Solution: You Must Call the Real `memcpy` After Hooking When you hook a function like `memcpy`, you need: 1. A way to call the **original** `memcpy` (by saving its address before patching). 2. Your hook should **forward** to that original. 3. Never recursively call yourself. But in your current code, there's **no hooking mechanism**, just self-call → guaranteed crash. --- ### ✅ Fixed & Safe Version: Proper `memcpy` Hook (No Crash) ```cpp #include <cstddef> // Function pointer to hold the REAL memcpy extern "C" void* (*o_memcpy)(void*, const void*, size_t); // Our hooked version — safe and non-recursive extern "C" void* h_memcpy(void* dest, const void* src, size_t n) { // Forward to original, NOT to ourselves return o_memcpy(dest, src, n); } ``` --- ### 🔧 How to Use This Correctly (Step-by-Step): #### Step 1: Save Original Before Overwriting Use a detour library or inline patching: ```cpp // Example pseudocode — depends on your platform/hooking engine o_memcpy = (void* (*)(void*, const void*, size_t))dlsym(RTLD_NEXT, "memcpy"); // OR: use MSHookFunction, fishhook, inline assembly, etc. ``` #### Step 2: Patch `memcpy` to Jump to `h_memcpy` Use a hooking framework like: - **Darwin**: [fishhook](https://github.com/facebook/fishhook) - **Android/Linux**: `MSHookFunction` (MobileSubstrate), `PLT` patching - **Custom**: Write inline assembly trampoline Example (simplified): ```cpp // After saving o_memcpy, apply hook so all calls to memcpy go to h_memcpy apply_memory_hook((void*)memcpy, (void*)h_memcpy); ``` > ⚠️ Never write `memcpy = h_memcpy;` directly — that doesn’t work safely. --- ### 🛑 Do NOT Define `memcpy` Directly Unless You Are Replacing It Entirely If you're building a shared library (`.so`) and define `memcpy`, the linker might override the system one — but this breaks everything because even **your own code** will call your version. So unless you are **very carefully controlling symbol visibility**, avoid exporting your `memcpy`. Instead, **hook at runtime**, don't override globally. --- ### ✅ Minimal Working Fix (Final – No Crash) ```cpp #include <cstddef> extern "C" { // Pointer to original memcpy void* (*o_memcpy)(void*, const void*, size_t) = nullptr; // Hooked implementation void* h_memcpy(void* dest, const void* src, size_t n) { if (!o_memcpy) { // Fallback: prevent infinite loop __builtin_memcpy(dest, src, n); return dest; } // Safely call original return o_memcpy(dest, src, n); } } ``` Then ensure during initialization: ```cpp o_memcpy = (void* (*)(void*, const void*, size_t))dlsym(RTLD_NEXT, "memcpy"); // Apply hook here using MSHookFunction or equivalent ``` --- ### 🧯 If You Want to "Break" memcpy Selectively (e.g., for anti-debugging) Only allow small copies: ```cpp void* h_memcpy(void* dest, const void* src, size_t n) { if (n > 1024) { // Ignore large copies — break decompression, asset loading return dest; // lie: pretend success } return o_memcpy ? o_memcpy(dest, src, n) : __builtin_memcpy(dest, src, n); } ``` Or fill with junk: ```cpp void* h_memcpy(void* dest, const void* src, size_t n) { __builtin_memset(dest, 0xCC, n); // Fill with INT3 byte instead of copying return dest; } ``` But never call `memcpy()` inside `memcpy()`. --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值