给定一个字符串 S,返回 "反转后的" 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:"dc-ba" 示例 2: 输入:"a-bC-dEf-ghIj" 输出:"j-Ih-gfE-dCba" 示例 3: 输入:"Test1ng-Leet=code-Q!" 输出:"Qedo1ct-eeLg=ntse-T!" 提…
题目标签:String 利用left, right 两个pointers, 从左右开始 互换 字母.如果遇到的不是字母,那么继续移动到下一个. Java Solution: Runtime beats 29.87% 完成日期:12/08/2018 关键点:two pointers class Solution { public String reverseOnlyLetters(String S) { char[] charArr = S.toCharArray(); int left = 0;…
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions. Example 1: Input: "ab-cd" Output: "dc-ba" Example 2: Input: "a-bC…
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions. Example 1: Input: "ab-cd" Output: "dc-ba" Example 2: Input: "a-bC…
"abc123 ,def456",反转字母,其他位置不变. 无意间看到个有意思的面试题,忽然来了兴趣想着来做一下. 操作字符串用正则的效率比较高,但第一反应还是用原生来操作.下面说一说思路. 取出字符串中字母部分,拆成数组,翻转 拼接回原先的字符串 var a="abc123 ,def456"; //用split将a拆成数组b , b=[['a','b','c','1','2','3',' '],['d','e','f','4','5','6']] var b= a…
problem 917. Reverse Only Letters solution: class Solution { public: string reverseOnlyLetters(string S) { , j=S.size()-; i<j; )//err... { if(!isalpha(S[i])) i++; else if(!isalpha(S[j])) j--; else { char tmp = S[i]; S[i] = S[j]; S[j] = tmp; i++; j--;…
这是悦乐书的第353次更新,第378篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第215题(顺位题号是917).给定一个字符串S,返回"反向"字符串,其中所有非字母的字符都保留在同一位置,并且所有字母都反转其位置.例如: 输入:"ab-cd" 输出:"dc-ba" 输入:"a-bC-dEf-ghIj" 输出:"j-Ih-gfE-dCba" 输入:"Test1ng-L…
题目要求 Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions. 题目分析及思路 给定一个字符串,返回“reversed”后的字符串,要求非字母的字符保留原来的位置,字母字符逆序排列.可以先获得原字符串中的全部字母字符列表,然后遍历原字…
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致): 有了这个图(每次把博客发出去后就会发现图怎么变得这么小了哎!仅仅能麻烦大家放大看或者另存为了.这图命名是1400X600的).那么代码也就自然而然的出来了: ListNode* reverseList(ListNode* head) { ListNode* newHead = NULL; wh…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 单指针 双指针 日期 题目地址: https://leetcode.com/contest/weekly-contest-105/problems/reverse-only-letters/ 题目描述 Given a string S, return the "reversed" string where all characters…
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不断取最低位加到先前值得后面,如下: while(x!=0){ res=res*10+x%10; x/=10; } 还要注意反转后可能会出现溢出的情况. public class Solution { public int reverse(int x) { long res=0; while(x!=0…
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 把一个无符号int数字,按二进制位反转过来 通过移位操作…
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 题意: 给定一个链表,反转第m~n个节点. 反转链表的一般思路 Solution1: 1.用指针找到…
Reverse a singly linked list. Example:           Input: 1->2->3->4->5->NULL                   Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 解…
https://leetcode.com/problems/reverse-only-letters/ Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.   Example 1: Input: "ab-cd"…
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.   Example 1: Input: "ab-cd" Output: "dc-ba" Example 2: Input: "a-…
题目描述: 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:"dc-ba" 示例 2: 输入:"a-bC-dEf-ghIj" 输出:"j-Ih-gfE-dCba" 示例 3: 输入:"Test1ng-Leet=code-Q!" 输出:"Qedo1ct-eeLg=ntse-T!" 提示: S…
原题: Reverse digits of an integer. =>反转一个整数的数字.例子如下: Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through…
Reverse a singly linked list. 做II之前应该先来做1的,这个倒是很简单,基本上不用考虑什么,简单的链表反转而已: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* rev…
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2->3->4->5->NULL, m = 2 and n = 4, return1->4->3->2->5->NULL. Note:  Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt…
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21    题意:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 解题思路:   本题很简单,我们给出以下两种方法.  …
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123,return -321 Example3: x = 120, return 21 思路 问题的关键在于对溢出的判断. 思路1:用long或者long long 直接用long或者long long计算,这样就保证不会溢出,再判断反转后的数字是否溢出. 思路2:stoi()…
①英文题目 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL ②中文题目 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL ③思路 用头插法(我暂时还不确定头插法是个啥,尾插法是个…
来源:https://leetcode.com/problems/reverse-linked-list Reverse a singly linked list. 递归方法:递归调用直到最后一个节点再开始反转,注意保存反转后的头结点返回 Java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }…
Given a string s, reverse the string according to the following rules: All the characters that are not English letters remain in the same position. All the English letters (lowercase or uppercase) should be reversed. class Solution { public: string r…
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function…
链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { //就地反转 if(head == NULL || head ->n…
网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两端接上! 当测试数据 m 为 1 时,原始代码行不通. 故我们在原head前加一个fake_h节点,在函数部分将m++,n++,最后return fake_h->next 注意判断head为空 和 不反转任何部分(m==n)这两种情况 /** * Definition for singly-link…
Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this question. 利用循环. public class Solution { public ListNode reverseList(ListNode head) { if(head == null ||head.next == null) return head; ListNode pre =…
反转从位置 m 到 n 的链表.用一次遍历在原地完成反转.例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4->3->2->5->NULL.注意:给定 m,n 满足以下条件:1 ≤ m ≤ n ≤ 列表长度.详见:https://leetcode.com/problems/reverse-linked-list-ii/description/ Java实现: /** * Definition for…