avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-344-Reverse String
文章

Leetcode-344-Reverse String

Posted 2022-07-10
2~2 min read

leetcode:https://leetcode.com/problems/reverse-string

Desciption:
Write a function that reverses a string. The input string is given as an array of characters s

idea:

step1: set two pointer(start index, end index)

step2: By setting the temporary variable, swap the front and back elements until the middle one is reached

Code:

class Solution {
    public void reverseString(char[] s) {
        int i = 0;
        int j = s.length-1;
        if(j == 0){
            System.out.print("");
        }
        while(i<j){
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }
}
Leetcode
Leetcode String
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

method and error.md

NEWER

Leetcode-541-Reverse String 2

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents