reverse array java
/* package whatever; // don't place package name! */ import java.util.*;
import java.lang.*;
import java.io.*; /* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] a = {1,2,4,5};
System.out.println( Arrays.toString(reverseArray(a)));
} public static int[] reverseArray(int[] nums){
int begin = 0;
int end = nums.length -1;
while(begin < end){
swap(nums, begin++, end--);
}
return nums;
} private static int[] swap(int[] nums, int begin, int end){
int temp = nums[begin];
nums[begin] = nums[end];
nums[end] = temp;
return nums;
}
}
reverse array java的更多相关文章
- [Algorithm] Reverse array of Chars by word
For example we have: ["p", "r", "e", "f", "e", &qu ...
- 7. Reverse Integer java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代码如下: pub ...
- leetcode 153. Find Minimum in Rotated Sorted Array --------- java
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Reverse Integer (JAVA)
public class Solution { public int reverse(int x) { StringBuffer sb = new StringBuffer(x+"" ...
- 【数据结构】算法 LinkList (Reverse LinkedList) Java
反转链表,该链表为单链表. head 节点指向的是头节点. 最简单的方法,就是建一个新链表,将原来链表的节点一个个找到,并且使用头插法插入新链表.时间复杂度也就是O(n),空间复杂度就需要定义2个节点 ...
- leetcode 7. Reverse Integer [java]
public int reverse(int x) { long res = 0; while (x != 0){ res = res* 10 + x % 10; x /= 10; } if(res ...
- Reverse array
数组颠倒算法 #include <iostream> #include <iterator> using namespace std; void reverse(int* A, ...
- CUDA学习,使用shared memory实现Reverse Array
- leetcode 33 Search in Rotated Sorted Array JAVA
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个 ...
随机推荐
- Scala之Map,Tuple
/** * 1,默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变: * 2,Map的实例是调用工厂方法模式apply来构造Map实例,而需要 ...
- LinuxMint下Docker的安装部署和验证
通过lsb_release命令查看以下我的LinuxMint发行版, 查看以下我的Linux内核版本, Docker要求Linux内核版本必须在要在3.10以上,显然我们的系统是满足的. 1. Doc ...
- python3 入门 (四) 类与继承
Python 类 Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用基类中的同名方法. 对象可以包含任意数量和类型的数据. p ...
- Cordova4.0 系列 -- 基本环境搭建(1)
一. 安装Node.js基本环境 官网下载地址:https://nodejs.org/ 安装成功之后可以使用简单命令查看其版本 node -v npm相关命令 node cli.js install ...
- 备忘:SSRS技巧三则
前言 最近在弄SSRS,发现了三个小技巧.在此记录一下.免得以后忘了. 技巧 1. SSRS输出成EXCEL时,让两个dataset各占一个sheet, 这个简单,在其中一个table的属性加上pag ...
- Java 读取文件到字符串
Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...
- web.xml中配置固定数据
在web.xml单个servlet中配置的数据的存取 存: <servlet> <description>This is the description of my J2EE ...
- Servlet Study 1
this content below are come from the JSR154 by sun Just for record purpose. if this relate to some ...
- 读JS高级(兼容&&BOM&&私有变量&&面向对象)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS模式:简单的图书馆享元模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...