【leetcode】solution in java——Easy2

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html

6:Reverse String

Write a function that takes a string as input and returns the string reversed.

此题:字符串反转题。

思路:此题大一学C++的时候上机题就做过了,当时思路是:把String转化为char[],从尾到头遍历一次倒序地把字符复制到另一个数组(从头到尾),然后把新数组toString()即可。现在用Java做的话,把新数组换成用StringBuffer代替,倒序遍历char[]逐个append到buffer,然后toString()即可。由于每个元素都遍历了一次,复杂度O(n)。

 public String reverseString(String s) {
        char[] chars=s.toCharArray();
        StringBuffer buffer=new StringBuffer();
        for(int i=chars.length-1;i>=0;--i){
            buffer.append(chars[i]);
        }
        return buffer.toString();
    }

第二种思路是:把char[]的前后半交换即可,只需遍历一半,复杂度O(n/2)。

 public String reverseString(String s) {
        char[] chars=s.toCharArray();
        int half=chars.length/2;
        for(int i=0;i<half;++i){
            char temp=chars[chars.length-1-i];
            chars[chars.length-1-i]=chars[i];
            chars[i]=temp;
        }
        //这里注意:char[]转化为String是通过new String(char[])实现的
        return new String(chars);
    }
时间: 2024-08-13 13:06:36

【leetcode】solution in java——Easy2的相关文章

【Leetcode】Sort List JAVA实现

Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代码实现 package com.edu.leetcode; import com.edu.leetcode.ListNode; public class SortList { /** * @param args */ public ListNode Merge(ListNode first,List

【Leetcode】Reorder List JAVA

一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 二.分析 1.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&